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

[Bug]: pdfjs-dist not working in chrome #18957

Closed
lhilgert9 opened this issue Oct 25, 2024 · 4 comments · Fixed by #19010
Closed

[Bug]: pdfjs-dist not working in chrome #18957

lhilgert9 opened this issue Oct 25, 2024 · 4 comments · Fixed by #19010

Comments

@lhilgert9
Copy link

Attach (recommended) or Link to PDF file

Every PDF

Web browser and its version

Google Chrome: 130.0.6723.59

Operating system and its version

macOS 15.0.1

PDF.js version

pdfjs-dist: 4.7.76

Is the bug present in the latest PDF.js version?

Yes

Is a browser extension

No

Steps to reproduce the problem

  1. use pdfjs-dist in a web-app as follows:
import * as pdfJS from 'pdfjs-dist';
pdfJS.GlobalWorkerOptions.workerSrc = window.location.origin + '/pdf.worker.min.js';

export default pdfJS;
  1. build your own pdf viewer
  2. open the web-app in chrome

What is the expected behavior?

The file should be displayed correctly, like in safari.

What went wrong?

Uncaught TypeError: Failed to construct 'Headers': Invalid name
    at PDFNetworkStreamFullRequestReader._onHeadersReceived (pdfjs-dist.js?v=b25d4dee:10221:29)
    at NetworkManager.onStateChange (pdfjs-dist.js?v=b25d4dee:10109:22)

Link to a viewer

No response

Additional context

The error has only occurred since version 4.7.76. In version 4.6.82 everything works without errors.
Looks like the error occurred here.

@nicolo-ribaudo
Copy link
Contributor

https://mozilla.github.io/pdf.js/web/viewer.html works in Chrome without any problem. Please provide a link to a reproduction.

@Snuffleupagus
Copy link
Collaborator

Please see https://github.com/mozilla/pdf.js/blob/master/.github/CONTRIBUTING.md (emphasis mine):

If you are developing a custom solution, first check the examples at https://github.com/mozilla/pdf.js#learning and search existing issues. If this does not help, please prepare a short well-documented example that demonstrates the problem and make it accessible online on your website, JS Bin, GitHub, etc. before opening a new issue or contacting us in the Matrix room -- keep in mind that just code snippets won't help us troubleshoot the problem.

@timvandermeij
Copy link
Contributor

Closing until more information is provided, as requested in the issue template. Without it this issue is unfortunately not actionable for us.

CyberAndrii added a commit to CyberAndrii/pdf.js that referenced this issue Nov 5, 2024
Fixes mozilla#18957

mozilla#18682 introduced a regression that causes the following error:

```
Uncaught TypeError: Failed to construct 'Headers': Invalid name
    at PDFNetworkStreamFullRequestReader._onHeadersReceived (pdf.mjs:10214:29)
    at NetworkManager.onStateChange (pdf.mjs:10103:22)
```

The mentioned PR replaced a call to `getResponseHeader()` with `getAllResponseHeaders()` without handling cases where it may return null or an empty string. Quote from the [docs](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#return_value):

> Returns:
>
>A string representing all of the response's headers (except those whose field name is Set-Cookie) separated by [CRLF](https://developer.mozilla.org/en-US/docs/Glossary/CRLF), or null if no response has been received. If a network error happened, an empty string is returned.
@CyberAndrii
Copy link
Contributor

Made a PR that should fix this: #19010

CyberAndrii added a commit to CyberAndrii/pdf.js that referenced this issue Nov 5, 2024
Fixes mozilla#18957

mozilla#18682 introduced a regression that causes the following error:

```
Uncaught TypeError: Failed to construct 'Headers': Invalid name
    at PDFNetworkStreamFullRequestReader._onHeadersReceived (pdf.mjs:10214:29)
    at NetworkManager.onStateChange (pdf.mjs:10103:22)
```

The mentioned PR replaced a call to `getResponseHeader()` with `getAllResponseHeaders()` without handling cases where it may return null or an empty string. Quote from the [docs](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#return_value):

> Returns:
>
>A string representing all of the response's headers (except those whose field name is Set-Cookie) separated by CRLF, or null if no response has been received. If a network error happened, an empty string is returned.

Run the following code and observe the error in the console. Note that the URL is intentionally set to an invalid value to simulate network error

```js
<script src="//mozilla.github.io/pdf.js/build/pdf.mjs" type="module"></script>

<script type="module">
  var url = 'blob:';

  pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.mjs';

  var loadingTask = pdfjsLib.getDocument(url);
  loadingTask.promise
    .then((pdf) => console.log('PDF loaded'))
    .catch((reason) => console.error(reason));

</script>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants