【问题标题】:pdfjs to img/canvas, only works in FireFoxpdfjs 到 img/canvas,仅适用于 FireFox
【发布时间】:2016-01-03 09:50:46
【问题描述】:

我使用 pdf.js 库从客户端上的 PDF 页面制作图像/画布。但它只适用于 FireFox,不适用于 Chrome 或 IE10+。我环顾四周,但找不到好的答案或解决方案。

我读到 IE 或 Chrome 上的 pdf.js 需要一个网络服务器,但是当我尝试 JSFiddle 上的代码时,它仍然无法正常工作。

这里是一个例子:https://jsfiddle.net/aqxwsfjo/2/

var url = 'https://raw.githubusercontent.com/mozilla/pdf.js/master/examples/helloworld/helloworld.pdf';

PDFJS.disableWorker = true;

PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {

    pdf.getPage(1).then(function getPageHelloWorld(page) {
        var scale = 1.5;
        var viewport = page.getViewport(scale);


        var canvas = document.getElementById('the-canvas');
        var context = canvas.getContext('2d');
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        page.render({canvasContext: context, viewport: viewport})


    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/mozilla/pdf.js/master/web/compatibility.js"></script>
<script src="https://raw.githubusercontent.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>


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

【问题讨论】:

    标签: jquery image pdf html5-canvas pdfjs


    【解决方案1】:

    看起来 GitHub 在返回带有 Content-Type: text/plain 的原始文件时添加了标题 X-Content-Type-Options: nosniff。查看相关问题:Link and execute external JavaScript file hosted on GitHub

    正如相关问题解决方案中所述,将https://raw.githubusercontent.com 替换为https://rawgit.com/,这将返回具有正确内容类型的文件。

    这里是更新的小提琴:https://jsfiddle.net/aqxwsfjo/4/

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 2016-02-07
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多