pdf.js 与本地 pdf 文件

2024-02-28

我正在尝试 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。然而,这给了我一条消息,当它明显位于我的根文件夹中时,它找不到该文件。知道什么可能导致此错误吗?


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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pdf.js 与本地 pdf 文件 的相关文章

随机推荐