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

Getting 'MIME type' errors which prevent PDF display #17296

Closed
jamminjames opened this issue Nov 20, 2023 · 12 comments
Closed

Getting 'MIME type' errors which prevent PDF display #17296

jamminjames opened this issue Nov 20, 2023 · 12 comments

Comments

@jamminjames
Copy link

I am getting these MIME type errors which prevent my test file pageviewer.html from displaying the pdf:

For /node_modules/pdfjs-dist/build/pdf.mjs:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.

For /node_modules/pdfjs-dist/web/pdf_viewer.mjs:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.

For pageviewer.js:
Uncaught ReferenceError: pdfjsLib is not defined

How can I fix these? I'm also not sure how to reference what pdf file to display. I'm trying to use the pageviewer.html example from the examples folder here.

I have the latest npm and nodejs installed.

Any help is appreciated!

Configuration:

  • Web browser and its version: Chrome Version 109.0.5414.120 (Official Build) (32-bit)
  • Operating system and its version: Windows 7
  • PDF.js version: latest
  • Is a browser extension: no

Steps to reproduce the problem:

  1. installed pdf.js and set up pageviewer.html as per example
  2. not working

What is the expected behavior? (add screenshot)
To display the pdf

What went wrong? (add screenshot)
Completely blank web page, with the above errors in the Developer Tools Console.

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension):
https://subs.humortimes.com/pageviewer.html

@Snuffleupagus
Copy link
Collaborator

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.

This error message says that your web-server isn't correctly configured, since it returns an incorrect MIME type for *.mjs files. Hence you'll need to update the server configuration to return text/javascript for such files. (Exactly how that's done might vary depending on the server used, and isn't really something that we can help you with here.)

PDF.js version: latest

For future reference: Please remember to provide a version number, since what "latest" is (obviously) changes over time.

@jamminjames
Copy link
Author

Ah, of course. We're using nginx, and that is configured in /etc/nginx/mime.types in our case. Thank you.

@eobeans
Copy link

eobeans commented Dec 11, 2023

Ah, of course. We're using nginx, and that is configured in /etc/nginx/mime.types in our case. Thank you.

hi, how you configured nginx to success deal it ? thx

@eobeans
Copy link

eobeans commented Dec 12, 2023

thx, we deal it. use
http {
include mime.types;
types {
application/javascript js mjs;
}
default_type application/octet-stream;

.....

@frankzhu0925
Copy link

thx, we deal it. use http { include mime.types; types { application/javascript js mjs; } default_type application/octet-stream;

.....

image
Same problem, solved by this conf

@arlomedia
Copy link

For the sake of better search results, the error I was getting in Safari was:

TypeError: text/plain is not a valid JavaScript MIME type

I use Apache, so I edited /etc/mime.types and added mjs after js on the same line as the existing text/javascript entry. I didn't need to restart Apache but did need to clear my browser cache.

@palalor01
Copy link

thx, i did the same and it works perfectly

http { ## use mime types include /etc/nginx/mime.types; types { application/javascript js mjs; } default_type application/octet-stream;

@beit-dp
Copy link

beit-dp commented Jul 9, 2024

If you use Apache, you can set it by updating the .htaccess file:

<IfModule mod_mime.c> AddType text/javascript mjs </IfModule>

@alekswebnet
Copy link

This nginx config works perfectly for me:

server {
    # ...

    location / {
        root   /usr/share/nginx/html;
        index  index.html;

        location ~* \.mjs$ {# target only *.mjs files
            # now we can safely override types since we are only
            # targeting a single file extension.
            types {
                text/javascript mjs;
            }
        }
    }
}

Thanks to the author! https://stackoverflow.com/a/19630564/12674896

@shivangi-2001
Copy link

@alekswebnet hi there,

I used to your code but doesn't help, can you guide me:

Screenshot 2024-09-06 at 5 34 16 PM Screenshot 2024-09-06 at 5 34 04 PM

@calixteman
Copy link
Contributor

@shivangi-2001, sorry but this issue has been closed a while back and is unrelated to pdf.js itself (see #17296 (comment)). So please don't use this issue tracker as a chat room. Thank you.

@susanforme

This comment was marked as outdated.

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

No branches or pull requests