From a4804e2841aa58478676a765b29667ec451eb587 Mon Sep 17 00:00:00 2001 From: rexxmagnus Date: Wed, 23 Dec 2020 08:49:49 +0000 Subject: [PATCH 1/4] Fix for excess page being added when flowing Allow text added on a page to flow onto the next page if it already exists, rather than always adding a new page below. --- lib/line_wrapper.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/line_wrapper.js b/lib/line_wrapper.js index a53f17a13..ee7cc9c7d 100644 --- a/lib/line_wrapper.js +++ b/lib/line_wrapper.js @@ -310,7 +310,23 @@ class LineWrapper extends EventEmitter { return false; } - this.document.addPage(); + // 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.addPage(); + } 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.addPage(); + } + this.column = 1; this.startY = this.document.page.margins.top; this.maxY = this.document.page.maxY(); From 664acc484dcfeb7096ad4fa4a6058be6713df536 Mon Sep 17 00:00:00 2001 From: rexxmagnus Date: Wed, 23 Dec 2020 09:03:38 +0000 Subject: [PATCH 2/4] Update CHANGELOG.md Update to include new bugfix for text flowing onto existing pages. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73b12e650..b33ce1ae6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - 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 ### [v0.11.0] - 2019-12-03 From ba9d5f3b58080faa0cc26dc46cee5090ab4f593a Mon Sep 17 00:00:00 2001 From: rexxmagnus Date: Wed, 23 Dec 2020 09:09:34 +0000 Subject: [PATCH 3/4] Update getting_started.md Added note about text flow when switching to previous pages. --- docs/getting_started.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/getting_started.md b/docs/getting_started.md index 69ba3acd8..f0d2a5bbf 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 From 6ca42cfe3155f22b807c2b8892e2d3f20943d336 Mon Sep 17 00:00:00 2001 From: rexxmagnus Date: Wed, 23 Dec 2020 09:19:51 +0000 Subject: [PATCH 4/4] Update line_wrapper.js Removed redundant double-negation. --- lib/line_wrapper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/line_wrapper.js b/lib/line_wrapper.js index ee7cc9c7d..4b9520d3e 100644 --- a/lib/line_wrapper.js +++ b/lib/line_wrapper.js @@ -311,7 +311,7 @@ class LineWrapper extends EventEmitter { } // If using a pagebuffer, check whether the page is the last one - if (!!this.document._pageBuffer){ + 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){