javascript pdf.js 不适用于 Safari
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17022052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pdf.js not working with Safari
提问by Yarin
We're testing out pdf.jsand while it seems like an awesome project we can't get it working in Safari.
我们正在测试pdf.js,虽然它看起来是一个很棒的项目,但我们无法在 Safari 中运行它。
(Tested on PDF.JS version = 0.8.229 (latest) / Safari 5.1.9 - 6.0.4 / Mac OSX 10.6.8 - 10.8.3)
(在 PDF.JS 版本 = 0.8.229(最新)/Safari 5.1.9 - 6.0.4 / Mac OSX 10.6.8 - 10.8.3 上测试)
EXAMPLE:
例子:
This is an example of the demo code served from our server with a sample PDF that works on Chrome/FFox but not Safari: http://test.appgrinders.com/pdf_js/test.html
这是从我们的服务器提供的演示代码示例,其中包含适用于 Chrome/FFox 但不适用于 Safari 的示例 PDF:http://test.appgrinders.com/pdf_js/test.html
Console output:
控制台输出:
Warning: Setting up fake worker.
Error: Invalid XRef stream (while reading XRef):
Error: Invalid XRef stream pdf.js:850undefined
Warning: Indexing all PDF objects
Error: Invalid XRef stream (while reading XRef):
Error: Invalid XRef stream pdf.js:850undefined
More tests:
更多测试:
The following is a list of sample PDFs we tested (They were all served from our server, and all worked in Chrome/FFox/Android). The only one that worked with Safari was the PDF file served from the pdf.js project itself:
以下是我们测试的示例 PDF 列表(它们都来自我们的服务器,并且都在 Chrome/FFox/Android 中运行)。唯一与 Safari 一起使用的是 pdf.js 项目本身提供的 PDF 文件:
FAILS IN SAFARI:
http://samplepdf.com/sample.pdf
http://forums.adobe.com/servlet/JiveServlet/previewBody/2041-102-1-2139/Sample.pdf
https://github.com/prawnpdf/prawn/raw/master/data/pdfs/form.pdf
Safari 失败:
http ://samplepdf.com/sample.pdf
http://forums.adobe.com/servlet/JiveServlet/previewBody/2041-102-1-2139/Sample.pdf
https://github.com/ prawnpdf/prawn/raw/master/data/pdfs/form.pdf
WORKS IN SAFARI:
http://cdn.mozilla.net/pdfjs/helloworld.pdf
(NOTE: This is a sample PDF from the pdf.js project and the only one we ever got working)
在 SAFARI 中工作:
http
://cdn.mozilla.net/pdfjs/helloworld.pdf(注意:这是来自 pdf.js 项目的示例 PDF,也是我们唯一工作过的示例)
We've submitted a bug report, but the developers do not seem to have an answer, so I'm hoping someone here might...
我们已经提交了一个错误报告,但开发人员似乎没有答案,所以我希望这里有人可能......
How can we get pdf.js working with Safari?
我们如何让 pdf.js 与 Safari 一起工作?
回答by Yarin
I've figured out how to get things working on Mac Safari (without necessarily understanding why)...
我已经找到了如何在 Mac Safari 上运行的方法(不一定理解为什么)...
compatibility.js
must be included.PDFJS.workerSrc
must be assigned.The demo code I had been testing (from the JS Bin demos here) does not do this, though some other online examples do (including the hello world exampleand the examples provided by @AndrewBenjamin – thanks). Other browsers don't seem to require this, but Safari won't work without it.
<script type="text/javascript" src="compatibility.js"></script> <script type="text/javascript" src="pdf.js"></script> <!-- NEED THIS for Safari Mac to render work --> <script type="text/javascript"> // Specify the main script used to create a new PDF.JS web worker. // In production, change this to point to the combined `pdf.js` file. PDFJS.workerSrc = 'pdf.worker.js'; </script>
compatibility.js
必须包括在内。PDFJS.workerSrc
必须赋值。我一直在测试的演示代码(来自此处的JS Bin 演示)没有这样做,尽管其他一些在线示例可以(包括hello world 示例和@AndrewBenjamin 提供的示例 - 谢谢)。其他浏览器似乎不需要这个,但没有它 Safari 将无法工作。
<script type="text/javascript" src="compatibility.js"></script> <script type="text/javascript" src="pdf.js"></script> <!-- NEED THIS for Safari Mac to render work --> <script type="text/javascript"> // Specify the main script used to create a new PDF.JS web worker. // In production, change this to point to the combined `pdf.js` file. PDFJS.workerSrc = 'pdf.worker.js'; </script>
Again, can't explain why, but this is how we got it working for us.
同样,无法解释原因,但这就是我们让它为我们工作的方式。
回答by AndrewBenjamin
I've got PDF.js working fine in Safari on my local server, but when I put it on the remote server, the goofy error comes back:
我的 PDF.js 在本地服务器上的 Safari 中运行良好,但是当我将它放在远程服务器上时,愚蠢的错误又回来了:
Warning: Setting up fake worker.
Unhandled rejection: Error: INVALID_STATE_ERR: DOM Exception 11
警告:设置假工人。
未处理的拒绝:错误:INVALID_STATE_ERR:DOM 异常 11
It will also show this error on my local computer if I happen to have the developer console open. Close the console, PDF displays in Safari; open the console, and it doesn't work anymore.
如果我碰巧打开了开发者控制台,它也会在我的本地计算机上显示此错误。关闭控制台,Safari中显示PDF;打开控制台,它不再工作了。
The question is: what do the developer tools and the remote server change versus running on a local server? Is this still a range-checking problem?
问题是:与在本地服务器上运行相比,开发人员工具和远程服务器有何变化?这仍然是范围检查问题吗?
I got PDF.js to work, though! I've modified so much stuff I don't know what part of what I did worked. Here's a list of stuff I did.
不过,我让 PDF.js 工作了!我修改了很多东西,我不知道我所做的工作的哪一部分。这是我所做的事情的清单。
Added
compatibility.js
– modified the last function in it to read like this:(function checkRangeRequests() { var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; if (!isSafari) { return; } document.addEventListener('DOMContentLoaded', function (e) { if (isSafari) { PDFJS.disableRange = true; } }); })();
Changed the order of
xhr.open()
calls inpdf.js
so thatxhr.setRequestHeader()
occurs afterxhr.open()
Removed all
'use strict';
linesAdded
xhr.setRequestHeader("Cache-control", "no-cache");
afterxhr.open
on lines 37272 and 41262Minified
pdf.js
and it all works all the time!
添加
compatibility.js
- 将其中的最后一个函数修改为如下所示:(function checkRangeRequests() { var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; if (!isSafari) { return; } document.addEventListener('DOMContentLoaded', function (e) { if (isSafari) { PDFJS.disableRange = true; } }); })();
更改了
xhr.open()
调用顺序,pdf.js
以便在之后xhr.setRequestHeader()
发生xhr.open()
删除了所有
'use strict';
行在第 37272 和 41262 行
xhr.setRequestHeader("Cache-control", "no-cache");
之后添加xhr.open
缩小
pdf.js
,它一直有效!
回答by AndrewBenjamin
PDFJS.getDocument() will accept eithera string link or a Uint8Array. In my client side version I pass PDFJS.getDocument() a Uint8Array, but if I want an existing file rendered, I just pass the path to the file:
PDFJS.getDocument()将接受任何字符串链接或Uint8Array。在我的客户端版本中,我通过 PDFJS.getDocument() 一个 Uint8Array,但如果我想要呈现现有文件,我只需将路径传递给文件:
jspdf line 965:
jspdf 第 965 行:
PDFJS.getDocument('..files/pdf/sample.pdf').then(function(pdf) {
I don't know what makes your safari browser fail, but if you can see sample.pdf on my test site, you have to be very close to solving this.
我不知道是什么让你的 safari 浏览器失败,但如果你能在我的测试站点上看到 sample.pdf ,你必须非常接近解决这个问题。
回答by andr3s2
I've figured out how to get things working on Mac Safari & iPad I removed the 'strict mode' at the beginning of
我已经找到了如何在 Mac Safari 和 iPad 上运行的方法我在开始时删除了“严格模式”
1. pdf.js/src/core/worker.js
2. pdf.js/web/download_manager.js
And worked!!
并且工作了!!
回答by Simón Internacional
I had the same problem with Safari Mobile... Can't open more than 1 Mb PDF's... I solved fragmenting the file in parts of 900 kb, join them in local and then injecting to pdf.js as Uint8Array() object. I think it's a no documented limitation.
我在 Safari Mobile 上遇到了同样的问题...无法打开超过 1 Mb 的 PDF...我解决了将文件分成 900 kb 的部分问题,将它们加入本地,然后作为 Uint8Array() 对象注入到 pdf.js . 我认为这是一个没有记录的限制。