diff --git a/CHANGELOG.md b/CHANGELOG.md index f37bbae9..408e8d71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/docs/getting_started.md b/docs/getting_started.md index 69ba3acd..f0d2a5bb 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -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 diff --git a/lib/line_wrapper.js b/lib/line_wrapper.js index b1e05a3e..07610d12 100644 --- a/lib/line_wrapper.js +++ b/lib/line_wrapper.js @@ -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();