Attempt to provide better default values for the disableFontFace/nativeImageDecoderSupport API options in Node.js

This should provide a better out-of-the-box experience when using PDF.js in a Node.js environment, since it's missing native support for both `@font-face` and `Image`.
Please note that this change *only* affects the default values, hence it's still possible for an API consumer to override those values when calling `getDocument`.

Also, prevents "ReferenceError: document is not defined" errors, when running the unit-tests in Node.js/Travis.
This commit is contained in:
Jonas Jenwald 2018-06-01 12:52:29 +02:00
parent dcc7f33ee7
commit 0ecc22cb04
4 changed files with 22 additions and 19 deletions

View File

@ -56,14 +56,8 @@ var pdfURL = '../../../web/compressed.tracemonkey-pldi-09.pdf';
// Read the PDF file into a typed array so PDF.js can load it. // Read the PDF file into a typed array so PDF.js can load it.
var rawData = new Uint8Array(fs.readFileSync(pdfURL)); var rawData = new Uint8Array(fs.readFileSync(pdfURL));
// Load the PDF file. The `disableFontFace` and `nativeImageDecoderSupport` // Load the PDF file.
// options must be passed because Node.js has no native `@font-face` and pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
// `Image` support.
pdfjsLib.getDocument({
data: rawData,
disableFontFace: true,
nativeImageDecoderSupport: 'none',
}).then(function (pdfDocument) {
console.log('# PDF document loaded.'); console.log('# PDF document loaded.');
// Get the first page. // Get the first page.

View File

@ -274,7 +274,9 @@ function getDocument(src) {
const NativeImageDecoderValues = Object.values(NativeImageDecoding); const NativeImageDecoderValues = Object.values(NativeImageDecoding);
if (params.nativeImageDecoderSupport === undefined || if (params.nativeImageDecoderSupport === undefined ||
!NativeImageDecoderValues.includes(params.nativeImageDecoderSupport)) { !NativeImageDecoderValues.includes(params.nativeImageDecoderSupport)) {
params.nativeImageDecoderSupport = NativeImageDecoding.DECODE; params.nativeImageDecoderSupport =
(apiCompatibilityParams.nativeImageDecoderSupport ||
NativeImageDecoding.DECODE);
} }
if (!Number.isInteger(params.maxImageSize)) { if (!Number.isInteger(params.maxImageSize)) {
params.maxImageSize = -1; params.maxImageSize = -1;
@ -283,7 +285,7 @@ function getDocument(src) {
params.isEvalSupported = true; params.isEvalSupported = true;
} }
if (typeof params.disableFontFace !== 'boolean') { if (typeof params.disableFontFace !== 'boolean') {
params.disableFontFace = false; params.disableFontFace = apiCompatibilityParams.disableFontFace || false;
} }
if (typeof params.disableRange !== 'boolean') { if (typeof params.disableRange !== 'boolean') {
@ -2168,6 +2170,8 @@ var WorkerTransport = (function WorkerTransportClosure() {
disableStream: params.disableStream, disableStream: params.disableStream,
disableAutoFetch: params.disableAutoFetch, disableAutoFetch: params.disableAutoFetch,
disableCreateObjectURL: params.disableCreateObjectURL, disableCreateObjectURL: params.disableCreateObjectURL,
disableFontFace: params.disableFontFace,
nativeImageDecoderSupport: params.nativeImageDecoderSupport,
}); });
}, },
}; };

View File

@ -15,6 +15,8 @@
let compatibilityParams = Object.create(null); let compatibilityParams = Object.create(null);
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) { if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
const isNodeJS = require('../shared/is_node');
const userAgent = const userAgent =
(typeof navigator !== 'undefined' && navigator.userAgent) || ''; (typeof navigator !== 'undefined' && navigator.userAgent) || '';
const isIE = /Trident/.test(userAgent); const isIE = /Trident/.test(userAgent);
@ -42,9 +44,15 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
compatibilityParams.disableStream = true; compatibilityParams.disableStream = true;
} }
})(); })();
}
const apiCompatibilityParams = Object.freeze(compatibilityParams);
export { // Support: Node.js
apiCompatibilityParams, (function checkFontFaceAndImage() {
}; // Node.js is missing native support for `@font-face` and `Image`.
if (isNodeJS()) {
compatibilityParams.disableFontFace = true;
compatibilityParams.nativeImageDecoderSupport = 'none';
}
})();
}
exports.apiCompatibilityParams = Object.freeze(compatibilityParams);

View File

@ -37,8 +37,5 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
} }
})(); })();
} }
const viewerCompatibilityParams = Object.freeze(compatibilityParams);
export { exports.viewerCompatibilityParams = Object.freeze(compatibilityParams);
viewerCompatibilityParams,
};