How to give pdf.js worker in Angular CLI application

后端 未结 5 901
醉酒成梦
醉酒成梦 2021-01-06 21:53

I used pdf.js directly in angular app for some pdf purposes. It works fine.

I imported the pdfjs from the pdfjs-dist and my package.json includes pdfjs-dist.

5条回答
  •  独厮守ぢ
    2021-01-06 22:29

    I solved this by including the pdf.worker.js file in the bundle much the same way as assets are stored.

    "assets": [
              "src/favicon.ico",
              "src/assets",
              {
                "glob": "pdf.worker.*",
                "input": "node_modules/pdfjs-dist/build/",
                "output": ""
              }              
            ],
    

    Then you need to

    import * as pdf from 'pdfjs-dist';
    // ... //
    pdf.GlobalWorkerOptions.workerSrc = 'pdf.worker.js';
    const loadingTask = pdf.getDocument({ data: this.pdfData });
    

提交回复
热议问题