Sha256: e80f7b31d64b705c8aa8c33f5c31138f1dad16ceb964054475c79b6cea35dcd1

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module Medivo
  class PdfGenerator

    def self.tmp_pdf
      pdf = Tempfile.new('pdf', :encoding => 'ascii-8bit')
      yield pdf
      pdf.close
      pdf
    end

    ##
    # file_path   String  path to pdf file with variable fields
    # variables   Hash    the hash of variables to fill in
    #
    # return  pdf File
    def self.variable_fields(file_path, variables)
      raise "variables #{variables} should be a hash" unless variables.is_a? Hash
      tmp_pdf do |pdf|
        fdf = FdfGenerator.file(variables)
        system 'pdftk', file_path, 'fill_form', fdf.path, 'output', pdf.path, 'flatten'
      end
    end

    ##
    # file_path   String  path to pdf file with variable fields
    # variables   Hash    the hash of variables to fill in
    # images      Array   of image info like:
    #           [ {:path=>'/absolute/path/to/image1.jpg', :at=>[x_pos,y_pos]},
    #            {:path=>'/absolute/path/to/image2.jpg', :at=>[20,800], :width=>20, :height=>40} ]
    #
    # return  pdf File
    def self.variable_fields_with_images(file_path, variables, images=[])
      content_pdf = variable_fields(file_path, variables)
      img_pdf = Prawn::Document.generate('dude.pdf') do |pdf|
        images.each do |img_info|
          pdf.image img_info[:path], :at=> img_info[:at]
        end
      end
      tmp_pdf do |pdf|
        system 'pdftk', content_pdf.path, 'stamp', img_pdf.path, 'output', pdf.path
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
medivo-0.1.8 lib/pdf/medivo/pdf_generator.rb