saad sarhani
saad sarhani

Reputation: 37

How i can fix this error message with pdfjs-dist and angular?

I install pdfjs-lib in project angular for convert PDF to canvas after I get this error message:

Error: ./node_modules/pdfjs-dist/build/pdf.js 2094:26
Module parse failed: Unexpected token (2094:26)
File was processed with these loaders:
 * ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
 * ./node_modules/@ngtools/webpack/src/ivy/index.js
You may need an additional loader to handle the result of these loaders.
|   async destroy() {
|     this.destroyed = true;
>     await this._transport?.destroy();
|     this._transport = null;
| 

the goal is to convert the PDF page to canvas before rendering in HTML :

import * as pdfjslib from 'pdfjs-dist';
pdfjslib.getDocument(url).promise.then(async(pdf) => {
        pdf.getPage(1).then(async (page) => {
          let viewport = page.getViewport({ scale: 0.3 });
          let canvas =<HTMLCanvasElement> document.getElementById("card"+pageId);
          let context =<CanvasRenderingContext2D> canvas.getContext("2d");
          canvas.width = viewport.width;
          canvas.height = viewport.height;
          await page.render({
            canvasContext: context,
            viewport: viewport
          });
          resolve("ok");
        }).catch((err) => {
          reject("Erreur sur le pdf!!");
        });
      }).catch((err) => {
        reject("Erreur sur le pdf!!");
       });

Upvotes: 2

Views: 7259

Answers (2)

Thuan Tran
Thuan Tran

Reputation: 329

I'm not sure if you still have this issue but downgrade to a lower version works for me, or because you are working with Angular, just use another library like ngx-extended-pdf-viewer, it provides a robust API and utils to work with PDF file.

Upvotes: 0

John Hashim
John Hashim

Reputation: 39

Your build configuration does not support option chaining.

You should try to use legacy folder, like this: import from legacy build folder

Upvotes: 3

Related Questions