This gem is a Ruby on Rails plugin. It permit to fill pdf form.
Add these lines to your Gemfile:
gem 'fill_pdf', git: "https://github.com/CapSens/fill-pdf"
The following configuration items are available through the config object in application.rb:
class Application < Rails::Application # ... # ... # This is required to generate document and save it. config.fill_pdf.output_path = '' # This is required. It is a pdftk library path. Linux('/usr/bin/pdftk'), OSX('/usr/local/pdftk') if RUBY_PLATFORM.include? "darwin" config.fill_pdf.pdftk_path = '/usr/local/bin/pdftk' else config.fill_pdf.pdftk_path = '/usr/bin/pdftk' end end
-
All Hash keys must be string
-
Template is a path of your pdf file
-
Describe FillPdf initializer
def initialize(template, dictionary={}) @attributes = {} @template = template @dictionary = dictionary @pdftk = PdfForms.new(Rails.application.config.fill_pdf.pdftk_path) end # Template is a path of a template file. # dictionary is a hash contains pdf fields values. This hash keys should be pdf fields names.
-
Dictionary example
def action dictionary = { name: 'Armand Niampa', address: '80 rue des haies, 75020 Paris, France', created_at: '20/01/2015/', payed_at: '20/01/2015/', monthly_payment: '200 €', amount: '2000 €', deduction_amount: '300 €' } # dictionary key represent template field # value represent field value #... #... end
-
Use plugin to generate and download document
def action dictionary = { name: 'Armand Niampa', address: '80 rue des haies, 75020 Paris, France', created_at: '20/01/2015/', payed_at: '20/01/2015/', monthly_payment: '200 €', amount: '2000 €', deduction_amount: '300 €' } begin template = Rails.root.join('public', 'pdf_templates', 'pdf_file.pdf') document = FillPdf::Fill.new(template, dictionary) send_file document.export, filename: "name of your new document.pdf", type: 'application/pdf' rescue Exception => exception logger.warn exception end end