Merge pull request #9480 from Snuffleupagus/refactor-worker-options
[api-major] Move the worker related options from the global `PDFJS` object
This commit is contained in:
commit
99060e2485
@ -15,7 +15,8 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
var DEFAULT_URL = '../../test/pdfs/f1040.pdf';
|
||||
var DEFAULT_SCALE = 1.0;
|
||||
|
@ -8,7 +8,8 @@ require('pdfjs-dist');
|
||||
var pdfPath = '../helloworld/helloworld.pdf';
|
||||
|
||||
// Setting worker path to worker bundle.
|
||||
PDFJS.workerSrc = '../../build/browserify/pdf.worker.bundle.js';
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../build/browserify/pdf.worker.bundle.js';
|
||||
|
||||
// Loading a document.
|
||||
var loadingTask = PDFJS.getDocument(pdfPath);
|
||||
|
@ -22,7 +22,8 @@ if (!PDFJS.PDFViewer || !PDFJS.getDocument) {
|
||||
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
// Some PDFs need external cmaps.
|
||||
//
|
||||
|
@ -22,7 +22,8 @@ if (!PDFJS.PDFViewer || !PDFJS.getDocument) {
|
||||
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
// Some PDFs need external cmaps.
|
||||
//
|
||||
|
@ -22,7 +22,8 @@ if (!PDFJS.PDFSinglePageViewer || !PDFJS.getDocument) {
|
||||
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
// Some PDFs need external cmaps.
|
||||
//
|
||||
|
@ -2,17 +2,19 @@
|
||||
|
||||
// In production, the bundled pdf.js shall be used instead of SystemJS.
|
||||
Promise.all([System.import('pdfjs/display/api'),
|
||||
System.import('pdfjs/display/global'),
|
||||
System.import('pdfjs/display/worker_options'),
|
||||
System.import('pdfjs/display/network'),
|
||||
System.resolve('pdfjs/worker_loader')])
|
||||
.then(function (modules) {
|
||||
var api = modules[0], global = modules[1], network = modules[2];
|
||||
var api = modules[0];
|
||||
var GlobalWorkerOptions = modules[1].GlobalWorkerOptions;
|
||||
var network = modules[2];
|
||||
api.setPDFNetworkStreamFactory((params) => {
|
||||
return new network.PDFNetworkStream(params);
|
||||
});
|
||||
|
||||
// In production, change this to point to the built `pdf.worker.js` file.
|
||||
global.PDFJS.workerSrc = modules[3];
|
||||
GlobalWorkerOptions.workerSrc = modules[3];
|
||||
|
||||
// Fetch the PDF document from the URL using promises.
|
||||
api.getDocument('helloworld.pdf').then(function (pdf) {
|
||||
|
@ -22,7 +22,8 @@
|
||||
//
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
//
|
||||
// Asynchronous download PDF
|
||||
|
@ -34,7 +34,8 @@
|
||||
//
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
// Opening PDF by passing its binary data as a string. It is still preferable
|
||||
// to use Uint8Array, but string or array-like structure will work too.
|
||||
|
@ -33,7 +33,8 @@
|
||||
// pdf.js's one, or the pdf.js is executed via eval(), the workerSrc property
|
||||
// shall be specified.
|
||||
//
|
||||
// PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
// PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
// '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
var pdfDoc = null,
|
||||
pageNum = 1,
|
||||
|
@ -24,10 +24,12 @@ if (typeof PDFJS === 'undefined' || !PDFJS.PDFViewer || !PDFJS.getDocument) {
|
||||
var USE_ONLY_CSS_ZOOM = true;
|
||||
var TEXT_LAYER_MODE = 0; // DISABLE
|
||||
PDFJS.maxImageSize = 1024 * 1024;
|
||||
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/';
|
||||
PDFJS.cMapPacked = true;
|
||||
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
|
||||
var DEFAULT_SCALE_DELTA = 1.1;
|
||||
var MIN_SCALE = 0.25;
|
||||
|
@ -39,16 +39,21 @@ function renderDocument(pdf, svgLib) {
|
||||
Promise.all([System.import('pdfjs/display/api'),
|
||||
System.import('pdfjs/display/svg'),
|
||||
System.import('pdfjs/display/global'),
|
||||
System.import('pdfjs/display/worker_options'),
|
||||
System.import('pdfjs/display/network'),
|
||||
System.resolve('pdfjs/worker_loader')])
|
||||
.then(function (modules) {
|
||||
var api = modules[0], svg = modules[1], global = modules[2], network = modules[3];
|
||||
var api = modules[0];
|
||||
var svg = modules[1];
|
||||
var global = modules[2];
|
||||
var GlobalWorkerOptions = modules[3].GlobalWorkerOptions;
|
||||
var network = modules[4];
|
||||
api.setPDFNetworkStreamFactory((params) => {
|
||||
return new network.PDFNetworkStream(params);
|
||||
});
|
||||
|
||||
// In production, change this to point to the built `pdf.worker.js` file.
|
||||
global.PDFJS.workerSrc = modules[4];
|
||||
GlobalWorkerOptions.workerSrc = modules[5];
|
||||
|
||||
// In production, change this to point to where the cMaps are placed.
|
||||
global.PDFJS.cMapUrl = '../../external/bcmaps/';
|
||||
|
@ -18,7 +18,8 @@ var PAGE_NUMBER = 1;
|
||||
var PAGE_SCALE = 1.5;
|
||||
var SVG_NS = 'http://www.w3.org/2000/svg';
|
||||
|
||||
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
PDFJS.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
|
||||
|
||||
function buildSVG(viewport, textContent) {
|
||||
// Building SVG with size of the viewport (for simplicity)
|
||||
|
@ -8,7 +8,8 @@ var pdfjsLib = require('pdfjs-dist');
|
||||
var pdfPath = '../helloworld/helloworld.pdf';
|
||||
|
||||
// Setting worker path to worker bundle.
|
||||
pdfjsLib.PDFJS.workerSrc = '../../build/webpack/pdf.worker.bundle.js';
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
'../../build/webpack/pdf.worker.bundle.js';
|
||||
|
||||
// Loading a document.
|
||||
var loadingTask = pdfjsLib.getDocument(pdfPath);
|
||||
|
2
external/dist/webpack.js
vendored
2
external/dist/webpack.js
vendored
@ -18,7 +18,7 @@ var pdfjs = require('./build/pdf.js');
|
||||
var PdfjsWorker = require('worker-loader!./build/pdf.worker.js');
|
||||
|
||||
if (typeof window !== 'undefined' && 'Worker' in window) {
|
||||
pdfjs.PDFJS.workerPort = new PdfjsWorker();
|
||||
pdfjs.GlobalWorkerOptions.workerPort = new PdfjsWorker();
|
||||
}
|
||||
|
||||
module.exports = pdfjs;
|
||||
|
@ -16,9 +16,10 @@
|
||||
|
||||
import {
|
||||
assert, createPromiseCapability, getVerbosityLevel, info, InvalidPDFException,
|
||||
isArrayBuffer, isSameOrigin, MessageHandler, MissingPDFException,
|
||||
NativeImageDecoding, PageViewport, PasswordException, stringToBytes,
|
||||
UnexpectedResponseException, UnknownErrorException, unreachable, Util, warn
|
||||
isArrayBuffer, isNum, isSameOrigin, MessageHandler, MissingPDFException,
|
||||
NativeImageDecoding, PageViewport, PasswordException, setVerbosityLevel,
|
||||
stringToBytes, UnexpectedResponseException, UnknownErrorException,
|
||||
unreachable, Util, warn
|
||||
} from '../shared/util';
|
||||
import {
|
||||
DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, getDefaultSetting,
|
||||
@ -27,17 +28,17 @@ import {
|
||||
import { FontFaceObject, FontLoader } from './font_loader';
|
||||
import { CanvasGraphics } from './canvas';
|
||||
import globalScope from '../shared/global_scope';
|
||||
import { GlobalWorkerOptions } from './worker_options';
|
||||
import { Metadata } from './metadata';
|
||||
import { PDFDataTransportStream } from './transport_stream';
|
||||
import { WebGLContext } from './webgl';
|
||||
|
||||
var DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536
|
||||
|
||||
var isWorkerDisabled = false;
|
||||
var workerSrc;
|
||||
var isPostMessageTransfersDisabled = false;
|
||||
let isWorkerDisabled = false;
|
||||
let workerSrc;
|
||||
|
||||
var pdfjsFilePath =
|
||||
const pdfjsFilePath =
|
||||
typeof PDFJSDev !== 'undefined' &&
|
||||
PDFJSDev.test('PRODUCTION && !(MOZCENTRAL || FIREFOX)') &&
|
||||
typeof document !== 'undefined' && document.currentScript ?
|
||||
@ -46,8 +47,8 @@ var pdfjsFilePath =
|
||||
var fakeWorkerFilesLoader = null;
|
||||
var useRequireEnsure = false;
|
||||
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) {
|
||||
// For GENERIC build we need add support of different fake file loaders
|
||||
// for different frameworks.
|
||||
// For GENERIC build we need to add support for different fake file loaders
|
||||
// for different frameworks.
|
||||
if (typeof window === 'undefined') {
|
||||
// node.js - disable worker and set require.ensure.
|
||||
isWorkerDisabled = true;
|
||||
@ -124,8 +125,12 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
|
||||
* @property {number} rangeChunkSize - Optional parameter to specify
|
||||
* maximum number of bytes fetched per range request. The default value is
|
||||
* 2^16 = 65536.
|
||||
* @property {PDFWorker} worker - The worker that will be used for the loading
|
||||
* and parsing of the PDF data.
|
||||
* @property {PDFWorker} worker - (optional) The worker that will be used for
|
||||
* the loading and parsing of the PDF data.
|
||||
* @property {boolean} postMessageTransfers - (optional) Enables transfer usage
|
||||
* in postMessage for ArrayBuffers. The default value is `true`.
|
||||
* @property {number} verbosity - (optional) Controls the logging level; the
|
||||
* constants from {VerbosityLevel} should be used.
|
||||
* @property {string} docBaseUrl - (optional) The base URL of the document,
|
||||
* used when attempting to recover valid absolute URLs for annotations, and
|
||||
* outline items, that (incorrectly) only specify relative URLs.
|
||||
@ -192,7 +197,7 @@ function getDocument(src) {
|
||||
|
||||
var params = {};
|
||||
var rangeTransport = null;
|
||||
var worker = null;
|
||||
let worker = null;
|
||||
var CMapReaderFactory = DOMCMapReaderFactory;
|
||||
|
||||
for (var key in source) {
|
||||
@ -238,11 +243,23 @@ function getDocument(src) {
|
||||
params.nativeImageDecoderSupport = NativeImageDecoding.DECODE;
|
||||
}
|
||||
|
||||
// Set the main-thread verbosity level.
|
||||
setVerbosityLevel(params.verbosity);
|
||||
|
||||
if (!worker) {
|
||||
const workerParams = {
|
||||
postMessageTransfers: params.postMessageTransfers,
|
||||
verbosity: params.verbosity,
|
||||
};
|
||||
// Worker was not provided -- creating and owning our own. If message port
|
||||
// is specified in global settings, using it.
|
||||
var workerPort = getDefaultSetting('workerPort');
|
||||
worker = workerPort ? PDFWorker.fromPort(workerPort) : new PDFWorker();
|
||||
// is specified in global worker options, using it.
|
||||
let workerPort = GlobalWorkerOptions.workerPort;
|
||||
if (workerPort) {
|
||||
workerParams.port = workerPort;
|
||||
worker = PDFWorker.fromPort(workerParams);
|
||||
} else {
|
||||
worker = new PDFWorker(workerParams);
|
||||
}
|
||||
task._worker = worker;
|
||||
}
|
||||
var docId = task.docId;
|
||||
@ -313,8 +330,7 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
|
||||
maxImageSize: getDefaultSetting('maxImageSize'),
|
||||
disableFontFace: getDefaultSetting('disableFontFace'),
|
||||
disableCreateObjectURL: getDefaultSetting('disableCreateObjectURL'),
|
||||
postMessageTransfers: getDefaultSetting('postMessageTransfers') &&
|
||||
!isPostMessageTransfersDisabled,
|
||||
postMessageTransfers: worker.postMessageTransfers,
|
||||
docBaseUrl: source.docBaseUrl,
|
||||
nativeImageDecoderSupport: source.nativeImageDecoderSupport,
|
||||
ignoreErrors: source.ignoreErrors,
|
||||
@ -1191,9 +1207,19 @@ class LoopbackPort {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} PDFWorkerParameters
|
||||
* @property {string} name - (optional) The name of the worker.
|
||||
* @property {Object} port - (optional) The `workerPort`.
|
||||
* @property {boolean} postMessageTransfers - (optional) Enables transfer usage
|
||||
* in postMessage for ArrayBuffers. The default value is `true`.
|
||||
* @property {number} verbosity - (optional) Controls the logging level; the
|
||||
* constants from {VerbosityLevel} should be used.
|
||||
*/
|
||||
|
||||
/**
|
||||
* PDF.js web worker abstraction, it controls instantiation of PDF documents and
|
||||
* WorkerTransport for them. If creation of a web worker is not possible,
|
||||
* WorkerTransport for them. If creation of a web worker is not possible,
|
||||
* a "fake" worker will be used instead.
|
||||
* @class
|
||||
*/
|
||||
@ -1201,8 +1227,8 @@ var PDFWorker = (function PDFWorkerClosure() {
|
||||
let nextFakeWorkerId = 0;
|
||||
|
||||
function getWorkerSrc() {
|
||||
if (getDefaultSetting('workerSrc')) {
|
||||
return getDefaultSetting('workerSrc');
|
||||
if (GlobalWorkerOptions.workerSrc) {
|
||||
return GlobalWorkerOptions.workerSrc;
|
||||
}
|
||||
if (typeof workerSrc !== 'undefined') {
|
||||
return workerSrc;
|
||||
@ -1212,7 +1238,7 @@ var PDFWorker = (function PDFWorkerClosure() {
|
||||
pdfjsFilePath) {
|
||||
return pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2');
|
||||
}
|
||||
throw new Error('No PDFJS.workerSrc specified');
|
||||
throw new Error('No "GlobalWorkerOptions.workerSrc" specified.');
|
||||
}
|
||||
|
||||
function getMainThreadWorkerMessageHandler() {
|
||||
@ -1279,14 +1305,19 @@ var PDFWorker = (function PDFWorkerClosure() {
|
||||
|
||||
let pdfWorkerPorts = new WeakMap();
|
||||
|
||||
function PDFWorker(name, port) {
|
||||
/**
|
||||
* @param {PDFWorkerParameters} params - The worker initialization parameters.
|
||||
*/
|
||||
function PDFWorker({ name = null, port = null,
|
||||
postMessageTransfers = true, verbosity = null, } = {}) {
|
||||
if (port && pdfWorkerPorts.has(port)) {
|
||||
throw new Error('Cannot use more than one PDFWorker per port');
|
||||
}
|
||||
|
||||
this.name = name;
|
||||
this.destroyed = false;
|
||||
this.postMessageTransfers = true;
|
||||
this.postMessageTransfers = postMessageTransfers !== false;
|
||||
this.verbosity = (isNum(verbosity) ? verbosity : getVerbosityLevel());
|
||||
|
||||
this._readyCapability = createPromiseCapability();
|
||||
this._port = null;
|
||||
@ -1383,12 +1414,11 @@ var PDFWorker = (function PDFWorkerClosure() {
|
||||
this._webWorker = worker;
|
||||
if (!data.supportTransfers) {
|
||||
this.postMessageTransfers = false;
|
||||
isPostMessageTransfersDisabled = true;
|
||||
}
|
||||
this._readyCapability.resolve();
|
||||
// Send global setting, e.g. verbosity level.
|
||||
messageHandler.send('configure', {
|
||||
verbosity: getVerbosityLevel(),
|
||||
verbosity: this.verbosity,
|
||||
});
|
||||
} else {
|
||||
this._setupFakeWorker();
|
||||
@ -1411,11 +1441,8 @@ var PDFWorker = (function PDFWorkerClosure() {
|
||||
}
|
||||
});
|
||||
|
||||
var sendTest = function () {
|
||||
var postMessageTransfers =
|
||||
getDefaultSetting('postMessageTransfers') &&
|
||||
!isPostMessageTransfersDisabled;
|
||||
var testObj = new Uint8Array([postMessageTransfers ? 255 : 0]);
|
||||
const sendTest = () => {
|
||||
let testObj = new Uint8Array([this.postMessageTransfers ? 255 : 0]);
|
||||
// Some versions of Opera throw a DATA_CLONE_ERR on serializing the
|
||||
// typed array. Also, checking if we can use transfers.
|
||||
try {
|
||||
@ -1494,11 +1521,14 @@ var PDFWorker = (function PDFWorkerClosure() {
|
||||
},
|
||||
};
|
||||
|
||||
PDFWorker.fromPort = function (port) {
|
||||
if (pdfWorkerPorts.has(port)) {
|
||||
return pdfWorkerPorts.get(port);
|
||||
/**
|
||||
* @param {PDFWorkerParameters} params - The worker initialization parameters.
|
||||
*/
|
||||
PDFWorker.fromPort = function(params) {
|
||||
if (pdfWorkerPorts.has(params.port)) {
|
||||
return pdfWorkerPorts.get(params.port);
|
||||
}
|
||||
return new PDFWorker(null, port);
|
||||
return new PDFWorker(params);
|
||||
};
|
||||
|
||||
PDFWorker.getWorkerSrc = function() {
|
||||
|
@ -349,12 +349,6 @@ function getDefaultSetting(id) {
|
||||
return globalSettings ? globalSettings.cMapUrl : null;
|
||||
case 'cMapPacked':
|
||||
return globalSettings ? globalSettings.cMapPacked : false;
|
||||
case 'postMessageTransfers':
|
||||
return globalSettings ? globalSettings.postMessageTransfers : true;
|
||||
case 'workerPort':
|
||||
return globalSettings ? globalSettings.workerPort : null;
|
||||
case 'workerSrc':
|
||||
return globalSettings ? globalSettings.workerSrc : null;
|
||||
case 'maxImageSize':
|
||||
return globalSettings ? globalSettings.maxImageSize : -1;
|
||||
case 'isEvalSupported':
|
||||
|
@ -14,11 +14,10 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
createBlob, createObjectURL, createPromiseCapability, getVerbosityLevel,
|
||||
InvalidPDFException, isLittleEndian, MissingPDFException, OPS, PageViewport,
|
||||
PasswordException, PasswordResponses, removeNullCharacters, setVerbosityLevel,
|
||||
shadow, UnexpectedResponseException, UnknownErrorException,
|
||||
UNSUPPORTED_FEATURES, Util, VERBOSITY_LEVELS
|
||||
createBlob, createObjectURL, createPromiseCapability, InvalidPDFException,
|
||||
isLittleEndian, MissingPDFException, OPS, PageViewport, PasswordException,
|
||||
PasswordResponses, removeNullCharacters, shadow, UnexpectedResponseException,
|
||||
UnknownErrorException, UNSUPPORTED_FEATURES, Util
|
||||
} from '../shared/util';
|
||||
import { DEFAULT_LINK_REL, getFilenameFromUrl, LinkTarget } from './dom_utils';
|
||||
import {
|
||||
@ -26,6 +25,7 @@ import {
|
||||
} from './api';
|
||||
import { AnnotationLayer } from './annotation_layer';
|
||||
import globalScope from '../shared/global_scope';
|
||||
import { GlobalWorkerOptions } from './worker_options';
|
||||
import { Metadata } from './metadata';
|
||||
import { renderTextLayer } from './text_layer';
|
||||
import { SVGGraphics } from './svg';
|
||||
@ -41,22 +41,6 @@ var PDFJS = globalScope.PDFJS;
|
||||
|
||||
PDFJS.pdfBug = false;
|
||||
|
||||
if (PDFJS.verbosity !== undefined) {
|
||||
setVerbosityLevel(PDFJS.verbosity);
|
||||
}
|
||||
delete PDFJS.verbosity;
|
||||
Object.defineProperty(PDFJS, 'verbosity', {
|
||||
get() {
|
||||
return getVerbosityLevel();
|
||||
},
|
||||
set(level) {
|
||||
setVerbosityLevel(level);
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
});
|
||||
|
||||
PDFJS.VERBOSITY_LEVELS = VERBOSITY_LEVELS;
|
||||
PDFJS.OPS = OPS;
|
||||
PDFJS.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
|
||||
PDFJS.shadow = shadow;
|
||||
@ -111,21 +95,6 @@ PDFJS.cMapPacked = PDFJS.cMapPacked === undefined ? false : PDFJS.cMapPacked;
|
||||
PDFJS.disableFontFace = (PDFJS.disableFontFace === undefined ?
|
||||
false : PDFJS.disableFontFace);
|
||||
|
||||
/**
|
||||
* Path and filename of the worker file. Required when the worker is enabled
|
||||
* in development mode. If unspecified in the production build, the worker
|
||||
* will be loaded based on the location of the pdf.js file. It is recommended
|
||||
* that the workerSrc is set in a custom application to prevent issues caused
|
||||
* by third-party frameworks and libraries.
|
||||
* @var {string}
|
||||
*/
|
||||
PDFJS.workerSrc = (PDFJS.workerSrc === undefined ? null : PDFJS.workerSrc);
|
||||
|
||||
/**
|
||||
* Defines global port for worker process. Overrides `workerSrc` setting.
|
||||
*/
|
||||
PDFJS.workerPort = (PDFJS.workerPort === undefined ? null : PDFJS.workerPort);
|
||||
|
||||
/**
|
||||
* Disable range request loading of PDF files. When enabled and if the server
|
||||
* supports partial content requests then the PDF will be fetched in chunks.
|
||||
@ -161,13 +130,6 @@ PDFJS.disableAutoFetch = (PDFJS.disableAutoFetch === undefined ?
|
||||
*/
|
||||
PDFJS.pdfBug = (PDFJS.pdfBug === undefined ? false : PDFJS.pdfBug);
|
||||
|
||||
/**
|
||||
* Enables transfer usage in postMessage for ArrayBuffers.
|
||||
* @var {boolean}
|
||||
*/
|
||||
PDFJS.postMessageTransfers = (PDFJS.postMessageTransfers === undefined ?
|
||||
true : PDFJS.postMessageTransfers);
|
||||
|
||||
/**
|
||||
* Disables URL.createObjectURL usage.
|
||||
* @var {boolean}
|
||||
@ -208,6 +170,7 @@ PDFJS.getDocument = getDocument;
|
||||
PDFJS.LoopbackPort = LoopbackPort;
|
||||
PDFJS.PDFDataRangeTransport = PDFDataRangeTransport;
|
||||
PDFJS.PDFWorker = PDFWorker;
|
||||
PDFJS.GlobalWorkerOptions = GlobalWorkerOptions;
|
||||
|
||||
PDFJS.getFilenameFromUrl = getFilenameFromUrl;
|
||||
|
||||
|
39
src/display/worker_options.js
Normal file
39
src/display/worker_options.js
Normal file
@ -0,0 +1,39 @@
|
||||
/* Copyright 2018 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
const GlobalWorkerOptions = Object.create(null);
|
||||
|
||||
/**
|
||||
* Defines global port for worker process. Overrides the `workerSrc` option.
|
||||
* @var {Object}
|
||||
*/
|
||||
GlobalWorkerOptions.workerPort = (GlobalWorkerOptions.workerPort === undefined ?
|
||||
null : GlobalWorkerOptions.workerPort);
|
||||
|
||||
/**
|
||||
* Path and filename of the worker file. Required when workers are enabled in
|
||||
* development mode. If unspecified in production builds, the worker will be
|
||||
* loaded based on the location of the `pdf.js` file.
|
||||
*
|
||||
* NOTE: The `workerSrc` should always be set in custom applications, in order
|
||||
* to prevent issues caused by third-party frameworks and libraries.
|
||||
* @var {string}
|
||||
*/
|
||||
GlobalWorkerOptions.workerSrc = (GlobalWorkerOptions.workerSrc === undefined ?
|
||||
'' : GlobalWorkerOptions.workerSrc);
|
||||
|
||||
export {
|
||||
GlobalWorkerOptions,
|
||||
};
|
@ -23,20 +23,10 @@
|
||||
* to the PDF.js.
|
||||
* @constructor
|
||||
*/
|
||||
function PDFJS() {
|
||||
function PDFJS() { // eslint-disable-line no-unused-vars
|
||||
// Mock class constructor. See src/display/api.js.
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls the logging level.
|
||||
* The constants from PDFJS.VERBOSITY_LEVELS should be used:
|
||||
* - errors
|
||||
* - warnings [default]
|
||||
* - infos
|
||||
* @var {number}
|
||||
*/
|
||||
PDFJS.verbosity = PDFJS.VERBOSITY_LEVELS.warnings;
|
||||
|
||||
/**
|
||||
* Represents the eventual result of an asynchronous operation.
|
||||
* @external Promise
|
||||
|
@ -28,6 +28,7 @@ var pdfjsDisplayTextLayer = require('./display/text_layer.js');
|
||||
var pdfjsDisplayAnnotationLayer = require('./display/annotation_layer.js');
|
||||
var pdfjsDisplayDOMUtils = require('./display/dom_utils.js');
|
||||
var pdfjsDisplaySVG = require('./display/svg.js');
|
||||
let pdfjsDisplayWorkerOptions = require('./display/worker_options.js');
|
||||
|
||||
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
|
||||
const isNodeJS = require('./shared/is_node.js');
|
||||
@ -82,6 +83,7 @@ exports.NativeImageDecoding = pdfjsSharedUtil.NativeImageDecoding;
|
||||
exports.UnexpectedResponseException =
|
||||
pdfjsSharedUtil.UnexpectedResponseException;
|
||||
exports.OPS = pdfjsSharedUtil.OPS;
|
||||
exports.VerbosityLevel = pdfjsSharedUtil.VerbosityLevel;
|
||||
exports.UNSUPPORTED_FEATURES = pdfjsSharedUtil.UNSUPPORTED_FEATURES;
|
||||
exports.createValidAbsoluteUrl = pdfjsSharedUtil.createValidAbsoluteUrl;
|
||||
exports.createObjectURL = pdfjsSharedUtil.createObjectURL;
|
||||
@ -93,3 +95,4 @@ exports.RenderingCancelledException =
|
||||
exports.getFilenameFromUrl = pdfjsDisplayDOMUtils.getFilenameFromUrl;
|
||||
exports.LinkTarget = pdfjsDisplayDOMUtils.LinkTarget;
|
||||
exports.addLinkAttributes = pdfjsDisplayDOMUtils.addLinkAttributes;
|
||||
exports.GlobalWorkerOptions = pdfjsDisplayWorkerOptions.GlobalWorkerOptions;
|
||||
|
@ -142,10 +142,10 @@ var FontType = {
|
||||
MMTYPE1: 10,
|
||||
};
|
||||
|
||||
var VERBOSITY_LEVELS = {
|
||||
errors: 0,
|
||||
warnings: 1,
|
||||
infos: 5,
|
||||
const VerbosityLevel = {
|
||||
ERRORS: 0,
|
||||
WARNINGS: 1,
|
||||
INFOS: 5,
|
||||
};
|
||||
|
||||
var CMapCompressionType = {
|
||||
@ -251,10 +251,12 @@ var OPS = {
|
||||
constructPath: 91,
|
||||
};
|
||||
|
||||
var verbosity = VERBOSITY_LEVELS.warnings;
|
||||
let verbosity = VerbosityLevel.WARNINGS;
|
||||
|
||||
function setVerbosityLevel(level) {
|
||||
verbosity = level;
|
||||
if (Number.isInteger(level)) {
|
||||
verbosity = level;
|
||||
}
|
||||
}
|
||||
|
||||
function getVerbosityLevel() {
|
||||
@ -265,19 +267,19 @@ function getVerbosityLevel() {
|
||||
// as warning that Workers were disabled, which is important to devs but not
|
||||
// end users.
|
||||
function info(msg) {
|
||||
if (verbosity >= VERBOSITY_LEVELS.infos) {
|
||||
if (verbosity >= VerbosityLevel.INFOS) {
|
||||
console.log('Info: ' + msg);
|
||||
}
|
||||
}
|
||||
|
||||
// Non-fatal warnings.
|
||||
function warn(msg) {
|
||||
if (verbosity >= VERBOSITY_LEVELS.warnings) {
|
||||
if (verbosity >= VerbosityLevel.WARNINGS) {
|
||||
console.log('Warning: ' + msg);
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated API function -- display regardless of the PDFJS.verbosity setting.
|
||||
// Deprecated API function -- display regardless of the `verbosity` setting.
|
||||
function deprecated(details) {
|
||||
console.log('Deprecated API usage: ' + details);
|
||||
}
|
||||
@ -1573,7 +1575,7 @@ export {
|
||||
FONT_IDENTITY_MATRIX,
|
||||
IDENTITY_MATRIX,
|
||||
OPS,
|
||||
VERBOSITY_LEVELS,
|
||||
VerbosityLevel,
|
||||
UNSUPPORTED_FEATURES,
|
||||
AnnotationBorderStyleType,
|
||||
AnnotationFieldFlag,
|
||||
|
@ -16,9 +16,10 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var WAITING_TIME = 100; // ms
|
||||
var PDF_TO_CSS_UNITS = 96.0 / 72.0;
|
||||
const WAITING_TIME = 100; // ms
|
||||
const PDF_TO_CSS_UNITS = 96.0 / 72.0;
|
||||
const IMAGE_RESOURCES_PATH = '/web/images/';
|
||||
const WORKER_SRC = '../build/generic/build/pdf.worker.js';
|
||||
|
||||
/**
|
||||
* @class
|
||||
@ -267,8 +268,9 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
|
||||
* @param {DriverOptions} options
|
||||
*/
|
||||
function Driver(options) {
|
||||
// Configure the global PDFJS object
|
||||
PDFJS.workerSrc = '../build/generic/build/pdf.worker.js';
|
||||
// Configure the global worker options.
|
||||
PDFJS.GlobalWorkerOptions.workerSrc = WORKER_SRC;
|
||||
|
||||
PDFJS.cMapPacked = true;
|
||||
PDFJS.cMapUrl = '../external/bcmaps/';
|
||||
PDFJS.pdfBug = true;
|
||||
|
@ -24,10 +24,10 @@ import {
|
||||
DOMCanvasFactory, RenderingCancelledException
|
||||
} from '../../src/display/dom_utils';
|
||||
import {
|
||||
getDocument, PDFDocumentProxy, PDFPageProxy, PDFWorker
|
||||
getDocument, PDFDataRangeTransport, PDFDocumentProxy, PDFPageProxy, PDFWorker
|
||||
} from '../../src/display/api';
|
||||
import { GlobalWorkerOptions } from '../../src/display/worker_options';
|
||||
import isNodeJS from '../../src/shared/is_node';
|
||||
import { PDFJS } from '../../src/display/global';
|
||||
|
||||
describe('api', function() {
|
||||
let basicApiFileName = 'basicapi.pdf';
|
||||
@ -324,7 +324,7 @@ describe('api', function() {
|
||||
}
|
||||
|
||||
it('worker created or destroyed', function (done) {
|
||||
var worker = new PDFJS.PDFWorker('test1');
|
||||
var worker = new PDFWorker({ name: 'test1', });
|
||||
worker.promise.then(function () {
|
||||
expect(worker.name).toEqual('test1');
|
||||
expect(!!worker.port).toEqual(true);
|
||||
@ -361,7 +361,7 @@ describe('api', function() {
|
||||
});
|
||||
});
|
||||
it('worker created and can be used in getDocument', function (done) {
|
||||
var worker = new PDFJS.PDFWorker('test1');
|
||||
var worker = new PDFWorker({ name: 'test1', });
|
||||
var loadingTask = getDocument(
|
||||
buildGetDocumentParams(basicApiFileName, {
|
||||
worker,
|
||||
@ -386,9 +386,9 @@ describe('api', function() {
|
||||
});
|
||||
});
|
||||
it('creates more than one worker', function (done) {
|
||||
var worker1 = new PDFJS.PDFWorker('test1');
|
||||
var worker2 = new PDFJS.PDFWorker('test2');
|
||||
var worker3 = new PDFJS.PDFWorker('test3');
|
||||
var worker1 = new PDFWorker({ name: 'test1', });
|
||||
var worker2 = new PDFWorker({ name: 'test2', });
|
||||
var worker3 = new PDFWorker({ name: 'test3', });
|
||||
var ready = Promise.all([worker1.promise, worker2.promise,
|
||||
worker3.promise]);
|
||||
ready.then(function () {
|
||||
@ -406,7 +406,7 @@ describe('api', function() {
|
||||
it('gets current workerSrc', function() {
|
||||
let workerSrc = PDFWorker.getWorkerSrc();
|
||||
expect(typeof workerSrc).toEqual('string');
|
||||
expect(workerSrc).toEqual(PDFJS.workerSrc);
|
||||
expect(workerSrc).toEqual(GlobalWorkerOptions.workerSrc);
|
||||
});
|
||||
});
|
||||
describe('PDFDocument', function() {
|
||||
@ -1305,7 +1305,7 @@ describe('api', function() {
|
||||
var fetches = 0;
|
||||
var getDocumentPromise = getDocumentData().then(function (data) {
|
||||
var initialData = data.subarray(0, initialDataLength);
|
||||
transport = new PDFJS.PDFDataRangeTransport(data.length, initialData);
|
||||
transport = new PDFDataRangeTransport(data.length, initialData);
|
||||
transport.requestDataRange = function (begin, end) {
|
||||
fetches++;
|
||||
waitSome(function () {
|
||||
@ -1339,7 +1339,7 @@ describe('api', function() {
|
||||
var fetches = 0;
|
||||
var getDocumentPromise = getDocumentData().then(function (data) {
|
||||
var initialData = data.subarray(0, initialDataLength);
|
||||
transport = new PDFJS.PDFDataRangeTransport(data.length, initialData);
|
||||
transport = new PDFDataRangeTransport(data.length, initialData);
|
||||
transport.requestDataRange = function (begin, end) {
|
||||
fetches++;
|
||||
if (fetches === 1) {
|
||||
|
@ -42,8 +42,8 @@
|
||||
|
||||
function initializePDFJS(callback) {
|
||||
Promise.all([
|
||||
'pdfjs/display/global',
|
||||
'pdfjs/display/api',
|
||||
'pdfjs/display/worker_options',
|
||||
'pdfjs/display/network',
|
||||
'pdfjs/display/fetch_stream',
|
||||
'pdfjs/shared/is_node',
|
||||
@ -77,9 +77,9 @@ function initializePDFJS(callback) {
|
||||
'pdfjs-test/unit/util_stream_spec',
|
||||
].map(function (moduleName) {
|
||||
return SystemJS.import(moduleName);
|
||||
})).then(function (modules) {
|
||||
var displayGlobal = modules[0];
|
||||
var displayApi = modules[1];
|
||||
})).then(function(modules) {
|
||||
var displayApi = modules[0];
|
||||
const GlobalWorkerOptions = modules[1].GlobalWorkerOptions;
|
||||
var PDFNetworkStream = modules[2].PDFNetworkStream;
|
||||
var PDFFetchStream = modules[3].PDFFetchStream;
|
||||
const isNodeJS = modules[4];
|
||||
@ -101,7 +101,7 @@ function initializePDFJS(callback) {
|
||||
}
|
||||
|
||||
// Configure the worker.
|
||||
displayGlobal.PDFJS.workerSrc = '../../build/generic/build/pdf.worker.js';
|
||||
GlobalWorkerOptions.workerSrc = '../../build/generic/build/pdf.worker.js';
|
||||
|
||||
callback();
|
||||
});
|
||||
|
12
web/app.js
12
web/app.js
@ -21,9 +21,9 @@ import {
|
||||
ProgressBar, RendererType, TextLayerMode
|
||||
} from './ui_utils';
|
||||
import {
|
||||
build, createBlob, getDocument, getFilenameFromUrl, InvalidPDFException,
|
||||
LinkTarget, MissingPDFException, OPS, PDFJS, PDFWorker, shadow,
|
||||
UnexpectedResponseException, UNSUPPORTED_FEATURES, version
|
||||
build, createBlob, getDocument, getFilenameFromUrl, GlobalWorkerOptions,
|
||||
InvalidPDFException, LinkTarget, MissingPDFException, OPS, PDFJS, PDFWorker,
|
||||
shadow, UnexpectedResponseException, UNSUPPORTED_FEATURES, version
|
||||
} from 'pdfjs-lib';
|
||||
import { CursorTool, PDFCursorTools } from './pdf_cursor_tools';
|
||||
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
|
||||
@ -54,11 +54,11 @@ function configure(PDFJS) {
|
||||
PDFJS.imageResourcesPath = './images/';
|
||||
if (typeof PDFJSDev !== 'undefined' &&
|
||||
PDFJSDev.test('FIREFOX || MOZCENTRAL || GENERIC || CHROME')) {
|
||||
PDFJS.workerSrc = '../build/pdf.worker.js';
|
||||
GlobalWorkerOptions.workerSrc = '../build/pdf.worker.js';
|
||||
}
|
||||
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
|
||||
PDFJS.cMapUrl = '../external/bcmaps/';
|
||||
PDFJS.workerSrc = '../src/worker_loader.js';
|
||||
GlobalWorkerOptions.workerSrc = '../src/worker_loader.js';
|
||||
} else {
|
||||
PDFJS.cMapUrl = '../web/cmaps/';
|
||||
}
|
||||
@ -740,6 +740,8 @@ let PDFViewerApplication = {
|
||||
PDFJSDev.test('FIREFOX || MOZCENTRAL || CHROME')) {
|
||||
parameters.docBaseUrl = this.baseUrl;
|
||||
}
|
||||
// TODO: Remove this once all options are moved from the `PDFJS` object.
|
||||
parameters.verbosity = PDFJS.verbosity;
|
||||
|
||||
if (args) {
|
||||
for (let prop in args) {
|
||||
|
Loading…
Reference in New Issue
Block a user