PDF.js is an open-source JavaScript library developed by Mozilla that allows users to open and interact with PDF documents directly in the browser without the need for a separate PDF plugin or software. It's widely used in web applications for displaying PDF content, and it's also the foundation of the PDF viewer in Firefox. Let's explore PDF.js with a demo that showcases its capabilities.

Before diving into the demo, let's briefly understand why PDF.js is beneficial. Firstly, it enables web developers to create seamless user experiences by integrating PDF viewing into web applications. Secondly, it supports a wide range of PDF features, including text extraction, form filling, and annotations. Lastly, it's cross-platform and compatible with various browsers.

Setting Up the PDF.js Demo
To get started with the PDF.js demo, you'll need to include the library in your HTML file. You can either download the library or use a CDN. Here's how you can include it using a CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.min.js"></script>
Loading a PDF Document

Once the library is included, you can load a PDF document using the `PDFDocument` object. Here's a simple example:
<script> const loadingTask = pdfjs.getDocument('path/to/your.pdf').promise; loadingTask.then(function(pdf) { // Now you can use 'pdf' to access the PDF document }); </script>
Displaying the PDF in a Canvas Element

After loading the PDF, you can display its content in a canvas element using the `PDFPage` object. Here's how you can do it:
<script> loadingTask.then(function(pdf) { pdf.getPage(1).then(function(page) { const canvas = document.getElementById('pdfCanvas'); const viewport = page.getViewport({ scale: 1 }); canvas.height = viewport.height; canvas.width = viewport.width; const renderContext = { canvasContext: canvas.getContext('2d'), viewport: viewport }; page.render(renderContext); }); }); </script>
Interacting with the PDF

PDF.js allows for more than just displaying PDF content. You can interact with the PDF, extract text, and even fill forms. Let's explore these features with our demo.
Text Extraction




















You can extract text from a PDF using the `PDFPage` object's `getTextContent` method. Here's an example:
<script> pdf.getPage(1).then(function(page) { page.getTextContent().then(function(textContent) { // 'textContent.items' is an array of text items console.log(textContent.items); }); }); </script>
Form Filling
PDF.js supports filling out forms in PDF documents. Here's a simple example of how to set the value of a text field:
<script> pdf.getPage(1).then(function(page) { const annotations = page.annotations; const formFields = annotations.filter(function(annotation) { return annotation.subtype === 'Widget'; }); formFields.forEach(function(field) { if (field.fieldName === 'yourFieldName') { field.value = 'Your value'; } }); }); </script>
PDF.js offers a wealth of features for working with PDF documents in the browser. This demo only scratches the surface of what's possible. To learn more, check out the official PDF.js documentation and explore the library's extensive API. Happy coding!