pdf.js与本地pdf文件 [英] pdf.js with local pdf file

查看:236
本文介绍了pdf.js与本地pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying out the pdf.js library and just want to display a local pdf file on my server instead of the pdf file provided by the example.

<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'网址。但是,这给我的消息,当它明确存在于我的根文件夹中时找不到该文件。任何想法可能会导致此错误?

So I changed there pdf url to my local '/test.pdf' url. However this gives me the message it cannot find the file when it's clearly there in my root folder. Any idea what could cause this error?

推荐答案

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

modify pdfjs/web/viewer.js file and change the

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

当我试图在我的本地系统上的http://mozilla.github.com/pdf.js/web/viewer.html

这篇关于pdf.js与本地pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆