How to build a JavaScript PDF viewer with Nutrient
In this post, we provide you with a step-by-step guide on how you can deploy Nutrient’s JavaScript PDF viewer.
What is a JavaScript PDF viewer?
A JavaScript PDF viewer uses JavaScript to render and view PDF documents in a web browser without the need to download it to your hard drive or use an external application like a PDF reader.
Nutrient JavaScript PDF viewer
We offer a commercial JavaScript PDF viewer library that can easily be integrated into your web application. It comes with 30+ features that let you view, annotate, edit, and sign documents directly in your browser. Out of the box, it has a polished and flexible UI that you can extend or simplify based on your unique use case.
- A prebuilt and polished UI for an improved user experience
- 15+ prebuilt annotation tools to enable document collaboration
- Support for more file types with client-side PDF, MS Office, and image viewing
- Dedicated support from engineers to speed up integration
Example of our JavaScript PDF viewer
To demo our JavaScript PDF viewer, upload a PDF, JPG, PNG, or TIFF file by clicking Open Document under the Standalone option (if you don’t see this option, select Choose Example from the dropdown). Once your document is displayed in the viewer, try drawing freehand, adding a note, or applying a crop or an eSignature.
If you prefer a video tutorial, you can watch our step-by-step guide.
Requirements to get started
To get started, you’ll need:
- The latest stable version of Node.js.
- A package manager compatible with npm. This guide contains usage examples for Yarn and the npm client (installed with Node.js by default).
Adding Nutrient to your project
Follow along for a step-by-step guide to adding Nutrient to your project.
Step 1: Create a project directory
-
Open your terminal or command prompt.
-
Create a new directory for your project:
mkdir my-pdf-viewer cd my-pdf-viewer
Step 2: Initialize your project
-
Initialize a new Node.js project:
npm init -y
Step 3: Install Nutrient
-
Install the
pspdfkit
package fromnpm
:
npm install pspdfkit
Step 4: Set up your assets folder
-
Create an
assets
directory in your project folder:
mkdir assets
-
Copy the required library files to the
assets
folder:
cp -R ./node_modules/pspdfkit/dist/ ./assets/
Make sure your assets
directory contains the pspdfkit.js
file and a pspdfkit-lib
directory with the library assets.
Step 5: Add your PDF document
-
Add the PDF document you want to display to your project’s directory. You can use our demo document as an example.
Step 6: Create your HTML file
-
Create an
index.html
file in your project directory and add the following code:
<!DOCTYPE html> <html> <head> <title>My PDF Viewer</title> <!-- Provide proper viewport information so that the layout works on mobile devices. --> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> </head> <body> <!-- Element where Nutrient will be mounted. --> <div id="pspdfkit" style="height: 100vh;"></div> <script src="assets/pspdfkit.js"></script> <script> PSPDFKit.load({ container: '#pspdfkit', document: 'document.pdf', // Add the path to your document here. }) .then(function (instance) { console.log('PSPDFKit loaded', instance); }) .catch(function (error) { console.error(error.message); }); </script> </body> </html>
Step 7: Serve your website
You’ll use the npm serve
package to serve your project.
-
Install the
serve
package:
npm install --global serve
-
Serve the contents of the current directory:
serve -l 8080 .
-
Navigate to http://localhost:8080 to view the website.
Adding even more capabilities
Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular JavaScript guides:
- Adding annotations
- Editing documents
- Filling PDF forms
- Adding signatures to documents
- Real-time collaboration
- Redaction
- UI customization
Conclusion
You should now have our JavaScript PDF viewer up and running in your web application. If you hit any snags, don’t hesitate to reach out to our Support team for help.
You can also integrate our JavaScript PDF viewer using web frameworks like Angular, Vue.js, and React.js. To see a list of all web frameworks, start your free trial. Or, launch our demo to see our viewer in action.
FAQ
Here are a few frequently asked questions about Nutrient.
What is Nutrient?
Nutrient is a powerful JavaScript library that allows developers to integrate PDF viewing, editing, and annotation capabilities into web applications.
How do I install Nutrient?
You can install Nutrient via npm using the command npm install pspdfkit
.
Can I use Nutrient with frameworks like React or Angular?
Yes, Nutrient supports integration with various frameworks, including React, Angular, and Vue.js.
What file formats does Nutrient support?
Nutrient supports a variety of file formats, including PDF, JPG, PNG, and TIFF.
Is there any support available for developers using Nutrient?
Yes, Nutrient offers dedicated support from engineers to assist with integration and troubleshooting.