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

Bugfix for flowing text always adding new page #1202

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
- Replace integration tests by visual regression tests
- Fix access permissions in PDF version 1.7ext3
- Fix Buffer() is deprecation warning
- Fix page always being added when flowing onto existing page
- Add `forms.md` to generate documentation files
- Fix "@" in FontName


### [v0.11.0] - 2019-12-03

- Fix infinite loop when an individual character is bigger than the width of the text.
Expand Down
3 changes: 3 additions & 0 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ pages are flushed to the output file yourself rather than letting PDFKit handle
it, just pass `bufferPages: true` as an option to the `PDFDocument` constructor. Then, you can call
`doc.switchToPage(pageNumber)` to switch to a previous page (page numbers start at 0).

When switching back to a previous page, text that flows off the end will run onto the next page
(if it already exists), otherwise a new page will be added at the end of the document.

When you're ready to flush the buffered pages to the output file, call `flushPages`.
This method is automatically called by `doc.end()`, so if you just want to buffer all pages in the document, you
never need to call it. Finally, there is a `bufferedPageRange` method, which returns the range
Expand Down
18 changes: 17 additions & 1 deletion lib/line_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,23 @@ class LineWrapper extends EventEmitter {
return false;
}

this.document.continueOnNewPage();
// If using a pagebuffer, check whether the page is the last one
if (this.document._pageBuffer){
var pageIndex = this.document._pageBuffer.indexOf(this.document.page);
// If the page is the last one in the buffer, add a new page.
if (pageIndex == this.document._pageBuffer.length - 1){
this.document.continueOnNewPage();
} else {
// The page isn't the last in the buffer, so jump to the next page
this.document.switchToPage(pageIndex + 1);
// Now position the cursor at the top margin
this.document.y = this.document.page.margins.top;
}
} else{
// No pagebuffer?
this.document.continueOnNewPage();
}

this.column = 1;
this.startY = this.document.page.margins.top;
this.maxY = this.document.page.maxY();
Expand Down