Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewer does not support local files on Chromium #10587

Closed
schlamar opened this issue Feb 26, 2019 · 3 comments
Closed

Viewer does not support local files on Chromium #10587

schlamar opened this issue Feb 26, 2019 · 3 comments
Labels

Comments

@schlamar
Copy link

Configuration:

  • Web browser and its version: QtWebEngine 5.12 (Chromium 69), Chrome 72
  • Operating system and its version: Windows 7
  • PDF.js version: 2.0.943, 2.1.266
  • Is a browser extension: No

Steps to reproduce the problem:

  1. Open viewer locally (file:///C:/pdftest/web/viewer.html)

What is the expected behavior?

Embedded PDF shown (it works in Firefox ESR 60).

What went wrong?

Error in console: Fetch API cannot load file:///C:/pdftest/web/compressed.tracemonkey-pldi-09.pdf. URL scheme must be "http" or "https" for CORS request.

I have seen #9361 and #9020 but I don't think that's the solution in this case.

@Snuffleupagus
Copy link
Collaborator

As is clearly mentioned in the README, see https://github.com/mozilla/pdf.js#getting-the-code, not all browsers support the file:// protocol:

Finally, you need to start a local web server as some browsers do not allow opening PDF files using a file:// URL.

@schlamar
Copy link
Author

Chrome supports the file protocol, as it works with the built-in PDF viewer and with the chrome extension (which was apparently fixed in #9363). So I'm pretty sure this is a bug in pdf.js and not a missing feature in Chrome.

fetch with file:// doesn't work in Firefox either (just try fetch("file:///C:/pdftest/web/compressed.tracemonkey-pldi-09.pdf" in console). So there must be a slightly difference opening the viewer in Firefox and on Chrome.

@schlamar
Copy link
Author

schlamar commented Feb 26, 2019

OK, I found it:

{
  var isNodeJS = __w_pdfjs_require__(4);

  if (isNodeJS()) {
    var PDFNodeStream = __w_pdfjs_require__(165).PDFNodeStream;

    pdfjsDisplayAPI.setPDFNetworkStreamFactory(function (params) {
      return new PDFNodeStream(params);
    });
  } else if (typeof Response !== 'undefined' && 'body' in Response.prototype && typeof ReadableStream !== 'undefined') {
    var PDFFetchStream = __w_pdfjs_require__(168).PDFFetchStream;

    pdfjsDisplayAPI.setPDFNetworkStreamFactory(function (params) {
      return new PDFFetchStream(params);
    });
  } else {
    var PDFNetworkStream = __w_pdfjs_require__(169).PDFNetworkStream;

    pdfjsDisplayAPI.setPDFNetworkStreamFactory(function (params) {
      return new PDFNetworkStream(params);
    });
  }
}

My Firefox (60 ESR) doesn't have body in Response.prototype, so it is using the PDFNetworkStream (which works with file URLs).

Looks like the logic not to use fetch for file protocol (#9363) is totally missing in your release builds.

I assume the viewer is broken on a newer Firefox, too.

Edit Removing the else if branch in pdf.js from the code above makes it work.

Edit2 Firefox 65 works, as it supports fetch with file protocol. My point from above is still valid. The fix from #9363 is not in the release build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants