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

3

8

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

Impolitic answered 2/2, 2011 at 1:25 Comment(2)
+1 for the creative question asking, even if it's a bit over the top.Pooch
A bit? That's like... 640kb over the top, which should be enough for anybody.Pectoral
P
2

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.

Pectoral answered 2/2, 2011 at 1:38 Comment(3)
+1 Agreed. PDF's are for not for content generation or editing they are for layout control. Just open one up in a raw text editor and you'll see what Mark means. They are full of gobbledygook and other such gibberish.Pooch
The main thing is getting the text in a predetermined location on each page - like a footer(empty spot). {galaxy} doesn't even have to be present. So no problems drawing the box and placing the text. I'm just wondering if this is possible with an existing PDF. And if so how to accomplish this task with Prawn PDF. @$!#$ I hear Imperial Storm Troopers...this location has been compromised.Impolitic
You gave it your best shot! - May the force be with you.Impolitic
B
3

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

Betatron answered 2/2, 2011 at 12:14 Comment(2)
Anything in Ruby that can make this happen? Thoughts?Impolitic
So you're looking to parse a PDF document and then regenerate it, replacing certain portions of it?Tinishatinker
P
2

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.

Pectoral answered 2/2, 2011 at 1:38 Comment(3)
+1 Agreed. PDF's are for not for content generation or editing they are for layout control. Just open one up in a raw text editor and you'll see what Mark means. They are full of gobbledygook and other such gibberish.Pooch
The main thing is getting the text in a predetermined location on each page - like a footer(empty spot). {galaxy} doesn't even have to be present. So no problems drawing the box and placing the text. I'm just wondering if this is possible with an existing PDF. And if so how to accomplish this task with Prawn PDF. @$!#$ I hear Imperial Storm Troopers...this location has been compromised.Impolitic
You gave it your best shot! - May the force be with you.Impolitic
T
0

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 .

Tacheometer answered 31/7, 2014 at 16:6 Comment(1)
site seems down. do you know the new location?Bradytelic

© 2022 - 2024 — McMap. All rights reserved.