Available Now: Explore our latest release with enhanced accessibility and powerful IDP features
By Andrey Safonov | 2023 Jun 06
3 min
Tags
pdf viewer
pdf.js
JavaScript PDF Viewer
In this blog, we will cover how to embed a PDF viewer into a JavaScript app. Now before we dive in, let’s take a look at the options we have. Naturally, we have the Apryse WebViewer, although you may consider using one of the PDF viewers out there built using Mozilla’s PDF.js. This is an open-source PDF rendering engine built by Mozilla and maintained by the development community. PDF.js comes with PDF rendering and an out-of-the-box UI. It is great for rendering 2-page PDF documents and doing very basic annotations like adding text to the page or drawing on top of the page. However, there are some issues that mean it may not work for you.
Here are some issues with PDF.js PDF viewing:
Nothing against PDF.js, it is a great open-source project. You can watch a video where I compare a number of open-source projects built on PDF.js.
Now why would you pick Apryse WebViewer?
Okay, now let’s get started with embedding ApryseWebViewer in your JavaScript App.
Let’s create a local http server that will serve the contents of the folder and make it accessible in our browser. You should also have a Node environmenthttps://nodejs.org/en setup and configured. You can use npm, which comes out of the box with Node installation, or yarn https://yarnpkg.com/, a popular package manager alternative after that. With that setup out of the way, create a folder and run the following in a terminal in the root of the folder you just created:
npm install -g http-server
You can download WebViewer.zip https://www.pdftron.com/downloads/WebViewer.zip, complete with samples and all necessary dependencies. Go ahead and extract it to the folder you just created.
In the root of the folder you just created, go ahead and create a new `index.html` with the following contents:
<!DOCTYPE html>
<html>
<head>
<title>Basic WebViewer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<!-- Import WebViewer as a script tag -->
<script src='WebViewer/lib/webviewer.min.js'></script>
<body>
<div id='viewer' style='width: 1024px; height: 600px; margin: 0 auto;'></div>
<script>
WebViewer({
path: 'WebViewer/lib', // path to the Apryse 'lib' folder on your server
licenseKey: 'YOUR_LICENSE_KEY', // sign up to get a key at https://dev.apryse.com
initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo.pdf',
// initialDoc: '/path/to/my/file.pdf', // You can also use documents on your server
}, document.getElementById('viewer'))
.then(instance => {
const { documentViewer, annotationManager } = instance.Core;
// call methods from instance, documentViewer and annotationManager as needed
// you can also access major namespaces from the instance as follows:
// const Tools = instance.Core.Tools;
// const Annotations = instance.Core.Annotations;
documentViewer.addEventListener('documentLoaded', () => {
// call methods relating to the loaded document
});
});
</script>
</body>
</html>
http-server -a localhost
Open http://localhost:8080/index.html on your browser, and you should see WebViewer load up.
Now that you have PDF Viewer up and running check out our other guides on how to load files or how to customize the WebViewer. Also, you can chat with us on Discord if you get stuck. If you have any more questions or want to learn more about Apryse SDK functionality, feel free to contact us.
PRODUCTS
Enterprise
Small Business
Popular Content