2

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!!");
       });
1

2 Answers 2

3

Your build configuration does not support option chaining.

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

Sign up to request clarification or add additional context in comments.

1 Comment

This should be avoided at all costs
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.