Reputation: 707
My version of pdfjs-dist: "pdfjs-dist": "^2.4.456"
This is import my code:
import PDFJS from 'pdfjs-dist'
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'
and when I call :
PDFJS.GlobalWorkerOptions.workerSrc = pdfjsWorker
this.loadingTask = PDFJS.getDocument({ data: self.pdfData })
It return this error: [TypeError: Cannot read property 'GlobalWorkerOptions' of undefined]
PDFJS
, it return undefinded
So when I try this solution in the internet, I add
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.5.207/pdf.min.js"></script>
in my index.html
and
add var PDFJS = window['pdfjs-dist/build/pdf'];
Then it works perfectly...But I dont know why the first approach doesn't work for me...And I do not want use this from CDN because I think it's not stable for my project.
So could you guys give me a point what I missed here? I tried to look around in node-modules
to figure out but I still do not why i can't import PDFJS from 'pdfjs-dist'
;
Upvotes: 8
Views: 12917
Reputation: 1469
Dec 2024 - the files have been updated. I'm using Vite. This is how I got it to work:
import * as pdfjsLib from "pdfjs-dist";
import pdfWorker from "pdfjs-dist/build/pdf.worker.mjs?url";
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfWorker;
Upvotes: 1
Reputation: 353
It's working public direct path
import { getDocument } from 'pdfjs-dist';
import { GlobalWorkerOptions } from 'pdfjs-dist/build/pdf';
GlobalWorkerOptions.workerSrc = window.location.origin + '/pdfjs/pdf.worker.min.js';
and add pdfjs/pdf.worker.min.js in public
Upvotes: 1
Reputation: 54
// It will work Now use this
import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = //unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js;
Upvotes: -1
Reputation: 707
I SOLVED MY PROBLEM
I do not know if it is the root of problem or not, but I just simple change from
import PDFJS from 'pdfjs-dist'
to import * as PDFJS from 'pdfjs-dist'
, and it work perfectly.
Upvotes: 15