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

Not able to add content on previous pages in v0.6.x #225

Closed
tuance opened this issue Apr 18, 2014 · 6 comments · Fixed by #302
Closed

Not able to add content on previous pages in v0.6.x #225

tuance opened this issue Apr 18, 2014 · 6 comments · Fixed by #302

Comments

@tuance
Copy link

tuance commented Apr 18, 2014

In the previous versions, before changing to node streams, I was able to go back to previous pages to add elements. These additional elements are calculated as the pages were being produced, allowing the automated pagination to kick in. Then after all pages were generated, those elements would be added. I also used this mechanism to add footer information (like page x out of y). How can this be done in the newest version?

@MathieuLoutre
Copy link

I've been looking for something like that too. Either that or an event emitted when a new page is created so you can add what you need on creation. However I think an array of pages and a way to target a specific page when doing modifications would work really nicely.

@devongovett
Copy link
Member

I will link to this discussion which has already been going on about this topic.

That array of pages used to exist, but was removed for performance reasons in v0.5. Basically, it requires buffering all of the content in memory until the end of the document, and for large documents this took up a huge amount of memory. I have seen node processes crash with a couple hundred pages of images because the v8 heap overflowed.

It may be possible to add an option, not turned on by default, to bring back this ability. For smallish documents it should work. Also, v0.5+ now also writes image contents to the document right away rather than storing them in memory (images are not part of the main page content stream), so it should be less of an issue now.

@MathieuLoutre
Copy link

Thanks Devon! Perfectly understandable. On option or callback would be great.
Thanks for the great work on this library. It's really working well.

@MathieuLoutre
Copy link

Would you be interested by a PR? The project I'm working on needs this and it would be a pleasure to dive in the code and try to make it happen. I could go back to 0.4.x but I feel I would be missing out...

@theolternative
Copy link

I see it's been a while and no PR has been merged.
For anyone who needs this kind of feature, I've managed to add customized watermarks, headers and footers in new pages by adding a few lines at the end of the addPage method (document.coffescript). Basically before returning the new PDFDocument object it checks if an option "didAddPageCallback" has been passed. If it is:

  • Takes care of saving the current state (_fontSize, _ fillColor, x, y and _wrapper)
  • Calls the callback
  • Restore the previous state

So far it has been working without issues.
I've directly modified the JS: If anyone is interested I can share the code.
The library is really great!

@ef4
Copy link
Contributor

ef4 commented Sep 11, 2014

People watching this bug may want to check out the above PR I just submitted.

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

Successfully merging a pull request may close this issue.

5 participants