pdf.js: total is undefined in LoadingTask.onProgress

Attach (recommended) or Link to PDF file here: https://jsfiddle.net/p3ybwp7d/1/

Configuration:

  • Web browser and its version: Google Chrome - Version 62.0.3202.75
  • Operating system and its version: Mac OS 10.13
  • PDF.js version: 1.10.97
  • Is a browser extension: No

Steps to reproduce the problem:

Create loading task and add onProgress callback:

let loadingTask: any = PDFJS.getDocument(this.src);

loadingTask.onProgress = (progressData) => {
  // progressData won't contain "total", only "loaded"
};

What is the expected behaviour? (add screenshot) In previous versions, onProgress did return both total and loaded.

What went wrong? (add screenshot) total field is undefined in loadingTask.onProgress callback.

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension): https://jsfiddle.net/p3ybwp7d/1/

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (12 by maintainers)

Most upvoted comments

It’s related to fetch, since you can observe the same issue in Firefox too (with the dom.streams.enabled and javascript.options.streams prefs set in about:config).

We just updated to 1.10.97 which broke our loading task. As a (hopefully temporary) workaround, we do a header-only fetch for the content-length header beforehand:

const total = await fetch(new Request(documentUrl, { method: 'HEAD', credentials: 'include' }))
    .then(res => parseInt(res.headers.get('content-length'), 10));
    
const loadingTask = window.PDFJS.getDocument({url: documentUrl, withCredentials: true});
loadingTask.onProgress = ({ loaded }) => {
    // do stuff with `loaded` and `total`
};

Hi! My first comment in GitHub, sorry if I make a mistake. I kind of found a solution: The suggestedLength of returnValues is never really updated with the length calculated.

So I did:

returnValues.suggestedLength = length ; , before any “if” .

Also the Http header must match, so attention with Content-Length and Content-length (case sensitive)

First we are setting PDFNetworkStream at https://github.com/mozilla/pdf.js/blob/237bc2ef9df204069c4996e14433e0a35123444a/src/pdf.js#L35-L45

based on the environment and support for stream. So if streaming is supported by the browser then PDFNetworkStream is PDFFetchStream.

After this we are calling this constructor with all the provided params from: https://github.com/mozilla/pdf.js/blob/237bc2ef9df204069c4996e14433e0a35123444a/src/display/api.js#L255

If you want to see what these params are, you can read here: https://github.com/mozilla/pdf.js/blob/237bc2ef9df204069c4996e14433e0a35123444a/src/display/api.js#L98-L140