diff --git a/src/display/api.js b/src/display/api.js index 4a32bd7c7..d184717d3 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -18,7 +18,7 @@ import { assert, createPromiseCapability, getVerbosityLevel, info, InvalidPDFException, isArrayBuffer, isNum, isSameOrigin, MessageHandler, MissingPDFException, NativeImageDecoding, PageViewport, PasswordException, setVerbosityLevel, - stringToBytes, UnexpectedResponseException, UnknownErrorException, + shadow, stringToBytes, UnexpectedResponseException, UnknownErrorException, unreachable, Util, warn } from '../shared/util'; import { @@ -164,6 +164,12 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) { * converted to OpenType fonts and loaded via font face rules. If disabled, * fonts will be rendered using a built-in font renderer that constructs the * glyphs with primitive path commands. The default value is `false`. + * @property {boolean} disableAutoFetch - (optional) Disable pre-fetching of PDF + * file data. When range requests are enabled PDF.js will automatically keep + * fetching more data even if it isn't needed to display the current page. + * The default value is `false`. + * NOTE: It is also necessary to disable streaming, see above, + * in order for disabling of pre-fetching to work correctly. */ /** @@ -266,6 +272,10 @@ function getDocument(src) { params.disableFontFace = false; } + if (typeof params.disableAutoFetch !== 'boolean') { + params.disableAutoFetch = false; + } + // Set the main-thread verbosity level. setVerbosityLevel(params.verbosity); @@ -333,7 +343,6 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) { typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('BUNDLE_VERSION') : null; source.disableRange = getDefaultSetting('disableRange'); - source.disableAutoFetch = getDefaultSetting('disableAutoFetch'); source.disableStream = getDefaultSetting('disableStream'); if (pdfDataRangeTransport) { source.length = pdfDataRangeTransport.length; @@ -683,6 +692,10 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() { destroy: function PDFDocumentProxy_destroy() { return this.loadingTask.destroy(); }, + + get loadingParams() { + return this.transport.loadingParams; + }, }; return PDFDocumentProxy; })(); @@ -2116,6 +2129,13 @@ var WorkerTransport = (function WorkerTransportClosure() { this.fontLoader.clear(); }); }, + + get loadingParams() { + let params = this._params; + return shadow(this, 'loadingParams', { + disableAutoFetch: params.disableAutoFetch, + }); + }, }; return WorkerTransport; diff --git a/src/display/dom_utils.js b/src/display/dom_utils.js index a2b9b6ebd..fa5b96be7 100644 --- a/src/display/dom_utils.js +++ b/src/display/dom_utils.js @@ -336,8 +336,6 @@ function getDefaultSetting(id) { switch (id) { case 'pdfBug': return globalSettings ? globalSettings.pdfBug : false; - case 'disableAutoFetch': - return globalSettings ? globalSettings.disableAutoFetch : false; case 'disableStream': return globalSettings ? globalSettings.disableStream : false; case 'disableRange': diff --git a/src/display/global.js b/src/display/global.js index 98b8b2dcf..d9cd5ae90 100644 --- a/src/display/global.js +++ b/src/display/global.js @@ -82,18 +82,6 @@ PDFJS.disableRange = (PDFJS.disableRange === undefined ? PDFJS.disableStream = (PDFJS.disableStream === undefined ? false : PDFJS.disableStream); -/** - * Disable pre-fetching of PDF file data. When range requests are enabled - * PDF.js will automatically keep fetching more data even if it isn't needed - * to display the current page. This default behavior can be disabled. - * - * NOTE: It is also necessary to disable streaming, see above, - * in order for disabling of pre-fetching to work correctly. - * @var {boolean} - */ -PDFJS.disableAutoFetch = (PDFJS.disableAutoFetch === undefined ? - false : PDFJS.disableAutoFetch); - /** * Enables special hooks for debugging PDF.js. * @var {boolean} diff --git a/test/driver.js b/test/driver.js index 3a28cc55d..bcbda8308 100644 --- a/test/driver.js +++ b/test/driver.js @@ -360,7 +360,6 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars let absoluteUrl = new URL(task.file, window.location).href; PDFJS.disableRange = task.disableRange; - PDFJS.disableAutoFetch = !task.enableAutoFetch; try { PDFJS.getDocument({ url: absoluteUrl, @@ -368,6 +367,7 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars nativeImageDecoderSupport: task.nativeImageDecoderSupport, cMapUrl: CMAP_URL, cMapPacked: CMAP_PACKED, + disableAutoFetch: !task.enableAutoFetch, }).then((doc) => { task.pdfDoc = doc; this._nextPage(task, failure); diff --git a/web/app.js b/web/app.js index 88da0aaa9..abeb7cc95 100644 --- a/web/app.js +++ b/web/app.js @@ -207,7 +207,7 @@ let PDFViewerApplication = { PDFJS.disableStream = value; }), preferences.get('disableAutoFetch').then(function resolved(value) { - PDFJS.disableAutoFetch = value; + AppOptions.set('disableAutoFetch', value); }), preferences.get('disableFontFace').then(function resolved(value) { if (AppOptions.get('disableFontFace') === true) { @@ -266,7 +266,8 @@ let PDFViewerApplication = { PDFJS.disableStream = (hashParams['disablestream'] === 'true'); } if ('disableautofetch' in hashParams) { - PDFJS.disableAutoFetch = (hashParams['disableautofetch'] === 'true'); + AppOptions.set('disableAutoFetch', + hashParams['disableautofetch'] === 'true'); } if ('disablefontface' in hashParams) { AppOptions.set('disableFontFace', @@ -933,7 +934,11 @@ let PDFViewerApplication = { // the loading bar will not be completely filled, nor will it be hidden. // To prevent displaying a partially filled loading bar permanently, we // hide it when no data has been loaded during a certain amount of time. - if (PDFJS.disableAutoFetch && percent) { + const disableAutoFetch = this.pdfDocument ? + this.pdfDocument.loadingParams['disableAutoFetch'] : + AppOptions.get('disableAutoFetch'); + + if (disableAutoFetch && percent) { if (this.disableAutoFetchLoadingBarTimeout) { clearTimeout(this.disableAutoFetchLoadingBarTimeout); this.disableAutoFetchLoadingBarTimeout = null; diff --git a/web/app_options.js b/web/app_options.js index 2e56a8b98..e0f90cdbe 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -139,6 +139,11 @@ const defaultOptions = { '../external/bcmaps/' : '../web/cmaps/'), kind: OptionKind.API, }, + disableAutoFetch: { + /** @type {boolean} */ + value: false, + kind: OptionKind.API, + }, disableFontFace: { /** @type {boolean} */ value: false, diff --git a/web/base_viewer.js b/web/base_viewer.js index 2b966379a..bc7955750 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -13,7 +13,6 @@ * limitations under the License. */ -import { createPromiseCapability, PDFJS } from 'pdfjs-lib'; import { CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, isValidRotation, MAX_AUTO_SCALE, NullL10n, PresentationModeState, RendererType, @@ -21,6 +20,7 @@ import { } from './ui_utils'; import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue'; import { AnnotationLayerBuilder } from './annotation_layer_builder'; +import { createPromiseCapability } from 'pdfjs-lib'; import { getGlobalEventBus } from './dom_events'; import { PDFPageView } from './pdf_page_view'; import { SimpleLinkService } from './pdf_link_service'; @@ -408,7 +408,7 @@ class BaseViewer { // starts to create the correct size canvas. Wait until one page is // rendered so we don't tie up too many resources early on. onePageRenderedCapability.promise.then(() => { - if (PDFJS.disableAutoFetch) { + if (pdfDocument.loadingParams['disableAutoFetch']) { // XXX: Printing is semi-broken with auto fetch disabled. pagesCapability.resolve(); return;