"Missing PDF" every time I use Grapecity PDF Viewer

1.2k Views Asked by At

I'm building an app that will have an interactive PDF form on a server (in HTML, CSS, JS). I have been trying to use the Grapecity PDF viewer, but to no avail. I've followed the documentation to a T, using these resources: one, two, three.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <title>GC Viewer Demo | PDF Plugin</title>
    <link rel="stylesheet" href="https://cdn.materialdesignicons.com/2.8.94/css/materialdesignicons.min.css">
    <script>
        function loadPdfViewer(selector) {
            var viewer = new GcPdfViewer(selector, { renderInteractiveForms: true });
            viewer.addDefaultPanels();
            viewer.open("HelloWorld.pdf");
        }
    </script>
  </head>
  <body onload="loadPdfViewer('#root')">
    <div id="root"></div>
    <script type="text/javascript" src="gcpdfviewer.js"></script>
  </body>
</html>

I currently have the "HelloWorld.pdf" and the gcpdfviewer javascripts in the same folder as the above index.html but every time I test the code in the browser, the PDF viewer loads, but the PDF doesn't, giving me an error that states "missing PDF."

This is really bothering me because the PDF is exactly where it's supposed to be, I think.

I'm currently not using a license key, but the documentation makes it seems like I don't need one. Maybe that's the issue.

Any ideas?

Edit - Here are the console errors in Chrome:

The pdf worker has been disabled. Note, rendering PDF in foreground thread can slow pdf viewer performance.
ce @ gcpdfviewer.js:1
index.html:1 Access to XMLHttpRequest at 'file:///C:/HelloWorld.pdf' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
HelloWorld.pdf:1 Failed to load resource: net::ERR_FAILED
index.html:1 Uncaught (in promise) V
3

There are 3 best solutions below

0
Dmitri On BEST ANSWER

As stated earlier, for security reasons, it is not possible to access files on your local filesystem via JavaScript, you need to set up a web server and open PDF files using the web server url.

But if you really want to do it, there is another workaround - start Chrome with disabled web security, and then open the index.html page from the local file system, for example:

"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:/temp/CustomChromeSession" --disable-web-security "file:///C:/temp/gcpdfviewer-test/index.html"

Note, this workaround is not recommended due to security reasons and this approach can be disabled by browser developers later.

Here's a screenshot of how it works

3
MESCIUS Team On

Thank you for using GCPDF Viewer. Is the filename exactly the same, it might be case-sensitive OS? Can you look in the browser network tab and watch for the request going to retrieve the PDF file, is it looking in the same location where you have placed the file. which server software are you running? is it serving the PDF file?

http://www.grapecity.com

0
Manish Gupta On

Firstly, the issue is not because of the non-license version. The issue occurs because you are trying to execute the sample locally using the file system. To overcome this issue, you should host the application on a local server and the Pdf will be loaded in the PdfViewer. While loading a Pdf in the PdfViewer, there is XMLHttpRequest which checks the origin. This is null in the case of the file system. Hence, throws the error on accessing the file. Here is the documentation link for configuring the PdfViewer: https://www.grapecity.com/documents-api-pdf/docs/online/view-pdf.html

Regards,

Manish Gupta