Sha256: 2a3d71993435bc66b1dffca923d041a00c47af2870e02eeefdce530827a937c6

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'prawn'
require 'prawn/disable_word_break'

Prawn::DisableWordBreak.config.default = false

module Thinreports
  module BasicReport
    module Generator
      class PDF
        # @return [Thinreports::BasicReport::Report::Base]
        attr_reader :report

        # @param [Thinreports::BasicReport::Report::Base] report
        # @param [Hash] security (nil)
        # @param [String] title (nil)
        def initialize(report, security: nil, title: nil)
          report.finalize

          @report = report.internal
          title ||= default_layout ? default_layout.format.report_title : nil

          @document = Document.new(title: title, security: security)
          @drawers = {}
        end

        # @param [String, nil] filename
        # @return [String, nil]
        def generate(filename = nil)
          draw_report
          filename ? @document.render_file(filename) : @document.render
        end

        def default_layout
          report.default_layout
        end

        private

        def draw_report
          report.pages.each do |page|
            draw_page(page)
          end
        end

        def draw_page(page)
          return @document.add_blank_page if page.blank?

          format = page.layout.format
          @document.start_new_page(format)

          drawer(format).draw(page)
        end

        def drawer(format)
          @drawers[format.identifier] ||= Drawer::Page.new(@document, format)
        end
      end
    end
  end
end

require_relative 'pdf/prawn_ext'
require_relative 'pdf/document'
require_relative 'pdf/drawer/base'
require_relative 'pdf/drawer/page'
require_relative 'pdf/drawer/list'
require_relative 'pdf/drawer/list_section'

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thinreports-0.14.2 lib/thinreports/basic_report/generator/pdf.rb
thinreports-0.14.1 lib/thinreports/basic_report/generator/pdf.rb