> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/full-api/running-without-viewer.md).

# Running Full APIs without viewer

Learn how to set up WebViewer with this step-by-step guide. Download, unzip, and move files to your server. Use ES7 JavaScript async/await for Full API setup. Follow along for a seamless integration.

## 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).

![](https://3532544125-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX9YnTSKIHvV7m0A36LbO%2Fuploads%2Fgit-blob-a9425a8de9a5b696c92563114c7a2a2e253e90fd%2F23d501b2ebeacb1e64ef82c1609e0ea511c60334-377x97.png?alt=media)

{% hint style="info" %}
This guide uses ES7 JavaScript async/await. For more information on async/await, refer to these online explanations.
{% endhint %}

* [JavaScript Async Functions](https://web.dev/articles/async-functions)

## Setting up your HTML document

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

{% tabs %}
{% tab title="HTML" %}
{% code lineNumbers="true" %}

```html
<!DOCTYPE html>
<html>
<head>
  <!-- for WebViewer versions before version 8 this would be WebViewer/lib/core/CoreControls.js -->
  <script src="WebViewer/lib/core/webviewer-core.min.js"></script>
  
  <script src="WebViewer/lib/core/pdf/PDFNet.js"></script>
  <script src="WebViewer/samples/FileSaver.min.js"></script>
  <script src="WebViewer/samples/full-apis/Setup.js"></script>
</head>
<body>
  <script src="SampleTest.js"></script>
</body>
</html>
```

{% endcode %}
{% endtab %}
{% endtabs %}

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:

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}

<pre class="language-js" data-line-numbers><code class="lang-js">(async function() {
  "use strict";
  const licenseKey = '<code class="expression">visitor.claims.wvKey || "YOUR_LICENSE_KEY"</code>';

  // the path to where the PDF worker files are
  Core.setWorkerPath('WebViewer/lib/core');

  async function main() {
    // To be filled
  }

  await PDFNet.runWithCleanup(main, licenseKey);
  // Alt: PDFNet.runWithoutCleanup(main, licenseKey);
  // 'runWithoutCleanup' does not deallocate anything after finishing.
})();
</code></pre>

[Core.setWorkerPath](https://sdk.apryse.com/api/web/Core.html#.setWorkerPath__anchor) [PDFNet.runWithCleanup](https://sdk.apryse.com/api/web/Core.PDFNet.html#.runWithCleanup__anchor)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}

<pre class="language-js" data-line-numbers><code class="lang-js">(async function() {
  "use strict";
  const licenseKey = '<code class="expression">visitor.claims.wvKey || "YOUR_LICENSE_KEY"</code>';

  // the path to where the PDF worker files are
  CoreControls.setWorkerPath('WebViewer/lib/core');

  async function main() {
    // To be filled
  }

  await PDFNet.runWithCleanup(main, licenseKey);
  // Alt: PDFNet.runWithoutCleanup(main, licenseKey);
  // 'runWithoutCleanup' does not deallocate anything after finishing.
})();
</code></pre>

[CoreControls.setWorkerPath](https://sdk.apryse.com/api/web/Core.html#.setWorkerPath__anchor) [PDFNet.runWithCleanup](https://sdk.apryse.com/api/web/Core.PDFNet.html#.runWithCleanup__anchor)
{% endtab %}
{% endtabs %}

In this script our code is wrapped inside an [immediately invoked function expression](https://developer.mozilla.org/en-US/docs/Glossary/IIFE/). 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](/web/full-api/advanced-features.md) 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.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  try {
    // creates an empty pdf document
    const doc = await PDFNet.PDFDoc.create();
    doc.initSecurityHandler();
    // Locks all operations on the document
    doc.lock();

    // insert user code after this point
    const pgnum = await doc.getPageCount();
    alert(`Test Complete! Your file has ${pgnum} pages`);
  } catch(err) {
    console.log(err.stack)
  }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDFDoc.create](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.create__anchor) [PDFDoc.initSecurityHandler](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#initSecurityHandler__anchor) [PDFDoc.lock](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#lock__anchor) [PDFDoc.getPageCount](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getPageCount__anchor)

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](/web/full-api/advanced-features.md).

## 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");`

![](https://3532544125-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX9YnTSKIHvV7m0A36LbO%2Fuploads%2Fgit-blob-defbdbd3dbae266c352d690bf4da147abe0b935f%2F45ac3ffb63d130f65ae22a860d1e9338feab874c-395x119.png?alt=media)

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/full-api/running-without-viewer.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
