【问题标题】:pdf.js with local pdf filepdf.js 与本地 pdf 文件
【发布时间】:2013-03-10 03:31:54
【问题描述】:

我正在试用 pdf.js 库,只想在我的服务器上显示本地 pdf 文件,而不是示例提供的 pdf 文件。

<html>
<body>
  <canvas id="the-canvas" style="border:1px solid black"></canvas>

  <!-- Use latest PDF.js build from Github -->
  <script type="text/javascript" src="https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>

  <script type="text/javascript">
    //
    // NOTE:
    // Modifying the URL below to another server will likely *NOT* work. Because of browser
    // security restrictions, we have to use a file server with special headers
    // (CORS) - most servers don't support cross-origin browser requests.
    //
    var url = '/test.pdf';

    //
    // Disable workers to avoid yet another cross-origin issue (workers need the URL of
    // the script to be loaded, and dynamically loading a cross-origin script does
    // not work)
    //
    PDFJS.disableWorker = true;

    //
    // Asynchronous download PDF as an ArrayBuffer
    //
    PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {
      //
      // Fetch the first page
      //
      pdf.getPage(1).then(function getPageHelloWorld(page) {
        var scale = 1.5;
        var viewport = page.getViewport(scale);

        //
        // Prepare canvas using PDF page dimensions
        //
        var canvas = document.getElementById('the-canvas');
        var context = canvas.getContext('2d');
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        //
        // Render PDF page into canvas context
        //
        page.render({canvasContext: context, viewport: viewport});
      });
    });
  </script>

</body>
</html>

所以我将 pdf url 更改为我的本地 '/test.pdf' url。但是,这给了我一条消息,当它清楚地存在于我的根文件夹中时,它无法找到该文件。知道什么可能导致此错误吗?

【问题讨论】:

标签: html pdf pdf.js


【解决方案1】:

修改 pdfjs/web/viewer.js 文件并更改

var DEFAULT_URL = '<file path on your server>'

当我尝试在我的本地系统上实现 http://mozilla.github.com/pdf.js/web/viewer.html 的演示时,这很有效

【讨论】:

  • 你能从本地目录中的文件路径加载吗?我知道他们出于安全目的禁用了此功能,但我需要从特定文件路径中读取
猜你喜欢
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 2021-03-27
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
相关资源
最近更新 更多