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

restore original graphical state after template #287

Merged
merged 2 commits into from Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/external.js
Expand Up @@ -2,6 +2,7 @@

const PDF = require('./object')
const Parser = require('./parser/parser')
const ops = require('./ops')

const PROPERTIES_TO_COPY = {
colorSpaces: 'ColorSpace',
Expand Down Expand Up @@ -106,6 +107,16 @@ module.exports = class ExternalDocument {
throw new TypeError('External document is invalid')
}

// Create content chunks used to save and restore the graphical state so a template cannot mess
// with the page's own graphical state.
await doc._startContentObject()
await doc._write(ops.q())
await doc._endContentObject()
await doc._startContentObject()
await doc._write(ops.Q())
await doc._endContentObject()
const [storeGraphicalState, restoreGraphicalState] = doc._contents.splice(doc._contents - 3, 2)

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
Expand All @@ -130,10 +141,15 @@ module.exports = class ExternalDocument {
if (!Array.isArray(contents)) {
contents = [contents]
}
contents = [
storeGraphicalState._object.toReference(),
...contents,
restoreGraphicalState._object.toReference()
]

let resources = first.get('Resources')
if (resources instanceof PDF.Reference) {
resources = resources.object.properties
resources = resources.object.properties
}

doc._template = {
Expand Down
Binary file modified test/pdfs/external/collision-prevention.pdf
Binary file not shown.
Binary file modified test/pdfs/external/template.pdf
Binary file not shown.
Binary file modified test/pdfs/external/templatefirstpageonly.pdf
Binary file not shown.
Binary file modified test/pdfs/issue/issue-150-nested-pages-in-templates.pdf
Binary file not shown.