Skip to content

Commit

Permalink
fix Name must start with a leading slash
Browse files Browse the repository at this point in the history
Refs #312
  • Loading branch information
rkusa committed Sep 5, 2023
1 parent a0040ac commit b6cdd70
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.pdf binary
*.pdf binary
*.pdf diff
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fixed `Name must start with a leading slash` when adding an external document to a `pdfjs` document, and adding that result to another `pdfjs` document again (caused by a `null` object reference due to object streams). Also fixes duplicate objects when adding external documents with
pages in object streams.

## [2.5.2]
### Fixed
Expand Down
9 changes: 6 additions & 3 deletions lib/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ module.exports = class ExternalDocument {
const kids = this.pages.get("Kids");
const filter = page ? (i) => i === page - 1 : undefined;

// As a first iteration, simply register all page objects to ensure they exist for inter-page
// references (as they are not part of the `addObjectsRecursive` in the second iteration)
for (const page of this._iterPagesRecursively(doc, kids, filter)) {
doc._registerObject(page, true);
}

for (const page of this._iterPagesRecursively(doc, kids, filter)) {
// if the page object does not define its MediaBox, explicitly set its MediaBox to the
// value defined by its parent Pages object
if (!page.properties.has("MediaBox") && this.mediaBox) {
page.properties.set("MediaBox", this.mediaBox);
}

// add single page
doc._registerObject(page, true);

// first, register objects to assign IDs (for references)
const objects = [];
Parser.addObjectsRecursive(objects, page, 0);
Expand Down
3 changes: 3 additions & 0 deletions lib/object/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class PDFReference {
}

toString() {
if (this.object.id === null) {
throw new TypeError("Tried to write reference with `null` object id");
}
return this.object.id + " " + this.object.rev + " R";
}

Expand Down
7 changes: 7 additions & 0 deletions lib/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class Parser {
if (objects.indexOf(value.object) > -1) {
break;
}

// skip references to other pages
const type = value.object.properties.get("Type");
if (type && type.toString() === "/Page") {
break;
}

objects.push(value.object);
Parser.addObjectsRecursive(objects, value.object);
break;
Expand Down
Binary file modified test/pdfs/external/addallpages.pdf
Binary file not shown.
Binary file modified test/pdfs/external/addinbetween.pdf
Binary file not shown.
Binary file modified test/pdfs/external/self.pdf
Binary file not shown.
Binary file modified test/pdfs/issue/issue-112.pdf
Binary file not shown.
Binary file modified test/pdfs/issue/issue-117-nested-pages.pdf
Binary file not shown.

0 comments on commit b6cdd70

Please sign in to comment.