pdf.js如何使用本地PDF文件?

20
我正在尝试使用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',但是它提示找不到文件,明明在我的根目录下。你知道这个错误可能是什么原因吗?


你找到答案了吗? - Asim Khan
正在处理 - 请参考 https://stackoverflow.com/questions/64541680/is-it-possible-to-convert-local-pdf-to-html-2b-improved-using-pdf-js。 - Tom
1个回答

1

1
你能从本地目录中的文件路径加载吗?我知道出于安全目的他们禁用了这个功能,但我需要读取特定的文件路径。 - Daredevil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接