Integrate into a Vanilla JavaScript Project
This guide will walk you through the steps necessary to integrate PSPDFKit for Web into your project. By the end, you’ll be able to present a PDF document in the PSPDFKit UI.
If you prefer a video tutorial, watch our step-by-step Getting Started with PSPDFKit video guide.
Requirements
-
A package manager compatible with npm. This guide contains usage examples for Yarn and the npm client. The npm client is installed with Node.js by default.
You need Node.js to complete this guide, but in general, Node.js isn’t a requirement for using PSPDFKit for Web.
Adding to Your Project
PSPDFKit for Web library files are distributed as an archive that can be installed as an npm module.
-
Add the PSPDFKit dependency:
yarn add pspdfkit
npm install --save pspdfkit
-
Copy the PSPDFKit for Web distribution to the
assets
directory in your project’s folder:
cp -R ./node_modules/pspdfkit/dist/ ./assets/
-
Make sure your
assets
directory contains thepspdfkit.js
file and apspdfkit-lib
directory with the library assets. -
Make sure your server has the
Content-Type: application/wasm
MIME typeset. Read more about this in the Troubleshooting section.
Integrating into Your Project
-
Rename the PDF document you want to display in your application to
document.pdf
, and then add the PDF document to your project’s root directory. You can use this demo document as an example. -
Add an empty
<div>
element with a definedwidth
andheight
to where PSPDFKit will be mounted:
<div id="pspdfkit" style="width: 100%; height: 100vh;"></div>
-
Include
pspdfkit.js
in your HTML page:
<script src="assets/pspdfkit.js"></script>
-
Initialize PSPDFKit for Web in JavaScript by calling
PSPDFKit.load()
:
<script> PSPDFKit.load({ container: "#pspdfkit", document: "document.pdf" }) .then(function(instance) { console.log("PSPDFKit loaded", instance); }) .catch(function(error) { console.error(error.message); }); </script>
See the full index.html
file below:
<!DOCTYPE html> <html> <head> <title>My App</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 PSPDFKit will be mounted. --> <div id="pspdfkit" style="width: 100%; height: 100vh;"></div> <script src="assets/pspdfkit.js"></script> <script> PSPDFKit.load({ container: "#pspdfkit", document: "document.pdf", }) .then(function(instance) { console.log("PSPDFKit loaded", instance); }) .catch(function(error) { console.error(error.message); }); </script> </body> </html>
Serving Your Website
You’ll use the npm serve
package as a simple HTTP server.
-
Install the
serve
package:
yarn global add serve
npm install --global serve
-
Serve the contents of the current directory:
serve -l 8080 .
-
Navigate to http://localhost:8080 to view the website.