Sha256: 4102f0ac801c4e858ae7b46842aad98da89078bb93e716fbfa44e0c575489a33

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'prawn'

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

      # @param [Thinreports::Report::Base] report
      # @param [Hash] security (nil)
      def initialize(report, security: 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

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

7 entries across 7 versions & 1 rubygems

Version Path
thinreports-0.12.1 lib/thinreports/generator/pdf.rb
thinreports-0.12.0 lib/thinreports/generator/pdf.rb
thinreports-0.11.0 lib/thinreports/generator/pdf.rb
thinreports-0.10.3 lib/thinreports/generator/pdf.rb
thinreports-0.10.2 lib/thinreports/generator/pdf.rb
thinreports-0.10.1 lib/thinreports/generator/pdf.rb
thinreports-0.10.0 lib/thinreports/generator/pdf.rb