Some test text!

Best practices

Contents

Preload WebViewer
Loading documents with loadDocument
Linearize PDF files
Support range requests
Keep original files
Support serving with Content-Encoding
Storing annotations

linkPreload WebViewer

If your website doesn't need to display the viewer immediately you can improve performance by preloading WebViewer so that it's immediately ready when you want to display a document. To do this you can load WebViewer without a document and hide the viewer. When it's time to display WebViewer you can simply unhide the element and call loadDocument.

Don't use "display: none" to hide the element
This causes issues in Firefox. See here for a workaround.

The reason this is worthwhile is that when WebViewer is instantiated it needs to load a number of JavaScript and CSS files and initialize the viewer UI. Depending on the network connection this can take a non-trivial amount of time which happens in the background when preloading.

linkLoading documents with loadDocument

If you want to load multiple documents in your app you'll want to reuse WebViewer. This is better than removing the old iframe and creating a new instance of WebViewer because some browsers have trouble with this and may leak memory.

To reuse WebViewer just keep a reference to your WebViewer instance and call loadDocument with the new document URL. The old document will be removed from the viewer (automatically cleaning up its resources) and the new document will be loaded in.

Note that if you are using the serverUrl option for loading annotations it's important to specify the documentId for each document so your server can determine which annotations should be returned. See more information here.
var myWebViewer = new PDFTron.WebViewer({
  ...,
}, viewerElement);

viewerElement.addEventListener('ready', function() {
  loadDocumentButton.on('click', function() {
    myWebViewer.loadDocument('mysite.com/myDocument.pdf', { documentId: 'id2' });
  });

  loadAnotherDocumentButton.on('click', function() {
    myWebViewer.loadDocument('mysite.com/myOtherDocument.pdf', { documentId: 'id2' });
  });
});

linkLinearize PDF files

When viewing PDFs directly WebViewer is able to start rendering pages earlier when a PDF is linearized. Linearized PDF files are also known as "Fast Web View" which is often indicated in the document properties dialog of your favorite desktop PDF reader. You can use PDFTron SDK on your server to ensure PDF files are linearized.

linkSupport range requests

Range requests allow the browser to request specific bytes from a URL. For WebViewer to efficiently download and start rendering documents quickly it's important that the server hosting the document file (for example PDF) supports these requests.

When serving files statically from a server this generally works without any configuration. However if you're serving the files in some other way you may need to use a library to handle the range requests for you.

The range request specification isn't very complicated so it's also possible for you to implement the handling yourself.

linkKeep original files

If you are doing file conversions of any type then you should always keep track of the original source file. For example, if you allow your customers to upload various file types (Office, images, text) and you convert them all to PDF, then you should still retain the original file, and maintain in your database links between the original and converted file. This ensures maximum flexibility and maintenance.

linkSupport serving with Content-Encoding

If viewing PDF or Office document directly, it's recommended that your server supports serving the larger worker files as pre-compressed brotli or gzip files. You can read more about this here.

linkStoring annotations

WebViewer provides example implementations of annotation handlers for a few different languages which store all annotations as a single XFDF file on your server. This is one way to implement annotation handling, however if you have multiple users accessing the same document then timing issues can occur with one user overwriting the annotations of another.

One way to handle this to store the XFDF data for each annotation separately, for example as rows in a database. Then when WebViewer requests the annotation data for a document you can run a query to get all the annotations with that document id, append the data together and send it back to WebViewer.

This is similar to the approach that the realtime collaboration guide takes although that sample goes further and updates the data in real time. The important part to take away from it is the use of getAnnotCommand and importAnnotCommand to save and update the changes.

Get the answers you need: Support

Contents

Preload WebViewer
Loading documents with loadDocument
Linearize PDF files
Support range requests
Keep original files
Support serving with Content-Encoding
Storing annotations