How to build a JavaScript PDF viewer with Nutrient
Table of contents
Build a JavaScript PDF viewer in three steps: Install the @nutrient-sdk/viewer package, add a container element to your HTML, and initialize with NutrientViewer.load() using useCDN: true for automatic asset loading.
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 lets 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(opens in a new tab).
- A package manager compatible with npm(opens in a new tab). This guide contains usage examples for Yarn(opens in a new tab) and the npm client(opens in a new tab) (installed with Node.js by default).
Adding Nutrient to your project
Install the @nutrient-sdk/viewer package via npm:
npm i @nutrient-sdk/viewerpnpm add @nutrient-sdk/vieweryarn add @nutrient-sdk/viewerThe SDK will automatically load required assets (fonts, icons, web workers) from Nutrient’s CDN at runtime when you use the useCDN: true option.
Integrating into your project
Add the PDF document you want to display to your project’s directory. You can use our demo document as an example.
Add a mounting
<div>and a script reference to your HTML:<div id="nutrient" style="width: 100%; height: 100vh;"></div><script type="module" src="index.js"></script>Create your JavaScript entry file (
index.js) with the following code:index.js (async () => {const container = document.getElementById("nutrient");// Import the SDKconst NutrientViewer = (await import("@nutrient-sdk/viewer")).default;if (container && NutrientViewer) {// Ensure there's only one NutrientViewer instanceNutrientViewer.unload(container);NutrientViewer.load({container,document: "document.pdf",useCDN: true, // Load assets from Nutrient's CDN}).then((instance) => {console.log("Nutrient loaded", instance);}).catch((error) => {console.error(error.message);});}})();
Serving your website
Install the
servepackage globally:Terminal window npm install --global serveStart a local server from the current directory:
Terminal window serve -l 8080 .Open your browser and go to
http://localhost:8080to view your website.
Adding even more capabilities
After deployment, customize the viewer or add more capabilities. 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 encounter any issues, contact 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.
Do you prefer an open source approach? See how to build a JavaScript PDF viewer with PDF.js.
Comparing libraries before you build? See our roundup of the best JavaScript PDF viewer options.
FAQ
Nutrient is a JavaScript library for integrating PDF viewing, editing, and annotation into web applications.
You can install Nutrient via npm using the command npm install @nutrient-sdk/viewer.
Yes, Nutrient supports integration with various frameworks, including React, Angular, and Vue.js.
Nutrient supports a variety of file formats, including PDF, JPG, PNG, and TIFF.
Yes, Nutrient offers dedicated support from engineers to assist with integration and troubleshooting.