How to edit or write on existing PDF with Ruby?

Since Prawn has removed the template feature (it was full of bugs) the easiest way I've found is the following:

  1. Use Prawn to generate a PDF with ONLY the dynamic parts you want to add.
  2. Use PDF::Toolkit (which wraps PDFtk) to combine the Prawn PDF with the original.

Rough Example:

require 'prawn'
require 'pdf/toolkit'

template_filename = 'some/dir/Awesome-Graphics.pdf'
prawn_filename = 'temp.pdf'
output_filename = 'output.pdf'

Prawn::Document.generate(prawn_filename) do
  # Generate whatever you want here.
  text_box "This is some new text!", :at => [100, 300]
end

PDF::Toolkit.pdftk(prawn_filename, "background", template_filename, "output", output_filename)

you have to definitely check out Prawn gem, by which you can generate any custom pdf files. You can actually use prawn to write in text into existing pdfs by treating the existing PDF as a template for your new Prawn document.

For example:

filename = "#{Prawn::DATADIR}/pdfs/multipage_template.pdf"
Prawn::Document.generate("full_template.pdf", :template => filename) do
  text "THis content is written on the first page of the template", :align => :center
end

This will write text onto the first page of the old pdf.

See more here: http://prawn.majesticseacreature.com/manual.pdf