Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a word in an existing PDF using Ruby Prawn?

Tags:

ruby

pdf

prawn

-Hello Sandeep here. I am an apprentice to the Jedi Master Yoda. My master believes the force is strong in the Ruby community and has chosen me for a simple task to complete using the Ruby langauge:

I need to use the Prawn PDF gem to accomplish the task. I have an existing pdf document called Dooku.pdf - it contains sensitive information on the Evil Count Dooku's activities. In Dooku.pdf there is text called {galaxy}. {galaxy} is always in the same location on every page of the pdf document.

I need to open Dooku.pdf, replace each instance of {galaxy} with {planet} and then save/close Dooku.pdf. How do I accomplish this task using the Prawn PDF gem?

-May the force be with you.

like image 759
Sandeep Avatar asked Feb 02 '11 01:02

Sandeep


2 Answers

You can't. Prawn is for writing PDFs, not reading them.

like image 165
Jörg W Mittag Avatar answered Oct 20 '22 00:10

Jörg W Mittag


1) I'm a star wars fan and all, but damn.

2) Your "simple task" is not even remotely simple. It's not that hard to COVER the existing text... but REPLACING IT is another mater entirely. And forget reflowing existing text. All-but-impossible.

If covering it will suffice, then you need only draw a rectangle filled with the background color over your first string, then draw your second string on top of it. I'm not familiar with Prawn PDF, so I'll leave the implementation details as an Exercise For the Reader.

like image 25
Mark Storer Avatar answered Oct 20 '22 00:10

Mark Storer