Sha256: a77c1c8627a55b4c1a68982dd1c1bbbb9c0ccb02018a2b701bf87314f6c4c824

Contents?: true

Size: 1.91 KB

Versions: 28

Compression:

Stored size: 1.91 KB

Contents

module Medivo
  class PdfGroup

    attr_reader :pdf

    def initialize
      @pdfs = []
    end

    ##
    # create a pdf group returns you a PdfGroup class
    # to get the pdf call #pdf, or read the pdf byes, call #read
    def self.create(&block)
      raise "need to pass a block do create" unless block_given?
      @self_before_instance_eval = eval "self", block.binding
      pdf_group = PdfGroup.new
      pdf_group.instance_eval &block
      pdf_group.combine_pdfs
      pdf_group
    end

    def method_missing(method, *args, &block)
      @self_before_instance_eval = self.class.instance_variable_get :@self_before_instance_eval
      @self_before_instance_eval.send method, *args, &block
    end

    def variable_fields(file_path, variables)
      @pdfs << PdfGenerator.variable_fields(file_path, variables)
    end

    def variable_fields_with_images(file_path, variables, image_data)
      @pdfs << PdfGenerator.variable_fields_with_images(file_path, variables, image_data)
    end

    def lab_requisition(requisition_id)
      pdf_bytes = Medivo::Order.pdf_requisition(requisition_id)
      @pdfs << PdfGenerator.tmp_pdf { |pdf| pdf.write pdf_bytes }
    end

    def lab_result(requisition_id)
      pdf_bytes = Medivo::Order.pdf_result(requisition_id)
      @pdfs << PdfGenerator.tmp_pdf { |pdf| pdf.write pdf_bytes }
    end

    def static_pdf(path)
      @pdfs << File.new(path)
    end

    ##
    # Combines the PDFs
    def combine_pdfs
      @pdf = PdfGenerator.tmp_pdf(false) do |pdf|
        args = [@pdfs.collect(&:path), 'cat', 'output', pdf.path].join(' ')
        `pdftk #{args}`
      end

      @pdfs.each do |file|
        file.close
        file.unlink if file.respond_to? :unlink
      end
    end

    def read
      pdf.read
    end

    # don't really have to do close the pdf, but I think it will trash quicker if you do
    def close
      if pdf
        pdf.close
        pdf.unlink
      end
    end

  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
medivo-0.2.1 lib/pdf/medivo/pdf_group.rb
medivo-0.2.0 lib/pdf/medivo/pdf_group.rb
medivo-0.1.25 lib/pdf/medivo/pdf_group.rb
medivo-0.1.24 lib/pdf/medivo/pdf_group.rb
medivo-0.1.23 lib/pdf/medivo/pdf_group.rb
medivo-0.1.22 lib/pdf/medivo/pdf_group.rb
medivo-0.1.21 lib/pdf/medivo/pdf_group.rb
medivo-0.1.20 lib/pdf/medivo/pdf_group.rb