Merge pull request #11398 from Snuffleupagus/issue-5887
Attempt to improve the `PDFDocument` error message for empty files (issue 5887)
This commit is contained in:
commit
af4ba75f68
@ -15,9 +15,9 @@
|
|||||||
/* eslint no-var: error */
|
/* eslint no-var: error */
|
||||||
|
|
||||||
import {
|
import {
|
||||||
assert, bytesToString, FormatError, info, isArrayBuffer, isArrayEqual, isBool,
|
assert, bytesToString, FormatError, info, InvalidPDFException, isArrayBuffer,
|
||||||
isNum, isSpace, isString, OPS, shadow, stringToBytes, stringToPDFString, Util,
|
isArrayEqual, isBool, isNum, isSpace, isString, OPS, shadow, stringToBytes,
|
||||||
warn
|
stringToPDFString, Util, warn
|
||||||
} from '../shared/util';
|
} from '../shared/util';
|
||||||
import { Catalog, ObjectLoader, XRef } from './obj';
|
import { Catalog, ObjectLoader, XRef } from './obj';
|
||||||
import { Dict, isDict, isName, isStream, Ref } from './primitives';
|
import { Dict, isDict, isName, isStream, Ref } from './primitives';
|
||||||
@ -375,7 +375,8 @@ class PDFDocument {
|
|||||||
throw new Error('PDFDocument: Unknown argument type');
|
throw new Error('PDFDocument: Unknown argument type');
|
||||||
}
|
}
|
||||||
if (stream.length <= 0) {
|
if (stream.length <= 0) {
|
||||||
throw new Error('PDFDocument: Stream must have data');
|
throw new InvalidPDFException(
|
||||||
|
'The PDF file is empty, i.e. its size is zero bytes.');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.pdfManager = pdfManager;
|
this.pdfManager = pdfManager;
|
||||||
|
@ -1519,7 +1519,7 @@ var XRef = (function XRefClosure() {
|
|||||||
return trailerDict;
|
return trailerDict;
|
||||||
}
|
}
|
||||||
// nothing helps
|
// nothing helps
|
||||||
throw new InvalidPDFException('Invalid PDF structure');
|
throw new InvalidPDFException('Invalid PDF structure.');
|
||||||
},
|
},
|
||||||
|
|
||||||
readXRef: function XRef_readXRef(recoveryMode) {
|
readXRef: function XRef_readXRef(recoveryMode) {
|
||||||
|
@ -148,8 +148,10 @@ describe('api', function() {
|
|||||||
var loadingTask = getDocument(buildGetDocumentParams('bug1020226.pdf'));
|
var loadingTask = getDocument(buildGetDocumentParams('bug1020226.pdf'));
|
||||||
loadingTask.promise.then(function () {
|
loadingTask.promise.then(function () {
|
||||||
done.fail('shall fail loading');
|
done.fail('shall fail loading');
|
||||||
}).catch(function (error) {
|
}).catch(function(reason) {
|
||||||
expect(error instanceof InvalidPDFException).toEqual(true);
|
expect(reason instanceof InvalidPDFException).toEqual(true);
|
||||||
|
expect(reason.message).toEqual('Invalid PDF structure.');
|
||||||
|
|
||||||
loadingTask.destroy().then(done);
|
loadingTask.destroy().then(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -292,6 +294,20 @@ describe('api', function() {
|
|||||||
done();
|
done();
|
||||||
}).catch(done.fail);
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('creates pdf doc from empty typed array', function(done) {
|
||||||
|
const loadingTask = getDocument(new Uint8Array(0));
|
||||||
|
|
||||||
|
loadingTask.promise.then(function() {
|
||||||
|
done.fail('shall not open empty file');
|
||||||
|
}, function(reason) {
|
||||||
|
expect(reason instanceof InvalidPDFException);
|
||||||
|
expect(reason.message).toEqual(
|
||||||
|
'The PDF file is empty, i.e. its size is zero bytes.');
|
||||||
|
|
||||||
|
loadingTask.destroy().then(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('PDFWorker', function() {
|
describe('PDFWorker', function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user