Move the cMapUrl and cMapPacked options from the global PDFJS object and into getDocument instead

This commit is contained in:
Jonas Jenwald 2018-02-17 16:57:24 +01:00
parent b674409397
commit 3c2fbdffe6
13 changed files with 72 additions and 63 deletions

View File

@ -15,7 +15,7 @@
'use strict';
if (!PDFJS.PDFViewer || !PDFJS.getDocument) {
if (!PDFJS.PDFPageView || !PDFJS.getDocument) {
alert('Please build the pdfjs-dist library using\n' +
' `gulp dist-install`');
}
@ -27,8 +27,8 @@ PDFJS.GlobalWorkerOptions.workerSrc =
// Some PDFs need external cmaps.
//
// PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/';
// PDFJS.cMapPacked = true;
var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/';
var CMAP_PACKED = true;
var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
var PAGE_TO_VIEW = 1;
@ -37,7 +37,11 @@ var SCALE = 1.0;
var container = document.getElementById('pageContainer');
// Loading document.
PDFJS.getDocument(DEFAULT_URL).then(function (pdfDocument) {
PDFJS.getDocument({
url: DEFAULT_URL,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) {
// Document loaded, retrieving the page.
return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) {
// Creating the page view with default parameters.

View File

@ -27,8 +27,8 @@ PDFJS.GlobalWorkerOptions.workerSrc =
// Some PDFs need external cmaps.
//
// PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/';
// PDFJS.cMapPacked = true;
var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/';
var CMAP_PACKED = true;
var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
var SEARCH_FOR = ''; // try 'Mozilla';
@ -60,7 +60,11 @@ container.addEventListener('pagesinit', function () {
});
// Loading document.
PDFJS.getDocument(DEFAULT_URL).then(function (pdfDocument) {
PDFJS.getDocument({
url: DEFAULT_URL,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and
// the (optional) linkService.
pdfViewer.setDocument(pdfDocument);

View File

@ -27,8 +27,8 @@ PDFJS.GlobalWorkerOptions.workerSrc =
// Some PDFs need external cmaps.
//
// PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/';
// PDFJS.cMapPacked = true;
var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/';
var CMAP_PACKED = true;
var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
var SEARCH_FOR = ''; // try 'Mozilla';
@ -60,7 +60,11 @@ container.addEventListener('pagesinit', function () {
});
// Loading document.
PDFJS.getDocument(DEFAULT_URL).then(function (pdfDocument) {
PDFJS.getDocument({
url: DEFAULT_URL,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and
// the (optional) linkService.
pdfSinglePageViewer.setDocument(pdfDocument);

View File

@ -24,8 +24,8 @@ if (typeof PDFJS === 'undefined' || !PDFJS.PDFViewer || !PDFJS.getDocument) {
var USE_ONLY_CSS_ZOOM = true;
var TEXT_LAYER_MODE = 0; // DISABLE
var MAX_IMAGE_SIZE = 1024 * 1024;
PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/';
PDFJS.cMapPacked = true;
var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/';
var CMAP_PACKED = true;
PDFJS.GlobalWorkerOptions.workerSrc =
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
@ -65,6 +65,8 @@ var PDFViewerApplication = {
var loadingTask = PDFJS.getDocument({
url: url,
maxImageSize: MAX_IMAGE_SIZE,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
});
this.pdfLoadingTask = loadingTask;

View File

@ -38,29 +38,31 @@ 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];
var svg = modules[1];
var global = modules[2];
var GlobalWorkerOptions = modules[3].GlobalWorkerOptions;
var network = modules[4];
var GlobalWorkerOptions = modules[2].GlobalWorkerOptions;
var network = modules[3];
api.setPDFNetworkStreamFactory((params) => {
return new network.PDFNetworkStream(params);
});
// In production, change this to point to the built `pdf.worker.js` file.
GlobalWorkerOptions.workerSrc = modules[5];
GlobalWorkerOptions.workerSrc = modules[4];
// In production, change this to point to where the cMaps are placed.
global.PDFJS.cMapUrl = '../../external/bcmaps/';
global.PDFJS.cMapPacked = true;
var CMAP_URL = '../../external/bcmaps/';
var CMAP_PACKED = true;
// Fetch the PDF document from the URL using promises.
api.getDocument(url).then(function (doc) {
api.getDocument({
url: url,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then(function(doc) {
renderDocument(doc, svg);
});
});

View File

@ -142,6 +142,10 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
* with limited image support through stubs (useful for SVG conversion),
* and 'none' where JPEG images will be decoded entirely by PDF.js.
* The default value is 'decode'.
* @property {string} cMapUrl - (optional) The URL where the predefined
* Adobe CMaps are located. Include trailing slash.
* @property {boolean} cMapPacked - (optional) Specifies if the Adobe CMaps are
* binary packed.
* @property {Object} CMapReaderFactory - (optional) The factory that will be
* used when reading built-in CMap files. Providing a custom factory is useful
* for environments without `XMLHttpRequest` support, such as e.g. Node.js.
@ -201,7 +205,7 @@ function getDocument(src) {
let params = Object.create(null);
var rangeTransport = null;
let worker = null;
var CMapReaderFactory = DOMCMapReaderFactory;
let CMapReaderFactory = DOMCMapReaderFactory;
for (var key in source) {
if (key === 'url' && typeof window !== 'undefined') {
@ -289,7 +293,7 @@ function getDocument(src) {
var messageHandler = new MessageHandler(docId, workerId, worker.port);
messageHandler.postMessageTransfers = worker.postMessageTransfers;
var transport = new WorkerTransport(messageHandler, task, networkStream,
CMapReaderFactory);
params, CMapReaderFactory);
task._transport = transport;
messageHandler.send('Ready', null);
});
@ -1550,14 +1554,15 @@ var PDFWorker = (function PDFWorkerClosure() {
*/
var WorkerTransport = (function WorkerTransportClosure() {
function WorkerTransport(messageHandler, loadingTask, networkStream,
CMapReaderFactory) {
params, CMapReaderFactory) {
this.messageHandler = messageHandler;
this.loadingTask = loadingTask;
this.commonObjs = new PDFObjects();
this.fontLoader = new FontLoader(loadingTask.docId);
this._params = params;
this.CMapReaderFactory = new CMapReaderFactory({
baseUrl: getDefaultSetting('cMapUrl'),
isCompressed: getDefaultSetting('cMapPacked'),
baseUrl: params.cMapUrl,
isCompressed: params.cMapPacked,
});
this.destroyed = false;

View File

@ -69,8 +69,9 @@ class DOMCMapReaderFactory {
fetch({ name, }) {
if (!this.baseUrl) {
return Promise.reject(new Error('CMap baseUrl must be specified, ' +
'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").'));
return Promise.reject(new Error(
'The CMap "baseUrl" parameter must be specified, ensure that ' +
'the "cMapUrl" and "cMapPacked" API parameters are provided.'));
}
if (!name) {
return Promise.reject(new Error('CMap name must be specified.'));
@ -345,10 +346,6 @@ function getDefaultSetting(id) {
return globalSettings ? globalSettings.disableFontFace : false;
case 'disableCreateObjectURL':
return globalSettings ? globalSettings.disableCreateObjectURL : false;
case 'cMapUrl':
return globalSettings ? globalSettings.cMapUrl : null;
case 'cMapPacked':
return globalSettings ? globalSettings.cMapPacked : false;
case 'isEvalSupported':
return globalSettings ? globalSettings.isEvalSupported : true;
default:

View File

@ -65,19 +65,6 @@ PDFJS.Util = Util;
PDFJS.PageViewport = PageViewport;
PDFJS.createPromiseCapability = createPromiseCapability;
/**
* The url of where the predefined Adobe CMaps are located. Include trailing
* slash.
* @var {string}
*/
PDFJS.cMapUrl = (PDFJS.cMapUrl === undefined ? null : PDFJS.cMapUrl);
/**
* Specifies if CMaps are binary packed.
* @var {boolean}
*/
PDFJS.cMapPacked = PDFJS.cMapPacked === undefined ? false : PDFJS.cMapPacked;
/**
* By default fonts are converted to OpenType fonts and loaded via font face
* rules. If disabled, the font will be rendered using a built in font

View File

@ -18,6 +18,8 @@
const WAITING_TIME = 100; // ms
const PDF_TO_CSS_UNITS = 96.0 / 72.0;
const CMAP_URL = '../external/bcmaps/';
const CMAP_PACKED = true;
const IMAGE_RESOURCES_PATH = '/web/images/';
const WORKER_SRC = '../build/generic/build/pdf.worker.js';
@ -271,8 +273,6 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
// Configure the global worker options.
PDFJS.GlobalWorkerOptions.workerSrc = WORKER_SRC;
PDFJS.cMapPacked = true;
PDFJS.cMapUrl = '../external/bcmaps/';
PDFJS.pdfBug = true;
// Set the passed options
@ -366,6 +366,8 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
url: absoluteUrl,
password: task.password,
nativeImageDecoderSupport: task.nativeImageDecoderSupport,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
}).then((doc) => {
task.pdfDoc = doc;
this._nextPage(task, failure);

View File

@ -281,8 +281,9 @@ describe('cmap', function() {
done.fail('No CMap should be loaded');
}, function (reason) {
expect(reason instanceof Error).toEqual(true);
expect(reason.message).toEqual('CMap baseUrl must be specified, ' +
'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").');
expect(reason.message).toEqual(
'The CMap "baseUrl" parameter must be specified, ensure that ' +
'the "cMapUrl" and "cMapPacked" API parameters are provided.');
done();
});
});

View File

@ -51,8 +51,9 @@ class NodeCMapReaderFactory {
fetch({ name, }) {
if (!this.baseUrl) {
return Promise.reject(new Error('CMap baseUrl must be specified, ' +
'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").'));
return Promise.reject(new Error(
'The CMap "baseUrl" parameter must be specified, ensure that ' +
'the "cMapUrl" and "cMapPacked" API parameters are provided.'));
}
if (!name) {
return Promise.reject(new Error('CMap name must be specified.'));

View File

@ -50,15 +50,6 @@ import { ViewHistory } from './view_history';
const DEFAULT_SCALE_DELTA = 1.1;
const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000;
function configure(PDFJS) {
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
PDFJS.cMapUrl = '../external/bcmaps/';
} else {
PDFJS.cMapUrl = '../web/cmaps/';
}
PDFJS.cMapPacked = true;
}
const DefaultExternalServices = {
updateFindControlState(data) {},
initPassiveLoading(callbacks) {},
@ -142,8 +133,6 @@ let PDFViewerApplication = {
// Called once when the document is loaded.
initialize(appConfig) {
this.preferences = this.externalServices.createPreferences();
configure(PDFJS);
this.appConfig = appConfig;
return this._readPreferences().then(() => {
@ -298,8 +287,8 @@ let PDFViewerApplication = {
}
if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) &&
hashParams['disablebcmaps'] === 'true') {
PDFJS.cMapUrl = '../external/cmaps/';
PDFJS.cMapPacked = false;
AppOptions.set('cMapUrl', '../external/cmaps/');
AppOptions.set('cMapPacked', false);
}
if ('textlayer' in hashParams) {
switch (hashParams['textlayer']) {

View File

@ -128,6 +128,17 @@ const defaultOptions = {
kind: OptionKind.VIEWER,
},
cMapPacked: {
/** @type {boolean} */
value: true,
kind: OptionKind.API,
},
cMapUrl: {
/** @type {string} */
value: (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION') ?
'../external/bcmaps/' : '../web/cmaps/'),
kind: OptionKind.API,
},
maxImageSize: {
/** @type {number} */
value: -1,