Skip to content

Commit

Permalink
fix: issue #373: handle bad encoding expcetion by start page renderin…
Browse files Browse the repository at this point in the history
…g after page operator list is resolved, add i373_err_broken.pdf to verify (#378)
  • Loading branch information
modesty authored Dec 28, 2024
1 parent 6744445 commit 7721899
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 40 deletions.
44 changes: 19 additions & 25 deletions base/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ var Page = (function PageClosure() {
return promise;
},
getOperatorList: function Page_getOperatorList(handler) {
var self = this;
var promise = new Promise();

function reject(e) {
Expand All @@ -148,8 +147,7 @@ var Page = (function PageClosure() {
var pageListPromise = new Promise();

var pdfManager = this.pdfManager;
var contentStreamPromise = pdfManager.ensure(this, 'getContentStream',
[]);
var contentStreamPromise = pdfManager.ensure(this, 'getContentStream', []);
var resourcesPromise = this.loadResources([
'ExtGState',
'ColorSpace',
Expand All @@ -166,35 +164,31 @@ var Page = (function PageClosure() {
this.pageIndex, 'p' + this.pageIndex + '_',
this.idCounters, this.fontCache);

var dataPromises = Promise.all(
[contentStreamPromise, resourcesPromise], reject);
dataPromises.then(function(data) {
Promise.all([contentStreamPromise, resourcesPromise]).then(function(data) {
var contentStream = data[0];


var opList = new OperatorList(handler, self.pageIndex);
try {
handler.send('StartRenderPage', {
transparency: partialEvaluator.hasBlendModes(self.resources),
pageIndex: self.pageIndex
});
partialEvaluator.getOperatorList(contentStream, self.resources, opList);
pageListPromise.resolve(opList);
}
catch(e) {
pageListPromise.reject(e);
}
});
try {
var opList = new OperatorList(handler, this.pageIndex);
partialEvaluator.getOperatorList(contentStream, this.resources, opList);
pageListPromise.resolve(opList);

handler.send('StartRenderPage', {
transparency: partialEvaluator.hasBlendModes(this.resources),
pageIndex: this.pageIndex
});
} catch (ex) {
reject(ex);
}
}.bind(this), reject);

var annotationsPromise = pdfManager.ensure(this, 'annotations');
Promise.all([pageListPromise, annotationsPromise]).then(function(datas) {
var pageOpList = datas[0];
var annotations = datas[1];
Promise.all([pageListPromise, annotationsPromise]).then(function(data) {
var pageOpList = data[0];
var annotations = data[1];

if (annotations.length === 0) {
pageOpList.flush(true);
promise.resolve(pageOpList);
return;
return promise;
}

var annotationsReadyPromise = Annotation.appendToOperatorList(
Expand Down
21 changes: 10 additions & 11 deletions base/display/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,18 @@ var Metadata = (PDFJS.Metadata = (function MetadataClosure() {
}

function Metadata(meta) {
if (typeof meta === 'string') {
this.metadata = {};
if (typeof meta === 'string') {
// Ghostscript produces invalid metadata
meta = fixMetadata(meta);

var parser = new DOMParser();
meta = parser.parseFromString(meta, 'application/xml');
} else if (!(meta instanceof Document)) {
error('Metadata: Invalid metadata object');
}

this.metaDocument = meta;
this.metadata = {};
this.parse();
meta = meta.trim();
if (meta.startsWith('<') && meta.endsWith('>')) {
var parser = new DOMParser();
meta = parser.parseFromString(meta, 'application/xml');
this.metaDocument = meta;
this.parse();
}
}
}

Metadata.prototype = {
Expand Down
4 changes: 2 additions & 2 deletions base/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ function error(msg) {
} else {
//log('Error: ' + msg);
}
//log(backtrace());
//PDFJS.LogManager.notify('error', msg);
// log(backtrace());
PDFJS.LogManager.notify('error', msg);
throw new Error(msg);
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"test:jest": "jest --config ./jest.config.json",
"test": "jest --config ./jest.config.json && npm run parse-r && npm run parse-fd",
"test:forms": "cd ./test && sh p2j.forms.sh",
"test:misc": "cd ./test && sh p2j.one.sh misc . \"Expected: 13 success, 4 fail exception with stack trace\" ",
"test:misc": "cd ./test && sh p2j.one.sh misc . \"Expected: 13 success, 5 fail exception with stack trace\" ",
"parse": "./bin/pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form",
"parse-s": "./bin/pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s",
"parse-t": "./bin/pdf2json.js -f ./test/pdf/fd/form/F1040.pdf -o ./test/target/fd/form -s -t",
Expand All @@ -48,7 +48,7 @@
"parse-tb": "./bin/pdf2json.js -f ./test/pdf/misc/i242_testingWithTable.pdf -o ./test/target/misc",
"parse-tc": "./bin/pdf2json.js -f ./test/pdf/misc/i293_pdfpac.pdf -o ./test/target/misc",
"parse-rectFix": "./bin/pdf2json.js -f ./test/pdf/misc/pr298_rect_fix_from_upstream.pdf -o ./test/target/misc",
"parse-e": "./bin/pdf2json.js -f ./test/pdf/misc/i43_encrypted.pdf -o ./test/target/misc",
"parse-e": "./bin/pdf2json.js -f ./test/pdf/misc/i373_err_broken.pdf -o ./test/target/misc",
"build:rollup": "rollup -c ./rollup.config.js",
"build:bundle-pdfjs-base": "node rollup/bundle-pdfjs-base.js",
"build": "npm run build:bundle-pdfjs-base && npm run build:rollup",
Expand Down
Binary file added test/pdf/misc/i373_err_broken.pdf
Binary file not shown.

0 comments on commit 7721899

Please sign in to comment.