我使用 PDFJS 来显示 PDF。大多数错误都被 viewer.js 的这个片段(来自 PDFJS 包的文件)正确捕获:
PDFJS.getDocument(parameters, pdfDataRangeTransport, passwordNeeded).then(
function getDocumentCallback(pdfDocument) {
self.load(pdfDocument, scale);
self.loading = false;
},
function getDocumentError(message, exception) {
但是当 PDF 的长度为 0 时,会出现错误:
Error: stream must have data
util.js (wiersz 62)
assertWellFormed@http://www.polishwords.com.pl/dev/pdfjs/src/util.js:124:5
init@http://www.polishwords.com.pl/dev/pdfjs/src/core.js:296:5
PDFDocument@http://www.polishwords.com.pl/dev/pdfjs/src/core.js:288:7
LocalPdfManager@http://www.polishwords.com.pl/dev/pdfjs/src/pdf_manager.js:75:21
onDone@http://www.polishwords.com.pl/dev/pdfjs/src/worker.js:212:24
NetworkManager_onStateChange@http://www.polishwords.com.pl/dev/pdfjs/src/network.js:190:1
util.js (wiersz 64)
Error: stream must have data
throw new Error(msg);
在 util.js 中抛出错误的代码片段如下所示:
// Fatal errors that should trigger the fallback UI and halt execution by
// throwing an exception.
function error(msg) {
// If multiple arguments were passed, pass them all to the log function.
if (arguments.length > 1) {
var logArguments = ['Error:'];
logArguments.push.apply(logArguments, arguments);
log.apply(null, logArguments);
// Join the arguments into a single string for the lines below.
msg = [].join.call(arguments, ' ');
} else {
log('Error: ' + msg);
}
log(backtrace());
PDFJS.LogManager.notify('error', msg);
throw new Error(msg);
}
所以我的问题是,如何在 viewer.js PDFView 类中正确处理 PDF JS 中的 0 文件大小 PDF 错误?