ng2-pdf-viewer: Shouldn't ng2-pdf-viewer use pdf.worker.min.js from pdfjs-dist library?

Bug Report or Feature Request (mark with an x)
- [ ] Regression (a behavior that used to work and stopped working in a new release)
- [x] Bug report -> please search issues before submitting
- [ ] Feature request
- [ ] Documentation issue or request

My project has to work without outside download requests, so we had to include our own pdf.worker.min.js. But this aproach can be quite troublesome, since pdf.worker.min.js version can change depending on pdfjs version.

As pdfjs-dist library (which is included in ng2-pdf-viewer dependencies) already comes with its version of pdf.worker.min.js, wouldn’t it be better for ng2-pdf-viewer to use it instead of downloading it from an external source?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 22 (9 by maintainers)

Most upvoted comments

In this way as a developer you don’t need to copy and paste pdf.worker.min.js to your assets folder and link it in the code.

My project has the same limitations. But instead of copying and pasting the pdf.worker.min.js into my own assets folder, I provided them as an angular asset in my angular.json. Perhaps it might help someone. I suppose using this you do not need to care for manual updates in pdfjs-dist anymore. Still, it might be a good idea to stick the version of both libraries to avoid nasty surprises.

"architect": {
    "build": {
        "options": {
            "assets": [
                "src/assets",
                "src/manifest.json",
                {
                    "glob": "pdf.worker.min.js",
                    "input": "node_modules/pdfjs-dist/build/",
                    "output": "/assets/js/"
                }
            ],

As documented in the readme, you can provide the following to your pdf-components constructor:

public constructor() {
    (window as any).pdfWorkerSrc = '/assets/js/pdf.worker.min.js';
}

@LAlves91 You can always copy pdf.worker.min.js from node_modules to your assets folder before you build and then set it like this https://github.com/VadimDez/ng2-pdf-viewer#set-custom-path-to-the-worker

@VadimDez and @nika1001,

The project I work on had a MAJOR problem recently because of this solution: pdfjs-dist isn’t static in ng2-pdf-viewer peer dependencies (pdfjs-dist: “^2.2.228”).

  • Problem: our version of pdf.worker.min.js was behind considering the latest pdfjs-dist minor release.
  • Result: PDF viewer wasn’t rendering PDF files but browser console didn’t show ANY errors. It took me a while to remember the cause could be our fixed version of pdf.worker.
  • Fix: we decided to freeze ng2-pdf-viewer AND pdfjs-dist versions in our package.json. Updates will have to be made with caution.

I think this can be seen as an evidence about this solution not being the best aproach to not downloading pdf.worker from an external url.

Best regards.

To make setup and use of this library easier - pdf.worker.min.js is getting dowloaded from the CDN in the first place. In this way as a developer you don’t need to copy and paste pdf.worker.min.js to your assets folder and link it in the code.

Hi @htruong24 ,

At the moment, this is how we have declared our PDF dependencies in package.json:

"ng2-pdf-viewer": "5.3.2", "pdfjs-dist": "2.1.266",

If your project has the same limitations as mine (application shouldn’t access external links), the trick is using these steps:

  • Fix ng2-pdf-viewer version;
  • See which version of pdfjs-dist that specific ng2-pdf-viewer is using;
  • With the information of the previous step, fix the version of pdfjs-dist in your package.json.

Once we followed this steps, all our functionalities which had PDF viewers stabilized.

Best regards!