pdfjs
Advanced tools
Comparing version 2.3.1 to 2.3.2
@@ -7,2 +7,6 @@ # Changelog | ||
## [2.3.2] - 2019-10-09 | ||
### Fixed | ||
- Fix using pages documents with nested /Pages objects as templates #150 | ||
## [2.3.1] - 2019-09-11 | ||
@@ -9,0 +13,0 @@ ### Fixed |
@@ -42,6 +42,36 @@ 'use strict' | ||
await this._addPagesRecursively(doc, kids, 0, filter) | ||
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) | ||
for (const obj of objects) { | ||
doc._registerObject(obj, true) | ||
} | ||
// write objects | ||
for (const obj of objects) { | ||
await doc._writeObject(obj) | ||
} | ||
page.prop('Parent', doc._pagesObj.toReference()) | ||
await doc._writeObject(page) | ||
doc._pages.push(page.toReference()) | ||
} | ||
} | ||
async _addPagesRecursively(doc, kids, i, filter) { | ||
* _iterPagesRecursively(doc, kids, filter, cursor) { | ||
if (!cursor) { | ||
cursor = { i: 0 } | ||
} | ||
for (const kid of kids) { | ||
@@ -52,2 +82,93 @@ const page = kid.object | ||
// encountered nested pages | ||
yield* this._iterPagesRecursively(doc, page.properties.get('Kids'), filter, cursor) | ||
continue | ||
} else if (!filter || filter(cursor.i)) { | ||
yield page | ||
} | ||
cursor.i++ | ||
} | ||
} | ||
async setAsTemplate(doc, firstPageOnly) { | ||
await doc._endPage() | ||
// take the first page only | ||
const filter = i => i === 0 | ||
const kids = this.pages.get('Kids') | ||
if (!kids[0]) { | ||
throw new TypeError('External document is invalid') | ||
} | ||
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) | ||
} | ||
const first = page.properties | ||
const objects = [] | ||
Parser.addObjectsRecursive(objects, page, 0) | ||
// first, register objects to assign IDs (for references) | ||
for (const obj of objects) { | ||
doc._registerObject(obj, true) | ||
} | ||
// write objects | ||
for (const obj of objects) { | ||
await doc._writeObject(obj) | ||
} | ||
let contents = first.get('Contents') | ||
if (!Array.isArray(contents)) { | ||
contents = [contents] | ||
} | ||
let resources = first.get('Resources') | ||
if (resources instanceof PDF.Reference) { | ||
resources = resources.object.properties | ||
} | ||
doc._template = { | ||
contents: contents.map(c => c.toString()), | ||
colorSpaces: {}, | ||
fonts: {}, | ||
xobjects: {}, | ||
} | ||
const colorSpaces = resources.get('ColorSpace') | ||
if (colorSpaces) { | ||
for (const alias in colorSpaces.dictionary) { | ||
doc._template.colorSpaces[alias] = colorSpaces.dictionary[alias].toString() | ||
doc._aliases.block(alias) | ||
} | ||
} | ||
const fonts = resources.get('Font') | ||
if (fonts) { | ||
for (const alias in fonts.dictionary) { | ||
doc._template.fonts[alias] = fonts.dictionary[alias].toString() | ||
doc._aliases.block(alias) | ||
} | ||
} | ||
const xobjects = resources.get('XObject') | ||
if (xobjects) { | ||
for (const alias in xobjects.dictionary) { | ||
doc._template.xobjects[alias] = xobjects.dictionary[alias].toString() | ||
doc._aliases.block(alias) | ||
} | ||
} | ||
doc._template.firstPageOnly = firstPageOnly | ||
} | ||
} | ||
async _getPagesRecursively(doc, kids, i, filter) { | ||
for (const kid of kids) { | ||
const page = kid.object | ||
if (page.properties.get('Type').toString() === '/Pages') { | ||
// encountered nested pages | ||
i = await this._addPagesRecursively(doc, page.properties.get('Kids'), i, filter) | ||
@@ -88,75 +209,4 @@ continue | ||
} | ||
async setAsTemplate(doc, firstPageOnly) { | ||
await doc._endPage() | ||
const kids = this.pages.get('Kids') | ||
if (!kids[0]) { | ||
throw new TypeError('External document is invalid') | ||
} | ||
const page = kids[0].object | ||
// 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) | ||
} | ||
const first = page.properties | ||
const objects = [] | ||
Parser.addObjectsRecursive(objects, page, 0) | ||
// first, register objects to assign IDs (for references) | ||
for (const obj of objects) { | ||
doc._registerObject(obj, true) | ||
} | ||
// write objects | ||
for (const obj of objects) { | ||
await doc._writeObject(obj) | ||
} | ||
let contents = first.get('Contents') | ||
if (!Array.isArray(contents)) { | ||
contents = [contents] | ||
} | ||
let resources = first.get('Resources') | ||
if (resources instanceof PDF.Reference) { | ||
resources = resources.object.properties | ||
} | ||
doc._template = { | ||
contents: contents.map(c => c.toString()), | ||
colorSpaces: {}, | ||
fonts: {}, | ||
xobjects: {}, | ||
} | ||
const colorSpaces = resources.get('ColorSpace') | ||
if (colorSpaces) { | ||
for (const alias in colorSpaces.dictionary) { | ||
doc._template.colorSpaces[alias] = colorSpaces.dictionary[alias].toString() | ||
doc._aliases.block(alias) | ||
} | ||
} | ||
const fonts = resources.get('Font') | ||
if (fonts) { | ||
for (const alias in fonts.dictionary) { | ||
doc._template.fonts[alias] = fonts.dictionary[alias].toString() | ||
doc._aliases.block(alias) | ||
} | ||
} | ||
const xobjects = resources.get('XObject') | ||
if (xobjects) { | ||
for (const alias in xobjects.dictionary) { | ||
doc._template.xobjects[alias] = xobjects.dictionary[alias].toString() | ||
doc._aliases.block(alias) | ||
} | ||
} | ||
doc._template.firstPageOnly = firstPageOnly | ||
} | ||
} | ||
{ | ||
"name": "pdfjs", | ||
"author": "Markus Ast <npm.m@rkusa.st>", | ||
"version": "2.3.1", | ||
"version": "2.3.2", | ||
"description": "A Portable Document Format (PDF) generation library targeting both the server- and client-side.", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
9821023
6518