I would like to use pdfjs-dist package in angular project. How to import it angular project
I installed it using
npm install --save pdfjs-dist
But don't know how to import into a particular component and work with it
PDF. js is an open-source JavaScript PDF viewer that renders PDF using web standards-compliant HTML5. Primarily seen in Mozilla Firefox's as the built-in PDF viewer, PDF. js also serves as an easy way for developers and integrators to embed PDF viewing capabilities in a web app or server.
Worker-src is a Content Security Policy (CSP) Level 3 directive that was introduced to specify valid sources for worker scripts (worker, shared worker and service worker) Web Workers makes it possible to run a script operation in a background thread separate from the main execution thread of a web application.
After installing it, in package.json
something look like this
"dependencies": {
...
"pdfjs-dist": "^2.2.228",
...
}
1) Now in component you could be able to import it like
import * as pdfjsLib from 'pdfjs-dist';
2) Now you could use it like bellow example
yourMethodName() {
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
const loadingTask = pdfjsLib.getDocument('https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/examples/learning/helloworld.pdf');
loadingTask.promise.then(function(pdf) {
console.log(pdf);
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With