Building my own UI doesn't work - no worker file

Which product are you using?

PDF.js Express 8.3.0

Detailed description of issue
I want to create my own UI with @pdftron/pdfjs-express (8.3.0) and integrate it with React (17.0.2). I followed this page, declared webviewer-core.min.js script in index.html template. When the script runs, an error appears. It says that there is no worker. In fact, the script needs to load /pdf/PDFworker.js, but there is no such a file in the library. I’ve found worker file like /pdfjs/pdf.worker.js and even tried to use it as PDFworker.js, but it made another error, so I may guess that file is incorrect. My question is: where to find the missing worker file and what exactly should it be?

I had used pdftron before and everything had been working great until I changed the library to PDF.js Express.

Expected behaviour
Building my own UI with the library, but at least to load pdf file in a correct way.

Does your issue happen with every document, or just one?
Every document

Link to document

Code snippet
I followed this page after changing library from pdftron.

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi there,

PDF.js Express does not use the PDFTron workers, so importing those will not work.

The files you need to import are:

  • core/webviewer-core.min.js
  • core/pdfjs/PDFJSDocumentType.js (this is the PDF.js Express version of the workers)

I’ll update that guide to make that a bit more clear. Sorry for the confusion.

Let me know if you have any other issues with this.

Thanks!
Logan

Hi Logan,

Thanks for your advice. I followed your instruction, I used the proper files. Now PDFJSDocumentType.js raises the following error in a console:

Cannot read properties of undefined (reading 'getAnnotationManager')

from window.documentViewer.getAnnotationManager(). I could define window.documentViewer as my documentViewer instance, but probably it’s not the correct way, because then the next error occures from the same PDFJSDocumentType.js file:

Cannot read properties of undefined (reading 'addSearchListener')

from window.instance.addSearchListener(...) and I have no idea what “instance” should be.

In the instruction here there is an “instance”, but I don’t know what it is. This one is not delivered from the scripts loaded in index.html, that you recommended previously. In the instruction Core is taken from the instance: instance.Core and in pdftron it was taken like that: window.Core. That works also here, Core is available from window.

My example code:

const App = () => {
  const viewer = useRef(null);

  useEffect(() => {
    const Core = window.Core;
    Core.setWorkerPath('webviewer/lib/core'); // this one is set OK, files are loaded correctly
    const documentViewer = new Core.DocumentViewer();
    // window.documentViewer = documentViewer; // this one is only to see if another error occurs

    documentViewer.setScrollViewElement(document.getElementById('scroll-view'));
    documentViewer.setViewerElement(document.getElementById('viewer'));
    documentViewer.loadDocument('/files/test.pdf').then(instance => {
      console.log('Document loaded', instance); // this line never runs because of errors
    });
  }, []);

  return (
    <div className="App">
      <div className="header">React sample</div>
      <div className="webviewer" ref={viewer} />
    </div>
  );
};

Could you help me with this problem?

Hi @Logan we are also invesigating building our own UI with pdf.js express and having the same problems as @dorota.k

Any help would be appreciated

Thanks,

Tom

Hi @Logan,

I have also tried to follow the guide here Core Engine for JavaScript PDF Viewer | PDF.js Express SDK and ended up with the same issues as @dorota.k

Can you provide a working sample with the required files from PDFJS Express library?

Thanks,
Jeff

Hey everyone,

I will get a sample set up showing how to build your own UI. It seems like our guides drifted a bit out of date in the latest releases and I apologize for that. I’ll have an updated sample ready shortly.

Thanks!
Logan

Hey guys - update.

I found some internal code that we use that relies on the default UI being loaded (which is a mistake on our part), which is why those errors are being throw. I’m going to make some changes internally to make this a bit easier. I’ll push a new release with those changes in the next few days, along with the sample I promised.

Thank you for your patience here,

Logan

3 Likes

Hi Logan,

That is great news, I’ll really appreciate the new release and sample.

Looking forward to that, thanks,
Dorota

Any news on a fix for this?

Thanks,
Jeff

Hey guys,

Just waiting for my code to get through code review - sorry for the delay.

Logan

Hi everyone!

I just pushed out version 8.4 of Express which fixes that instance error you were seeing. I also updated the build your own UI guide and created this sample for reference.

Let me know if you have any issues.

Logan

1 Like

Hi Logan,

This is working now thanks. How do you switch the viewer to page by page mode rather than continuous scrolling?

Documentation refers to setLayoutMode but I think this is feature of the UI instance rather than standalone core viewer.
https://pdfjs.express/api/WebViewerInstance.html#setLayoutMode__anchor

Is there a way to set it in the window.documentViewer?
I have tried enableRightToLeftPageRendering() but that doesnt seem to work.

I can also see DisplayModeManager in the documentation and DisplayMode but there are no examples of its usage. The parameter mode is a string but I don’t see any definition of the valid values.

Any help would be appreciated, thanks.

Thanks,
Jeff

Nevermind I found out how to do it using DisplayModeManager

1 Like