Some test text!

Search
Hamburger Icon

Web / Guides

Open a document

There are a few ways to open a document such as from a URL, a blob, 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 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:

Features option

Constructor option

You can enable the file picker with Features at any point in time.
WebViewer(...)
  .then(instance => {
    const { Feature } = instance.UI;
    instance.UI.enableFeatures([Feature.FilePicker]);
  });

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

With a File Object

If you have the File object, from a file picker for example, you can pass the object directly to the loadDocument function. File objects are similar to Blobs and should load fine.

<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>

See more WebViewer events such as documentLoaded to understand when to execute API operations.

If you are loading from an array buffer, please ensure the extension or filename options are set while loading.

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.

Constructor option

API option

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

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.

Constructor option

API option

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

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.

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

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.

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

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+.
WebViewer(...)
  .then(instance => {
    instance.UI.loadDocument('https://myserver.com/myfile.pdf', {
      password: 'asdf',
      customHandlerId: 42
    });
  });

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

Get the answers you need: Chat with us