PDF JS Demo: Interactive PDF Viewing & Manipulation

Emma Jul 30, 2026

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.

A full comparison of 6 JS libraries for generating PDFs
A full comparison of 6 JS libraries for generating PDFs

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.

Free PDF Viewer Using AngularJS
Free PDF Viewer Using AngularJS

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:

Best Free JavaScript Books
Best Free JavaScript Books

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

Loading a PDF Document

8 Free PDF Tools to Create, Edit & Convert Documents Easily
8 Free PDF Tools to Create, Edit & Convert Documents Easily

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

REACT JS MASTER PROGRAM – BY PLACEMENT POINT
REACT JS MASTER PROGRAM – BY PLACEMENT POINT

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

HTTP method express
HTTP method express

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

JavaScript DOM Explained Simply: The Key to Dynamic Websites
JavaScript DOM Explained Simply: The Key to Dynamic Websites
an image of a computer screen with some text on it
an image of a computer screen with some text on it
PDF Reader - Free PDF Viewer, Book Reader
PDF Reader - Free PDF Viewer, Book Reader
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
Create Lists From JSON Data in Javascript (Simple Examples)
Create Lists From JSON Data in Javascript (Simple Examples)
Ask Your PDF Review: Chat with Documents and Extract Insights
Ask Your PDF Review: Chat with Documents and Extract Insights
Node.js & Express.js: Build Fast & Scalable Web Apps 🚀🌍
Node.js & Express.js: Build Fast & Scalable Web Apps 🚀🌍
5 Free Yumpu PDF Downloader: How to Save Yumpu as PDFs Free
5 Free Yumpu PDF Downloader: How to Save Yumpu as PDFs Free
With VeryPDF PDF Imposition software, PDF imposing tasks like creating PDF booklets, PDF books
With VeryPDF PDF Imposition software, PDF imposing tasks like creating PDF booklets, PDF books
React Native PDF Reader or Viewer Example
React Native PDF Reader or Viewer Example
100+ Best Node.js Frameworks
100+ Best Node.js Frameworks
How to Darken A PDF File Free (Only 3 Steps)
How to Darken A PDF File Free (Only 3 Steps)
Top NodeJS Frameworks You Should Know
Top NodeJS Frameworks You Should Know
Add Watermark To Image In NodeJS
Add Watermark To Image In NodeJS
an image of a diagram with the words in russian and english, including two different languages
an image of a diagram with the words in russian and english, including two different languages
Accelerate Your Skills: Learn React.js in Just 50 Days
Accelerate Your Skills: Learn React.js in Just 50 Days
Client Challenge
Client Challenge
an image of a cell phone screen with the words design, development, and knowledge on it
an image of a cell phone screen with the words design, development, and knowledge on it
a young man is holding up his hand and pointing to the side project for beginner programming
a young man is holding up his hand and pointing to the side project for beginner programming
Building a Login System for a MERN (MongoDB, Express.js, React.js, Node.js) Web App
Building a Login System for a MERN (MongoDB, Express.js, React.js, Node.js) Web App

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!