Open a document

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.

Opening a document in WebViewer from Blob

If your document is already in Blob format you can pass the Blob object directly to loadDocument function.

1WebViewer(...)
2 .then(instance => {
3 // `myBlob` is your blob data which can come
4 // from sources such as a server or the filesystem
5 instance.UI.loadDocument(myBlob, { filename: 'myfile.pdf' });
6
7 const { documentViewer } = instance.Core;
8 documentViewer.addEventListener('documentLoaded', () => {
9 // perform document operations
10 });
11 });

If you are loading from a blob, please ensure the extension or filename options are set while loading.

See more WebViewer events 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

JavaScript

1WebViewer({
2 path: '../../../lib',
3 initialDoc: 'http://<documentserver>/FileDownload?docId=foo',
4 extension: 'docx',
5 ...
6}, document.getElementById('viewer'))
7 .then(instance => {
8 // ...
9 });

Via API

1WebViewer(...)
2 .then(instance => {
3 instance.UI.loadDocument('http://<documentserver>/FileDownload?docId=foo', {
4 extension: 'docx'
5 });
6
7 //...
8 });

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

JavaScript

1WebViewer({
2 path: '../../../lib',
3 initialDoc: 'http://<documentserver>/FileDownload?docId=foo',
4 filename: 'report.docx',
5 ...
6}, document.getElementById('viewer'))
7 .then(instance => {
8 // ...
9 });

Via API

1WebViewer(...)
2 .then(instance => {
3 instance.UI.loadDocument('http://<documentserver>/FileDownload?docId=foo', {
4 filename: 'report.docx'
5 });
6
7 //...
8 });

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.

1WebViewer(...)
2 .then(instance => {
3 instance.UI.loadDocument('https://myserver.com/myfile.pdf', {
4 customHeaders: {
5 Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
6 }
7 });
8 });

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.

1WebViewer(...)
2 .then(instance => {
3 instance.UI.loadDocument('https://myserver.com/myfile.pdf', {
4 password: 'asdf'
5 });
6 });

The customHandlerId option

An Apryse custom security handler 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.

This is only available to WebViewer 8.2+.

JavaScript

1WebViewer(...)
2 .then(instance => {
3 instance.UI.loadDocument('https://myserver.com/myfile.pdf', {
4 password: 'asdf',
5 customHandlerId: 42
6 });
7 });

If you run into any issues loading a document, please visit our FAQ for loading errors.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales