Running Full APIs without viewer

Initial setup

  1. Download WebViewer, unzip the file, and move the "WebViewer" folder to a desired location within a web server folder.
  2. Create an empty HTML file and place it in the same location as the WebViewer folder (file will be referred to as SampleTest.html).
  3. Create an empty JavaScript file and place it in the same location as the WebViewer folder (file will be referred to as SampleTest.js).

Apryse Docs Image

This guide uses ES7 JavaScript async/await. For more information on async/await, refer to these online explanations.

Setting up your HTML document

Open up SampleTest.html with a text editor and copy/paste the following code into the HTML document.

HTML

1<!DOCTYPE html>
2<html>
3<head>
4 <!-- for WebViewer versions before version 8 this would be WebViewer/lib/core/CoreControls.js -->
5 <script src="WebViewer/lib/core/webviewer-core.min.js"></script>
6
7 <script src="WebViewer/lib/core/pdf/PDFNet.js"></script>
8 <script src="WebViewer/samples/FileSaver.min.js"></script>
9 <script src="WebViewer/samples/full-apis/Setup.js"></script>
10</head>
11<body>
12 <script src="SampleTest.js"></script>
13</body>
14</html>

Here we include Full API, the necessary libraries it depends on, and our currently empty SampleTest.js custom file. Adding these files in a different order may result in errors.

Setting up your JavaScript document

Open up SampleTest.js with a text editor and copy/paste the following code into the JavaScript document:

1(async function() {
2 "use strict";
3 const licenseKey = 'Insert commercial license key here after purchase';
4
5 // the path to where the PDF worker files are
6 Core.setWorkerPath('WebViewer/lib/core');
7
8 async function main() {
9 // To be filled
10 }
11
12 await PDFNet.runWithCleanup(main, licenseKey);
13 // Alt: PDFNet.runWithoutCleanup(main, licenseKey);
14 // 'runWithoutCleanup' does not deallocate anything after finishing.
15})();

In this script our code is wrapped inside an immediately invoked function expression. Inside the function we have an empty main() function that we pass as a callback to PDFNet.runWithCleanup(). We'll add our custom code to the main() function later. We use PDFNet.runWithCleanup() to take care of locking and memory management of our code, more detail of this can be found in the Advanced Features section. Also note that PDFNet.runWithCleanup() and PDFNet.runWithoutCleanup() don't necessarily need to be used with async/await functions. They can be use with any function that returns a promise.

Setting up your main function

Copy/paste the following code and use it to replace our currently empty main() function.

JavaScript

1async function main() {
2 try {
3 // creates an empty pdf document
4 const doc = await PDFNet.PDFDoc.create();
5 doc.initSecurityHandler();
6 // Locks all operations on the document
7 doc.lock();
8
9 // insert user code after this point
10 const pgnum = await doc.getPageCount();
11 alert(`Test Complete! Your file has ${pgnum} pages`);
12 } catch(err) {
13 console.log(err.stack)
14 }
15}

Here we set up the basic requirements for a Full API script.

PDFNet.PDFDoc.create() - Creates an empty placeholder PDF document. All Full API functions return a promise, which means that for functions which return a value or object, we need to add an await statement in front to guarantee that the object is resolved by the time we use it. Without adding an await statement, PDFNet.PDFDoc.create() will simply return a promise rather than the document we want.

To open an existing PDF document, call PDFNet.PDFDoc.createFromURL("path/to/pdfdoc.pdf"); instead.

doc.initSecurityHander() - Initializes security handler which is used to check for and handle password-locked PDFs.

doc.lock() - Locks all operations on the document in order to avoid editing conflicts from other processes. More information is available in the advanced guide.

Running our own code

Now that we have properly set up our document, we can call read/write operations on it.

await doc.getPageCount() - Returns a promise that contains the number of pages in the PDF document. Remember to use await to resolve the promise.

Testing

To test if everything is working correctly, we have an alert message at the end of our try block that will output the number of pages in our PDF doc.

Since we have not added any pages to our pdf doc, the number of pages will be 0. Try swapping out create() with createFromURL("path/to/pdfdoc.pdf") to test the getPageCount() function on an existing PDF document.

const doc = await PDFNet.PDFDoc.createFromURL("myfile.pdf");

Apryse Docs Image

Run SampleTest.html on a server. The "Test Complete!" alert box should pop up after a few moments.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales