Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to change some text added by choosing a template? #458

Closed
segfault88 opened this issue Mar 7, 2013 · 2 comments
Closed

Comments

@segfault88
Copy link

Hi,

The Prawn template feature is really awesome, however - I am wondering if it is possible to change some text added by using a template. I'm happy to do some of the work if anybody could point me in the right direction...

Looking at the code, when initializing the document - if there is a template passed it calls fresh_content_streams in internals.rb, which has this comment:

# adds a new, empty content stream to each page. Used in templating so
# that imported content streams can be left pristine

Can I get to the imported content streams, and change them (then write a new document)? Appreciate any pointers.

Just to be clear, I am making a new document with a template PDF. In this PDF it has the text "foo", I want to replace it (in the same place, font etc.) with "bar" then save a new PDF.

@bradediger
Copy link
Member

Sadly, this is not possible to do robustly with Prawn, because the PDF specification operates at a lower level than the level you want to work at.

A bit of a technical background: PDF text is drawn by the use of character codes, which identify glyphs but not necessarily Unicode characters. There is a provision in the PDF spec for fonts to include a mapping from character IDs to Unicode, but it is optional. So even the problem of identifying "this page contains the text foo" is nontrivial at best and impossible to accomplish for arbitrary compliant PDFs. The rest of the process (replacing text) has similar technical difficulties if we would want to create a completely compliant solution.

So I think the best solution for you is going to depend on where these template PDFs are going to come from. If they are being generated by Prawn, you may be able to defer the processing until you have the data you need. If they are coming from somewhere else, you'll probably have to measure distances and draw text on top of the template. It's a semi-manual process, but it has worked for me several times in the past. Hope this helps!

@segfault88
Copy link
Author

Yes, that helps - thanks a lot for the information! A shame this isn't possible, but oh well - I can deal with it another way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants