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

Add support for inserting pages #997

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
### Unreleased
- Fix infinite loop when text is positioned after page right margin

### [v0.11.0] - 2019-07-09 [Joost Lubach]

- Added support for inserting pages

### [v0.10.0] - 2019-06-06

- Fix links to pages within the document
Expand Down
47 changes: 47 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,53 @@ class PDFDocument extends stream.Readable {
return this;
}

insertPage(at, options) {
// end the current page if needed
if (options == null) {
({ options } = this);
}
if (!this.options.bufferPages) {
throw new Error("Cannot insert pages without option 'bufferPages'")
}

// create a page object
this.page = new PDFPage(this, options);
this._pageBuffer.splice(at, 0, this.page);

// add the page to the object store
const pages = this._root.data.Pages.data;
pages.Kids.splice(at, 0, this.page.dictionary);
pages.Count++;

// reset x and y coordinates
this.x = this.page.margins.left;
this.y = this.page.margins.top;

// flip PDF coordinate system so that the origin is in
// the top left rather than the bottom left
this._ctm = [1, 0, 0, 1, 0, 0];
this.transform(1, 0, 0, -1, 0, this.page.height);

this.emit('pageAdded');

return this;
}

addPageAfterThis(options) {
Copy link
Member

Choose a reason for hiding this comment

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

For now i would not add this method. Can be added later with a clear use case

if (!this.options.bufferPages) {
this.addPage(options);
return;
}

const index = this._pageBuffer.indexOf(this.page);
if (index === -1) {
this.addPage(options);
return;
}

this.insertPage(index + 1, options);
}

bufferedPageRange() {
return { start: this._pageBufferStart, count: this._pageBuffer.length };
}
Expand Down
2 changes: 1 addition & 1 deletion lib/line_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class LineWrapper extends EventEmitter {
return false;
}

this.document.addPage();
this.document.addPageAfterThis();
Copy link
Member

Choose a reason for hiding this comment

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

For safety this needs to be kept as addPage

this.column = 1;
this.startY = this.document.page.margins.top;
this.maxY = this.document.page.maxY();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"document",
"vector"
],
"version": "0.10.0",
"version": "0.11.0",
Copy link
Member

Choose a reason for hiding this comment

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

Do not update the version in package.json. It's done when a release is prepared

"homepage": "http://pdfkit.org/",
"author": {
"name": "Devon Govett",
Expand Down