Allow to bundle the worker with the app not to need to download it

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

Based on some network issues, I have added pdfjs-dist to my package.json. Then I found I can set custom path to the worker… Can I use the bundled pdfjs-dist instead of downloading one?

Thank you for reply!

1 possible answer(s) on “Allow to bundle the worker with the app not to need to download it

  1. This solved it for me.

    dependencies:
        "@angular/core": "^6.0.0",
        "ng2-pdf-viewer": "^5.0.1",
    resolutions:
        "pdfjs-dist": "2.0.489",
        "@types/pdfjs-dist": "^0.1.2"
    

    I’m making a mobile app that can view pdfs offline using the <pdf-viewer> element. I needed to customize the worker path due to cloudflare not being listed in our <meta> content security policy or available offline, but did need to add the following, http: for localhost development, file: for cordova android/ios and electron mac/win/linux.

    worker-src http: file:;
    

    Similar to ng2-pdf-viewer itself, my app is also using webpack.
    In the client before the component containing the <pdf-viewer> element is constructed:

    declare let window: any;
    window.pdfWorkerSrc = require('pdfjs-dist/webpack');
    

    I no longer get the error Warning: Setting up fake worker. pdf.js 351 and my own animated loading spinner on the page no longer noticeably freezes/is choppy during the pdf’s rendering, indicating that pdf rendering is no longer processing on the UI-thread, but instead on the worker-thread.

    I found this solution for webpack users of pdfjs-dist in issue 7612, here.