【问题标题】:"Missing PDF" every time I use Grapecity PDF Viewer每次我使用 Grapecity PDF Viewer 时都会“丢失 PDF”
【发布时间】:2020-12-06 02:46:04
【问题描述】:

我正在构建一个应用程序,它将在服务器上具有交互式 PDF 表单(HTML、CSS、JS)。我一直在尝试使用 Grapecity PDF 查看器,但无济于事。我已经按照文档进行了 T,使用以下资源:onetwothree

<!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>

我目前将“HelloWorld.pdf”和 gcpdfviewer javascripts 放在与上述 index.html 相同的文件夹中,但每次我在浏览器中测试代码时,PDF 查看器都会加载,但 PDF 没有,给出我的错误提示“缺少 PDF”。

这真的让我很困扰,因为我认为 PDF 正是它应该在的地方。

我目前没有使用许可证密钥,但文档表明我似乎不需要。也许这就是问题所在。

有什么想法吗?

编辑 - 以下是 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

【问题讨论】:

    标签: javascript html pdf server grapecity


    【解决方案1】:

    首先,问题不是因为非授权版本。 出现此问题的原因是您尝试使用文件系统在本地执行示例。为了克服这个问题,您应该将应用程序托管在本地服务器上,并且 Pdf 将加载到 PdfViewer 中。 在 PdfViewer 中加载 Pdf 时,有 XMLHttpRequest 检查来源。在文件系统的情况下这是空的。因此,在访问文件时抛出错误。 这是配置 PdfViewer 的文档链接: https://www.grapecity.com/documents-api-pdf/docs/online/view-pdf.html

    问候,

    马尼什·古普塔

    【讨论】:

      【解决方案2】:

      感谢您使用 GCPDF 查看器。文件名是否完全相同,可能是区分大小写的操作系统? 您能否在浏览器网络选项卡中查看要检索 PDF 文件的请求,它是否在您放置文件的同一位置查找。 您正在运行哪个服务器软件?它是否提供 PDF 文件?

      http://www.grapecity.com

      【讨论】:

      • 感谢您的回复!我用 Chrome 控制台错误编辑了我的原始帖子。我相信如果有问题,它与服务器有关。诚然,这是我的第一个此类项目 - 我假设我必须设置一个服务器并在其上托管 pdf 文档?我将如何让我的代码通过 GCPDF Viewer 与服务器交互?
      • 我目前正在本地做所有事情。
      • 当我尝试使用 Firefox 时,我收到此错误而不是“缺少 PDF”:GcPdfFile: Stream must have data
      【解决方案3】:

      如前所述,出于安全原因,无法通过 JavaScript 访问本地文件系统上的文件,您需要设置 Web 服务器并使用 Web 服务器 url 打开 PDF 文件。

      但如果你真的想这样做,还有另一种解决方法——在禁用网络安全的情况下启动 Chrome,然后从本地文件系统打开 index.html 页面,例如:

      "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"
      

      请注意,出于安全原因,不建议使用此解决方法,浏览器开发人员可以稍后禁用此方法。

      Here's a screenshot of how it works

      【讨论】:

        猜你喜欢
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 2019-11-13
        • 1970-01-01
        相关资源
        最近更新 更多