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

Optional page buffering #302

Merged
merged 1 commit into from
Sep 12, 2014
Merged

Optional page buffering #302

merged 1 commit into from
Sep 12, 2014

Conversation

ef4
Copy link
Contributor

@ef4 ef4 commented Sep 11, 2014

This closes #225 by adding an optional page buffer. The API works as follows:

  1. Pass the option bufferPages: true when creating your document.
  2. Call doc.bufferedPageCount() at any time to ask how many pages are buffered.
  3. Call doc.switchToPage(n) to make any of the buffered pages the current page again. This lets you add additional content.
  4. Call doc.flushPages() to flush the page buffer out to the underlying stream. This is optional, it also happens automatically at end().

@tuance
Copy link

tuance commented Sep 11, 2014

This is the perfect solution to what I need to allow me to upgrade to the latest version and keeping the functionality of post page rendering operations. Thanks!

@MathieuLoutre
Copy link

This looks really nice, thank you!

@devongovett
Copy link
Member

I like this solution, thanks for the PR. Just a few minor changes, and I think we can merge this.

I think instead of creating @_pageBuffer only when in bufferPages mode, we should always create it, and then just call flushPages internally when a new page is added if NOT in bufferPages mode. I think the code will be simpler this way. That way, flushPages will be used both internally and externally. I'll put some line comments to clarify what I mean. Does this make sense to you?

@@ -19,6 +19,9 @@ class PDFDocument extends stream.Readable
# Whether streams should be compressed
@compress = @options.compress ? yes

if @options.bufferPages
@_pageBuffer = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always create this array, regardless of @options.bufferPages.

@ef4
Copy link
Contributor Author

ef4 commented Sep 12, 2014

Sure, I like the proposed edits. I'll push an update.

@devongovett
Copy link
Member

One more thing that might be confusing for some people is that switchToPage takes indexes in the buffer, not indexes in the document. So, switchToPage(0) doesn't switch to the first page in the document, it switches to the first page in the buffer.

Maybe we should keep a @_pageBufferStart variable that tracks the first index in the @_pageBuffer so that calling switchToPage(0) throws an error if page 0 in the document had already been flushed. What do you think?

@ef4
Copy link
Contributor Author

ef4 commented Sep 12, 2014

If we did change the meaning of switchToPage like that, we would need to change bufferedPageCount to bufferedPageRange so people know what they can iterate over.

But I think the dominant use case will be buffering all the pages until the end, and that case wouldn't benefit from this added complexity. So I prefer the API as it is.

@devongovett
Copy link
Member

Yeah I agree about the dominant use case, but it still seems strange to me. Right now, if I call switchToPage(0) when not in bufferPages mode, it does nothing since it switches back to the current page.

I think, if we keep it how it is, the method should be renamed to something like switchToBufferedPage to be more clear. Otherwise, we switch it to global page numbers and rename the other method to bufferedPageRange as you point out.

The complexity of the second solution is not that much, just one additional variable that would get incremented by @_pageBuffer.length when flushPages is called. What do you think?

@ef4
Copy link
Contributor Author

ef4 commented Sep 12, 2014

Yeah, that sounds ok. Would you prefer

bufferedPageRange: -> {start: @_pageBufferStart, count: @_pageBuffer.length}

vs

bufferedPageCount: -> @_pageBuffer.length
bufferedPageStart: -> @_pageBufferStart

@devongovett
Copy link
Member

The first one. I think just one method is better.

@ef4
Copy link
Contributor Author

ef4 commented Sep 12, 2014

Oops, I just realized I pushed the wrong version, hang on...

@ef4
Copy link
Contributor Author

ef4 commented Sep 12, 2014

Ok, all suggestions incorporated.

devongovett added a commit that referenced this pull request Sep 12, 2014
@devongovett devongovett merged commit 3842f6b into foliojs:master Sep 12, 2014
@devongovett
Copy link
Member

Great, thanks so much for the PR! I know a lot of people have been itching for this feature for a few months, so glad to incorporate something for them.

@ef4
Copy link
Contributor Author

ef4 commented Sep 12, 2014

Sweet, glad to help. Thanks for fast merges.

@MathieuLoutre
Copy link

Awesome that this got merged so fast. Really looking forward to using this now!

On Fri, Sep 12, 2014 at 8:06 PM, Devon Govett [email protected]
wrote:

Great, thanks so much for the PR! I know a lot of people have been itching for this feature for a few months, so glad to incorporate something for them.

Reply to this email directly or view it on GitHub:
#302 (comment)

@ef4 ef4 deleted the buffered-pages branch September 12, 2014 19:11
@devongovett
Copy link
Member

Just released v0.7.0 with this feature and lots of other fixes. Check it out! You can find docs on this particular feature, on the website. Thanks again, @ef4!

@ef4
Copy link
Contributor Author

ef4 commented Sep 15, 2014

😄

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 this pull request may close these issues.

Not able to add content on previous pages in v0.6.x
4 participants