Maybe you can use require function.
Add @types/node packages, and write require('pdfjs-dist') at the top of the your source code.
So, you can modify your code like below.
Now, this code will work.
import { PDFJSStatic } from 'pdfjs-dist';
const PDFJS: PDFJSStatic = require('pdfjs-dist');
PDFJS.getDocument('helloworld.pdf').then(console.log);
I think that @types/pdfjs-dist has problems in its implementation.
» npm install @types/pdfjs-dist
Maybe you can use require function.
Add @types/node packages, and write require('pdfjs-dist') at the top of the your source code.
So, you can modify your code like below.
Now, this code will work.
import { PDFJSStatic } from 'pdfjs-dist';
const PDFJS: PDFJSStatic = require('pdfjs-dist');
PDFJS.getDocument('helloworld.pdf').then(console.log);
I think that @types/pdfjs-dist has problems in its implementation.
With @types/pdfjs-dist 2.1.0 and pdfjs-dist "2.1.266", the fundamental issue still doesn't quite seem fixed, but I've found that the approach from their own test works:
import { getDocument, PDFDocumentProxy, PDFPromise, Util } from 'pdfjs-dist';
(It's not pretty, though, as it pollutes your namespace.)
» npm install pdfjs-dist
This worked for me:
Import the package as below
import * as pdfjsLib from "pdfjs-dist";
Copy the pdf.worker.min.mjs file from node_modules/pdfjs-dist/build/ directory into your public folder
Set the worker to point to the file in your public folder
pdfjsLib.GlobalWorkerOptions.workerSrc = window.location.origin + "/pdf.worker.min.mjs";
Helpful articles:
https://medium.com/@hesseclaus/using-pdfjs-with-react-app-rewired-f1f3a2527c45
https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples
I'm also running pdfjs v4 in vite and found this works:
import { GlobalWorkerOptions } from 'pdfjs-dist/legacy/build/pdf.mjs';
GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/legacy/build/pdf.worker.min.mjs', import.meta.url).toString();
(We're running a legacy build, but I assume the main build works the same way)
Vite won't recognise it's a js module and hence won't minify it, so I'm using the min.mjs version here.