Firefox 19 从 JavaScript 打印 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15011799/
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
Firefox 19 print PDF from JavaScript
提问by Gareth Bowen
I have a "Print" button which invokes the following JavaScript
我有一个调用以下 JavaScript 的“打印”按钮
window.frames.myPdfFrame.print();
where "myPdfFrame" refers to an iframe with a PDF for the src.
其中“myPdfFrame”是指带有 src 的 PDF 的 iframe。
In Chrome and Firefox 18 (and below) this opens the print dialog as expected, but as of Firefox 19 I get the following error
在 Chrome 和 Firefox 18(及更低版本)中,这会按预期打开打印对话框,但从 Firefox 19 开始,我收到以下错误
Error: Permission denied to access property 'print'
I assume this is related to using the embedded PDF viewer released with Firefox 19rather than the Adobe plugin. Using the print icon in the toolbar of the PDF plugin works as expected.
我认为这与使用随 Firefox 19而不是 Adobe 插件一起发布的嵌入式 PDF 查看器有关。使用 PDF 插件工具栏中的打印图标按预期工作。
Is there a way to invoke the print dialog on an inline PDF in Firefox 19 from Javascript?
有没有办法在 Firefox 19 中从 Javascript 调用内联 PDF 上的打印对话框?
回答by Miklos Aubert
I tried the provided JSFiddle demo, with Firefox 22 and NoScript. Interestingly, when I first opened the fiddle, the iframe's content was blocked by NoScript, but the print dialog readily popped up! I clicked "Ok", and I got a taskbar toast saying the document about.blankwas sent to the printer. Sure enough, I checked the printer and it spit out a nice white page. Not useful...
我使用 Firefox 22和 NoScript尝试了提供的JSFiddle 演示。有趣的是,当我第一次打开 fiddle 时,iframe 的内容被 NoScript 屏蔽了,但打印对话框很容易弹出!我点击了“确定”,然后我得到了一个任务栏吐司,说文档about.blank已发送到打印机。果然,我检查了打印机,它吐出了一张漂亮的白页。没用处...
Now, after telling NoScript to "temporarily allow" the domain of the PDF (irs.gov), I reproduce the bug :
现在,在告诉 NoScript“暂时允许”PDF(irs.gov)的域后,我重现了错误:
Error: Permission denied to access property 'print'
This other questionmade me wonder if it was a problem of same origin policy. So I tested it on my own webserver, with a PDF document hosted on the same machine. And... no dice !
这个另一个问题让我怀疑这是否是同源政策的问题。所以我在我自己的网络服务器上测试了它,在同一台机器上托管了一个 PDF 文档。而且……没有骰子!
I then tried my test page in IE 9 and Chrome. Both of them do showthe print dialog, but when I print the document, in the case of IE 9, it's actually printing about:blank, and Chrome prints a grey rectangle saying "Loading"... looks like it's printing the whole page, not just the document in the iframe.
然后我在 IE 9 和 Chrome 中尝试了我的测试页面。它们都显示了打印对话框,但是当我打印文档时,在 IE 9 的情况下,它实际上是在打印 about:blank,而 Chrome 打印了一个灰色矩形,上面写着“正在加载”……看起来它正在打印整个页面,而不仅仅是 iframe 中的文档。
So, I agree with Jason Sperske that this doesn't seem possible.
所以,我同意 Jason Sperske 的观点,这似乎是不可能的。
UPDATE: this answeroffers a solution that "just works", but only for PHP files generated by the Fpdf library...
回答by Epoc
On recent versions of Firefox (since 19), you have to disable the bugged and native PDF viewer (pdf.js) in about:config
. Set the pdfjs.disabled
property to true
and you will see the print window appearing using your script.
在最新版本的 Firefox 上(自 19 起),您必须在about:config
. 将该pdfjs.disabled
属性设置为true
,您将看到使用您的脚本出现的打印窗口。
If there's a download starting, set the plugin.disable_full_page_plugin_for_types
property to application/pdf
.
如果开始下载,请将plugin.disable_full_page_plugin_for_types
属性设置为application/pdf
。
回答by Ankit Zalani
Please try the following code, it's working:
请尝试以下代码,它正在工作:
window.frames.myPdfFrame.focus();
window.print();