Sha256: 9822508b027f722ad3cf041ab2c3c339d9ddff221b12067f4d435413ac9de73a

Contents?: true

Size: 1.95 KB

Versions: 24

Compression:

Stored size: 1.95 KB

Contents

module Medivo
  class PdfGenerator

    def self.tmp_pdf(auto_close=true)
      pdf = Tempfile.new('pdf', :encoding => 'ascii-8bit')
      yield pdf
      # if you don't close here the helper method pdf_to_text does not work
      # its also a good idea to close the temp file anyway
      pdf.close if auto_close
      pdf
    end

    ##
    # @param  file_path   [String]  path to pdf file with variable fields
    # @param  variables   [Hash]    the hash of variables to fill in
    #
    # @return [File]  the 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'
        fdf.unlink
      end
    end

    ##
    # @param  file_path   [String]  path to pdf file with variable fields
    # @param  variables   [Hash]    the hash of variables to fill in
    # @param  image_data  [Array]   of image info like:
    #   [ {:path=>'/absolute/path/to/image1.jpg', :options=>{ :at=>[x_pos, y_pos]} },
    #    {:path=>'/absolute/path/to/image2.jpg', :options=>{ :at=>[20, 800], :width=>20, :height=>40} }
    #   ]
    #
    # @return  [File]  pdf
    #
    # NOTE: you need to create tmp dir in home directory for temporary image files
    #
    def self.variable_fields_with_images(file_path, variables, image_data=[])
      content_pdf = variable_fields(file_path, variables)
      image_path =  "#{ENV['HOME']}/tmp/image_path_#{SecureRandom.hex(6)}_#{Time.now.to_i}.pdf"
      img_pdf = Prawn::Document.generate(image_path) do |pdf|
        image_data.each do |data|
          pdf.image data[:path], data[:options]
        end
      end
      pdf = tmp_pdf do |pdf|
        system 'pdftk', content_pdf.path, 'stamp', img_pdf.path, 'output', pdf.path
      end
      content_pdf.unlink
      File.delete(img_pdf.path)
      pdf
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
medivo-0.2.24 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.21 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.20 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.19 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.18 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.17 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.16 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.15 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.14 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.13 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.12 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.11 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.10 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.9 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.8 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.7 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.6 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.5 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.3 lib/pdf/medivo/pdf_generator.rb
medivo-0.2.2 lib/pdf/medivo/pdf_generator.rb