PDF WebViewer loads/shows PDF only once

Which product are you using?

PDF.js Express Version

Detailed description of issue
In my VueJs (NuxtJs SSR enabled) application I open the viewer multiple times on the same page as the user selects different documents. The document is loaded from a byte array. It loads and shows the PDF for the first time in a Dialog control (a primevue control), however for every consecutive try to open another PDF it throws runtime exception as t is null or calling addEventListener on null or undefined. The strange part is the same functionality works when in development mode but fails in build mode.

Expected behaviour
{Provide a screenshot or description of the expected behaviour}

Does your issue happen with every document, or just one?
1 document loads but cannot open another document unless whole page is refreshed.

Link to document
{Provide a link to the document in question if possible}

The below code runs on success of an API which returns the base64 encoded PDF bytes. Hence this code gets called on every hyperlink click on the UI. All the data is supplied correctly to the viewer. As I said it works perfect in DEV mode, however fails in build mode with errors thrown from the viewer control.
Code snippet
(async () => {
const _c64 = j.data.contentAsBase64
const WebViewer = await import(‘@pdftron/webviewer’)
const webDef = WebViewer.default
webDef({ path: ‘/pdfjs’, licenseKey: ‘my-license-key’ }, document.getElementById(‘webviewer’)).then((instance) => {
instance.UI.loadDocument(_self.base64ToBlob(_c64), { filename: ‘file.pdf’ })
})
})()

Hi there,

Thank you for contacting pdf.js express forums,

Are you using the latest 8.7 version of pdf.js express?

Does the first document load have any console errors? Can you try running .closeDocument
PDFJS Express WebViewer Class: DocumentViewer before opening another one?

Best regards,
Kevin Kim

Hi,
First document opening does not show any error in the console. The 2 dependencies in my package.json are as:

"@pdftron/pdfjs-express-viewer": "^8.7.0",
"@pdftron/webviewer": "^8.11.0",

I am observing a new behavior now. I thought of not opening the pdf viewer inside the dialog on the same page and instead open it in a new window. Assuming it will open everytime in a new browser tab. However, the PDF loads the first time a new tab opens. Now if I click on another PDF it opens the new tab but the PDF does not load. And the strange part is :::: if I close the first tab window, the 2nd PDF which did not open early loads correctly… I am not getting it, why this should happen in a new window?

Appreciate you help.

Regards,
Amod

Hi,

Where do I add the closedocument? To test I have added it immediately after loading it like shown below. It did not help. Is that syntax correct?

(async () => {
          const _c64 = j.data.contentAsBase64
          const WebViewer = await import('@pdftron/webviewer')
          const webDef = WebViewer.default
          webDef({ path: '/pdfjs', licenseKey: 'my-license' }, 
            document.getElementById('webviewer')).then((instance) => {
            const { Core } = instance
            instance.UI.loadDocument(_self.base64ToBlob(_c64), { filename: 'file.pdf' })
            Core.DocumentViewer.closeDocument()
          })
        })()

Appreciate your support

Regards,
Amod

Hi,

I added the closeDocument and dispose. But still no luck.

Screenshot 1 - PDF loads the first time. As you see on top I added to buttons to check the behavior. Once the PDF loads, i press Close, then dispose. That removes the PDF from the viewer. Viewer stays.

Regards,
Amod

Hi,

Couldn’t upload 2 images in a post earlier,

continued…

Screenshot 2

Now I click on another hyperlink and it does not load the viewer back as in the above image.
There must be something more that removes the viewer/pdf completely ?? I am guessing once that is done it will give way to the next PDF. As I have mentioned above it does not work even if I try to open PDFs in multiple tabs at the same time. Only when I close the loaded PDFs it gives way to the next 1 in queue

Hope this helps.

Regards,
Amod

Hi,

Version info and the error while loading the first pdf. Though I can view it, it shows this in the console.

Hope this helps.
Regards,
Amod

Hi,

I have understood the issue. The DIV hosting the control loads a little later than the call to the webviewer constructor. As a result, the DIV at that time is NULL and hence the error. And strangely this happens 2nd time onwards when the viewer is opened. But you might still want to see why it did not open in different tabs until 1 of the viewers that opened first was closed. That’s strange.

Thank you for your support !!

Regards,
Amod

Hi there,

Thank you for your update,

I want to note that @pdftron/webviewer and @pdftron/pdfjs-express are different packages. We do not support @pdftron/webviewer on this forum, and you will have to contact PDFTron support for any further support regarding @pdftron/webviewer

Best regards,
Kevin Kim