Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit or write on existing PDF with Ruby?

I have a couple of PDF template files with complex content and several blank regions/areas in them. I need to be able to write text in those blank regions and save the resulting PDFs in a folder.

I googled for answers on this question quite intensively, but I didn't find definite answers. One of the better solutions is PDF::Toolkit, but it would require the purchase of Adobe Acrobat to add replaceable attributes to existing PDF documents.

The PHP world is blessed with FPDI that can be used to simply open a PDF file and write/draw on it over the existing content. There is a Ruby port of this library, but the last commit for it happened at the beginning of 2009. Also that project doesn't look like it is widely used and supported.

The question is: What is the better Ruby way of editing, writing or drawing on existing PDFs?

This question also doesn't seem to be answered on here. These questions are related, but not really the same:

  • Prawn gem: How to create the .pdf from an *existing* file (.xls)
  • watermark existing pdf with ruby
  • Ruby library for manipulating existing PDF
  • How to replace a word in an existing PDF using Ruby Prawn?
like image 757
Alex Kovshovik Avatar asked Feb 07 '12 23:02

Alex Kovshovik


People also ask

Can a pdf be editable?

You can edit the PDF in Adobe Acrobat or another PDF editing tool that allows you to access forms in a PDF and create new ones. Microsoft Word can also import PDF files and turn them into editable Word documents, although they may be slightly altered in the process.


2 Answers

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

like image 164
Said Kaldybaev Avatar answered Sep 19 '22 23:09

Said Kaldybaev


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) 
like image 26
Marc L Avatar answered Sep 21 '22 23:09

Marc L