> 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/open-save-document/open.md).

# Open and Load PDFs and Documents Using JavaScript

Learn how to efficiently open a document in WebViewer from a URL, blob, filesystem, or base64 data. Utilize loading options like initialDoc and loadDocument API for seamless file access. Enhance your

There are a few ways to open a document such as from a URL, a blob, arrayBuffer, the filesystem, or base64 data. Additionally, there are loading options to help WebViewer determine the type of the file being loaded.

{% tabs %}
{% tab title="URL" %}

## Opening a document in WebViewer from URL

If you have a URL for a document, you can open it by including [`initialDoc`](https://sdk.apryse.com/api/web/global.html#WebViewerOptions__anchor) as a constructor option or by using the [`loadDocument`](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor) API.

{% tabs %}
{% tab title="Constructor option" %}
Use the **initialDoc** constructor option to provide the document when mounting WebViewer.

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

```js
WebViewer({
  ...,
  initialDoc: 'https://myserver.com/myfile.pdf',
}, document.getElementById('viewer')).then(instance => {
    const { documentViewer } = instance.Core;
    documentViewer.addEventListener('documentLoaded', () => {
      // perform document operations
    });
});
```

{% endcode %}

[WebViewer Options](https://sdk.apryse.com/api/web/global.html#WebViewerOptions__anchor) [documentViewer](https://sdk.apryse.com/api/web/Core.html#.documentViewer__anchor)
{% endtab %}

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

```js
WebViewer({
  ...,
  initialDoc: 'https://myserver.com/myfile.pdf',
}, document.getElementById('viewer')).then(instance => {
    const { docViewer } = instance;
    docViewer.on('documentLoaded', () => {
      // perform document operations
    });
});
```

{% endcode %}

[WebViewer Options](https://sdk.apryse.com/api/web/global.html#WebViewerOptions__anchor) [docViewer](https://sdk.apryse.com/api/web/Core.html#.documentViewer__anchor)
{% endtab %}
{% endtabs %}

###

{% endtab %}

{% tab title="API option" %}
Use [loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor) to load documents with additional loading options after mounting WebViewer.

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

```js
WebViewer(...)
  .then(instance => {
    instance.UI.loadDocument('https://myserver.com/myfile.pdf', { filename: 'myfile.pdf' });
    const { documentViewer } = instance.Core;
    documentViewer.addEventListener('documentLoaded', () => {
      // perform document operations
    });
  });
```

{% endcode %}

[UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor) [documentViewer](https://sdk.apryse.com/api/web/Core.html#.documentViewer__anchor)
{% endtab %}

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

```js
WebViewer(...)
  .then(instance => {
    instance.loadDocument('https://myserver.com/myfile.pdf', { filename: 'myfile.pdf' });
    const { docViewer } = instance;
    docViewer.on('documentLoaded', () => {
      // perform document operations
    });
  });
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor) [docViewer](https://sdk.apryse.com/api/web/Core.html#.documentViewer__anchor)
{% endtab %}
{% endtabs %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
If you are loading from an extensionless URL, please ensure the [extension](#the-extension-option) or [filename](#the-filename-option) options are set.
{% endhint %}
{% endtab %}

{% tab title="Blob" %}

## Opening a document in WebViewer from Blob

If your document is already in [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob/) format you can pass the Blob object directly to loadDocument function.

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

```js
WebViewer(...)
  .then(instance => {
    // `myBlob` is your blob data which can come
    // from sources such as a server or the filesystem
    instance.UI.loadDocument(myBlob, { filename: 'myfile.pdf' });

    const { documentViewer } = instance.Core;
    documentViewer.addEventListener('documentLoaded', () => {
      // perform document operations
    });
  });
```

{% endcode %}

[UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#.loadDocument__anchor)
{% endtab %}

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

```js
WebViewer(...)
  .then(instance => {
    // `myBlob` is your blob data which can come
    // from sources such as a server or the filesystem
    instance.loadDocument(myBlob, { filename: 'myfile.pdf' });

    const { docViewer } = instance;
    docViewer.on('documentLoaded', () => {
      // perform document operations
    });
  });
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
If you are loading from a blob, please ensure the [extension](#the-extension-option) or [filename](#the-filename-option) options are set while loading.
{% endhint %}
{% endtab %}

{% tab title="ArrayBuffer" %}

## Opening a document from ArrayBuffer

If your document is already in [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/) format you can convert the ArrayBuffer object to a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob/) and then pass resulting Blob directly to loadDocument function.

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

```js
WebViewer(...)
  .then(instance => {

    // `arrayBuffer` is your buffer data which can come
    // from sources such as a server or the filesystem
    instance.UI.loadDocument(arrayBuffer, {
    // You need to specify either the file name of extension so
    // that WebViewer knows how to interpret the data
    filename: 'myfile.docx'
    
    // You can also change the InitialMode with WebViewer 11
    // initialMode: WebViewer.Modes.DOCX_EDITOR,
    });

    const { documentViewer } = instance.Core;
    documentViewer.addEventListener('documentLoaded', () => {
      // perform document operations
    });
  });
```

{% endcode %}

[UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}

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

```js
WebViewer(...)
  .then(instance => {

    // `arrayBuffer` is your buffer data which can come
    // from sources such as a server or the filesystem
    const arr = new Uint8Array(arrayBuffer);
    const blob = new Blob([arr], { type: 'application/pdf' });
    instance.UI.loadDocument(blob, { filename: 'myfile.pdf' });

    const { docViewer } = instance.Core;
    docViewer.on('documentLoaded', () => {
      // perform document operations
    });
  });
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
If you are loading from an array buffer, please ensure the [extension](#the-extension-option) or [filename](#the-filename-option) options are set while loading.
{% endhint %}
{% endtab %}

{% tab title="Filesystem" %}

## Opening a document in WebViewer from file system

## Enable File Browser Option

If you wish to allow users to open files from their local file system, you can enable this feature with:

{% tabs %}
{% tab title="Feature option" %}
You can enable the file picker with Features at any point in time.

{% code lineNumbers="true" %}

```js
WebViewer(...)
  .then(instance => {
    const { Feature } = instance.UI;
    instance.UI.enableFeatures([UI.Feature.FilePicker]);
  });
```

{% endcode %}

[UI.enableFeatures](https://sdk.apryse.com/api/web/UI.html#.enableFeatures__anchor) [Feature](https://sdk.apryse.com/api/web/UI.html#.Feature__anchor)

###

{% endtab %}

{% tab title="Constructor option" %}
You can also specify this via the WebViewer constructor at the very beginning.

{% code lineNumbers="true" %}

```js
WebViewer({
  ...
  enableFilePicker: true,
}).then(instance => {
    // ...
  });
```

{% endcode %}

[WebViewer Options](https://sdk.apryse.com/api/web/global.html#WebViewerOptions__anchor)
{% endtab %}
{% endtabs %}

And this option will be available to users in the top-right of the WebViewer UI:

![](https://3532544125-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX9YnTSKIHvV7m0A36LbO%2Fuploads%2Fgit-blob-0c9d146355ec5e2efe688be88ba43ac3ee852c91%2F4a16761c18d423f2fdb657fd2c0bc88844f66736-182x262.png?alt=media)

## With a File Object

If you have the [File](https://developer.mozilla.org/en-US/docs/Web/API/File/) object, from a file picker for example, you can pass the object directly to the loadDocument function. File objects are similar to [Blobs](/web/open-save-document/open/blob.md) and should load fine.

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

```js
<label for="file_upload">Choose A file</label>
<input type="file" id="file_upload" name="file_upload" accept=".pdf">

<div id='viewer' style='width: 1024px; height: 600px;'></div>

<script>
  const input = document.getElementById('file_upload');

  WebViewer(...)
    .then(instance => {
      input.addEventListener('change', () => {

        // Get the file from the input
        const file = input.files[0];
        instance.UI.loadDocument(file, { filename: file.name });
      });

      const { documentViewer } = instance.Core;
      documentViewer.addEventListener('documentLoaded', () => {
        // perform document operations
      });
    });
</script>
```

{% endcode %}

[UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}

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

```js
<label for="file_upload">Choose A file</label>
<input type="file" id="file_upload" name="file_upload" accept=".pdf">

<div id='viewer' style='width: 1024px; height: 600px;'></div>

<script>
  const input = document.getElementById('file_upload');

  WebViewer(...)
    .then(instance => {
      input.addEventListener('change', () => {

        // Get the file from the input
        const file = input.files[0];
        instance.loadDocument(file, { filename: file.name });
      });

      const { docViewer } = instance;
      docViewer.on('documentLoaded', () => {
        // perform document operations
      });
    });
</script>
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}
{% endtabs %}
{% endtab %}

{% tab title="Base64 data" %}

## Opening a document in WebViewer from base64 data

If you have the file data as a base64 string, the best way to load the document in WebViewer is to first convert it to a Blob and then load it as [described for Blobs](/web/open-save-document/open.md). Below is some example code showing how to convert base64 to a Blob.

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

```js
function base64ToBlob(base64) {
  const binaryString = window.atob(base64);
  const len = binaryString.length;
  const bytes = new Uint8Array(len);
  for (let i = 0; i < len; ++i) {
    bytes[i] = binaryString.charCodeAt(i);
  }

  return new Blob([bytes], { type: 'application/pdf' });
};

WebViewer(...)
  .then(instance => {
    // `myBase64String` is your base64 data which can come
    // from sources such as a server or the filesystem
    instance.UI.loadDocument(base64ToBlob(myBase64String), { filename: 'myfile.pdf' });

    const { documentViewer } = instance.Core;
    documentViewer.addEventListener('documentLoaded', () => {
      // perform document operations
    });
  });
```

{% endcode %}

[UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}

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

```js
function base64ToBlob(base64) {
  const binaryString = window.atob(base64);
  const len = binaryString.length;
  const bytes = new Uint8Array(len);
  for (let i = 0; i < len; ++i) {
    bytes[i] = binaryString.charCodeAt(i);
  }

  return new Blob([bytes], { type: 'application/pdf' });
};

WebViewer(...)
  .then(instance => {
    // `myBase64String` is your base64 data which can come
    // from sources such as a server or the filesystem
    instance.loadDocument(base64ToBlob(myBase64String), { filename: 'myfile.pdf' });

    const { docViewer } = instance;
    docViewer.on('documentLoaded', () => {
      // perform document operations
    });
  });
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}
{% endtabs %}

##

{% endtab %}
{% endtabs %}

See more [WebViewer events](/web/events/viewer-events.md) such as `documentLoaded` to understand when to execute API operations.

## Loading options

WebViewer provides various options to load a document. Whether you are loading a document through the `initialDoc` option in the constructor or calling `loadDocument` after mounting, you can always provide options to load your document.

### The extension option

When loading a document using a URL, WebViewer will use the URL to determine what type of file it is. For example `http://myserver.com/myfile.docx` ends with `.docx` so WebViewer will assume it's a docx file. However, what happens if you are loading from a blob or extensionless URL? That's where the `extension` option comes in.

You can use the `extension` option to explicitly tell WebViewer what type of file it is. For example, `extension: 'docx'` or `extension: 'png'`. By default WebViewer will assume it's a PDF file but that might not be correct.

**Via WebViewer Constructor**

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

```js
WebViewer({
  path: '../../../lib',
  initialDoc: 'http://<documentserver>/FileDownload?docId=foo',
  extension: 'docx',
  ...
}, document.getElementById('viewer'))
  .then(instance => {
    // ...
  });
```

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

[WebViewer Options](https://sdk.apryse.com/api/web/global.html#WebViewerOptions__anchor)

**Via API**

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

```js
WebViewer(...)
  .then(instance => {
    instance.UI.loadDocument('http://<documentserver>/FileDownload?docId=foo', {
      extension: 'docx'
    });

    //...
  });
```

{% endcode %}

[UI](https://sdk.apryse.com/api/web/UI.html) [UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}

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

```js
WebViewer(...)
  .then(instance => {
    instance.loadDocument('http://<documentserver>/FileDownload?docId=foo', {
      extension: 'docx'
    });

    //...
  });
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}
{% endtabs %}

### The filename option

The `filename` option is used to indicate the name of the file when you press the download button in the viewer. However, the option can also be used as a way of indicating the type of file you are opening when the URL does not end with a file extension. Note that the `extension` property has precedence over `filename` for determining the document type.

**Via WebViewer Constructor**

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

```js
WebViewer({
  path: '../../../lib',
  initialDoc: 'http://<documentserver>/FileDownload?docId=foo',
  filename: 'report.docx',
  ...
}, document.getElementById('viewer'))
  .then(instance => {
    // ...
  });
```

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

[WebViewer Options](https://sdk.apryse.com/api/web/global.html#WebViewerOptions__anchor)

**Via API**

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

```js
WebViewer(...)
  .then(instance => {
    instance.UI.loadDocument('http://<documentserver>/FileDownload?docId=foo', {
      filename: 'report.docx'
    });

    //...
  });
```

{% endcode %}

[UI](https://sdk.apryse.com/api/web/UI.html) [UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}

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

```js
WebViewer(...)
  .then(instance => {
    instance.loadDocument('http://<documentserver>/FileDownload?docId=foo', {
      filename: 'report.docx'
    });

    //...
  });
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}
{% endtabs %}

### The customHeaders option

If your document server requires additional options in the HTTP request, you can use the second argument in `loadDocument` function to pass them. This is especially useful when you need the `Authorization` header in the request with your auth key.

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

```js
WebViewer(...)
  .then(instance => {
    instance.UI.loadDocument('https://myserver.com/myfile.pdf', {
      customHeaders: {
        Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
      }
    });
  });
```

{% endcode %}

[UI](https://sdk.apryse.com/api/web/UI.html) [UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}

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

```js
WebViewer(...)
  .then(instance => {
    instance.loadDocument('https://myserver.com/myfile.pdf', {
      customHeaders: {
        Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
      }
    });
  });
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}
{% endtabs %}

### The password option

For password-protected documents, it can be provided during the `loadDocument` call via the options argument. It can either take the password string or a function that will eventually provide a password via a callback provided as a parameter.

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

```js
WebViewer(...)
  .then(instance => {
    instance.UI.loadDocument('https://myserver.com/myfile.pdf', {
      password: 'asdf'
    });
  });
```

{% endcode %}

[UI](https://sdk.apryse.com/api/web/UI.html) [UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}

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

```js
WebViewer(...)
  .then(instance => {
    instance.loadDocument('https://myserver.com/myfile.pdf', {
      password: 'asdf'
    });
  });
```

{% endcode %}

[WebViewerInstance.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)
{% endtab %}
{% endtabs %}

### The customHandlerId option

An Apryse [custom security handler](/web/security/custom-handler.md) easily encrypts and secures documents with our algorithm to allow opening the document only in WebViewer (or PDFNet).

For these types of documents, it is necessary to provide the integer custom handler ID along with the password.

{% hint style="warning" %}
This is only available to WebViewer 8.2+.
{% endhint %}

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

```js
WebViewer(...)
  .then(instance => {
    instance.UI.loadDocument('https://myserver.com/myfile.pdf', {
      password: 'asdf',
      customHandlerId: 42
    });
  });
```

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

[UI](https://sdk.apryse.com/api/web/UI.html) [UI.loadDocument](https://sdk.apryse.com/api/web/UI.html#loadDocument__anchor)

If you run into any issues loading a document, please visit our [FAQ for loading errors.](/web/get-started/faq/loading-errors.md)


---

# 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/open-save-document/open.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.
