Build a JavaScript PDF viewer with Apryse WebViewer SDK and npm
This guide demonstrates how to integrate the Apryse WebViewer SDK into a vanilla JavaScript application using Node Package Manager (npm), without a bundler. You’ll install the SDK, serve its assets, and load it directly in the browser using a script tag. The examples use a simple HTML‑based project to focus on the core integration steps. By the end, you’ll initialize WebViewer and render a PDF document in the UI.
You can also download our ready-to-use GitHub samples to get started quickly, or explore the interactive Showcase demo to see WebViewer's full capabilities in action.
Prerequisites
Before you start:
Install Node.js and npm. We recommend using the latest active LTS release.
Install Visual Studio Code or another code editor to develop and debug your code.
Get your Apryse trial key.
1. Create your project
Set up your project by creating a folder and preparing a workspace for your WebViewer application.
In your terminal, go to the directory where you want to create the project.
Create a new project folder and move to it so you can start working with it:
mkdir webviewer-npm-integration
cd webviewer-npm-integrationInitialize your project and generate a default
package.jsonfile:
npm init -y2. Install WebViewer
Next, install the Apryse WebViewer SDK using npm. This command adds the WebViewer package to your project, allowing you to integrate the PDF viewer and editor into your application.
After navigating to your webviewer-npm-integration project directory, run the following command to install WebViewer:
3. Copy WebViewer assets
WebViewer needs access to its static assets at runtime, including WebAssembly modules, HTML, and CSS files. You must copy these assets into the public directory so they can be served correctly. For more, see Copying WebViewer static assets.
From your project root, create the
public/lib/webviewerdirectory if it doesn't already exist:
Copy all WebViewer static assets from
node_modules/@pdftron/webviewer/publicinto the newpublic/lib/webviewerdirectory:
Copy the
webviewer.min.jslibrary file fromnode_modulesto thepublicdirectory:
Your project should now include a similar structure:
4. Create PDF viewer
In this section, you'll create an HTML page for your WebViewer application, add the WebViewer script, define a container for the viewer, and initialize WebViewer so it can load and display a document.
Open the
webviewer-npm-integrationfolder in Visual Studio Code.Create a new
index.htmlfile in thewebviewer-npm-integration/publicfolder.Add the following HTML to the
index.htmlfile:
Add the following JavaScript to your
index.htmlfile, placing it after the<div id="viewer">element and before the closing</body>tag:
Save the
index.htmlfile.
5. Verify your output
Once your project files are in place, serve the webpage so that WebViewer can load its UI and display the PDF you added as the initial document in index.html. We use http-server to preview the page locally in your browser.
From your project directory, run the following command to start a local web server:
If prompted, press y to install http-server. A successful output looks similar to:
Open the localhost URL from your terminal to view the WebViewer UI and PDF document.
Get started video
In this 4-minute video, learn how to install WebViewer in a simple HTML-based project using npm.
Next Steps
Last updated
Was this helpful?