-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Setup PDF.js in a website
You can choose to use a pre-built version of PDF.js or build PDF.js from source.
Add the dependencies to your project:
npm install pdfjs-dist node-ensure --save
Simple usage example:
require('pdfjs-dist');
var fs = require('fs');
var data = new Uint8Array(fs.readFileSync('helloworld.pdf'));
PDFJS.getDocument(data).then(function (pdfDocument) {
console.log('Number of pages: ' + pdfDocument.numPages);
});
Depending on the APIs that you want to use, you might need to stuff some DOM APIs. Refer to https://github.com/mozilla/pdf.js/blob/master/examples/node/getinfo.js for a more complex example.
Add the dependencies to your project:
npm install pdfjs-dist entry-loader --save-dev
To use the library in your project add require('pdfjs-dist')
to your file requires and build your project normally.
Refer to https://github.com/mozilla/pdf.js/tree/master/examples/webpack for a complete example.
When the source code of PDF.js changes, the online demo is automatically updated. The source of all demo files can easily be accessed at https://github.com/mozilla/pdf.js/tree/gh-pages. These files can also be uploaded to your server to use PDF.js to display PDF files from your website.
-
Download https://github.com/mozilla/pdf.js/archive/gh-pages.zip.
-
Extract the ZIP file (a directory called "pdf.js-gh-pages" will be created).
-
Copy the following directories to your website:
- pdf.js-gh-pages/build/
- pdf.js-gh-pages/web/
The web/ directory contains a 1 MB PDF file called "compressed.tracemonkey-pldi-09.pdf". This file is only used as an example for the demo and can safely be removed.
-
If you want to open a PDF from your website with PDF.js, simply link to the viewer and pass the location of the PDF file. For example:
<a href="/web/viewer.html?file=%2Fyourpdf.pdf">Open yourpdf.pdf with PDF.js</a>
After cloning PDF.js, you can build PDF.js from source by running node make generic
in Git Bash or another terminal. This will create the built PDF.js in the build
folder, which you can upload to your server. Note that you must include compatibility.js
in order to support browsers like IE8+.