Skip to content

Instantly share code, notes, and snippets.

@ckgagan
Created March 3, 2017 17:21
Show Gist options
  • Save ckgagan/3b6caef0d19f221a1a8451f10aed654a to your computer and use it in GitHub Desktop.
Save ckgagan/3b6caef0d19f221a1a8451f10aed654a to your computer and use it in GitHub Desktop.
require 'prawn'
require 'pdf/toolkit'
class PdfFiller
DOWNLOAD = "/tmp/downloads"
FINAL_DIR = "#{Dir.home}/edited_files"
attr_reader :file_path
def initialize( file_path)
@file_path = file_path
system("mkdir -p #{FINAL_DIR}")
end
def fill_data
prawn_file = "#{DOWNLOAD}/temp_#{Time.current.to_i}.pdf"
output_file = "#{FINAL_DIR}/#{file_path.split('/').last}"
hash = data_hash
Prawn::Document.generate(prawn_file) do |file|
# coordinates are generated with hit and trial method
file.text_box hash[:city], :at => [115, 335]
file.text_box hash[:name], :at => [200, 290]
file.start_new_page
end
if ::PDF::Toolkit.pdftk(prawn_file, "multibackground", file_path, "output", output_file)
output_file
else
raise "PDF merging exception raised"
end
end
def data_hash
{
city: "city name",
name: "any string"
}
end
end
NOTE: system must have pdftk installed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment