Where should PDFNet APIs be called when they are used with WebViewer?

440 views
Skip to first unread message

David Tippett

unread,
Aug 16, 2016, 8:01:30 PM8/16/16
to PDFTron WebViewer
Q: What is the best location to call PDFNet APIs when used in conjunction with WebViewer?

A: The recommended place to use PDFNet APIs is in your custom config file passed to WebViewer. For example if you created your WebViewer instance using the following:

var myWebViewer = new PDFTron.WebViewer({
type: "html5",
path: "../../../lib",
initialDoc: "../TestFiles/newsletter.pdf",
config: 'config.js',
documentType: "pdf",
showLocalFilePicker: true,
enableAnnotations: true,
pdfnet: true
}, viewerElement);

PDFNet functions would be accessible in the config file "config.js".

Note that this is recommended because using including PDFNet.js in the same frame as the WebViewer object can easily lead to undesirable behaviour. In particular it is very easy to accidentally create two PDFNetJS worker backends: one when calling PDFNet.initialize() in the context where WebViewer is created (the parent frame) and one in WebViewer's internal iframe. (the child frame) This can lead to an unfortunate set of issues because:

1. Multiple workers will consume more memory and may lead to memory exhaustion on some browsers.
2. Documents created in one context are cannot be used in the other context. (so getting the document from WebViewer's internal iframe and trying to use it in the outer iframe would lead to an error)

Using the config file will avoid these issues because it is loaded in WebViewer's internal iframe.
Reply all
Reply to author
Forward
0 new messages