Attempting to use loadDocument results in "PDF header not found" error

WebViewer Version: 6.14.16

I am trying to view PDF files in the WebViewer from an API service in a React application. Here is a copy of my code

            if (viewerInstance){
                viewerInstance.UI.loadDocument(props.fileData.filePath, {extension: 'pdf'})
            }else{
                WebViewer({
                    path: 'lib/pdftron',
                    initialDoc: props.fileData.filePath,
                    extension: 'pdf'
                }, viewerDiv.current as HTMLDivElement).then(instance => {
                    setViewerInstance(instance);
                });
            }

When I initially create the WebViewer, it works fine. But when I change the filePath to a different file, I get the following error:

Exception:
Message: PDF header not found. The file is not a valid PDF document.
Filename:
Function: Skipheader
Linenumber:

This happens even if I switch the filePath back to what the WebViewer was initially created with, when the file loaded without issue.

Additionally, this doesn’t happen when I am running both the React server and the API on my own machine under localhost. This only happens when we deploy them to a server, like our test server.

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,

Thank you for contacting WebViewer’s support. This error normally shows up when WebViewer tries to open a file as a PDF but can’t (i.e. trying to open a .docx file as a PDF). Would it be possible to get a screenshot of your browser developer console network tab and what kind of response you are getting back from your server? Normally you should see something like the following

Screen Shot 2022-04-07 at 4.21.45 PM

It’s possible you aren’t loading a PDF document or you are getting some unexpected data back from your server. It’s interesting that changing the value back to the initial value still give issues, it could be your browser is caching the data somewhere

Best Regards,
Andrew Yip
Software Developer
PDFTron Systems, Inc.

I found the problem. The path I’m passing in starts with “./” so loadDocument() is inserting “lib/pdfTron/ui” (the path to the copy of the library I created) into the URL.

Hi,

I’m glad you were able to solve the issue. Feel free to let us know if you have any other issues.

Best Regards,
Andrew Yip
Software Developer
PDFTron Systems, Inc.

Hi Mark

Do you mind Sharing how you changed the path to i’m facing the same issue as you posted a few months ago

Sure. I changed the code to this

            if (viewerInstance){
                const fixedPath = props.fileData.filePath.replace(/^\./,'')
                viewerInstance.UI.loadDocument(fixedPath, {extension: 'pdf'})
            }else{

Basically just stripping off any initial ‘.’ that the path might have.