How to replace a word in an existing PDF using Rub

2019-06-20 02:00发布

问题:

-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.

回答1:

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.



回答2:

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



回答3:

Late and around, but in Ruby you can call Applidok PDF merge, which allow to write user data (e.g. from a Web form) on any existing PDF. See Ruby doc: http://doc.applidok.com/merge-ruby.html .



标签: ruby pdf prawn