从javascript嵌入标签打印Pdf [英] Print Pdf from javascript embed tag

查看:308
本文介绍了从javascript嵌入标签打印Pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF文件,我试图通过Javascript打印。我试过这个嵌入技巧:然而,无声打印嵌入式PDF ,打印功能从不变得可用,它总是未定义的。



我已经用这段代码试过Iframe技巧:

 函数printPDF(){$​​ b $ b if(document.getElementById(pdfDocument)。contentWindow.document.readyState ===complete){
document.getElementById(pdfDocument)。focus();
document.getElementById(pdfDocument)。contentWindow.print();
} else {
setInterval(printPDF(),1000);
}
}

(pdfDocument是iframe的ID)这样做弹出打印对话框,但打印空白页面。我希望嵌入式标签的工作方式。但为什么打印功能永远不可用?



这个主题上的大部分帖子都很旧。什么是最好的HTML5 / jQuery的方式来做到这一点? (或只是普通的JS)

编辑:

这里是嵌入标签的JS代码:

  function printPDF(){$​​ b $ b alert(document.getElementById(pdfDocument)。print); 
//等待PDF准备打印
if(typeof document.getElementById(pdfDocument)。print =='undefined'){
setTimeout(function(){printPDF() ;},1000);
} else {
var x = document.getElementById(pdfDocument);
x.print();
}
}

每秒都会改变未定义。 打印选项永远不可用。任何想法?

解决方案

我会在将来对任何人进行大量研究后发现我在这里学到的知识,这些人可能会发现这一点。



PDF的显示方式因浏览器,浏览器版本而异,浏览器配置和操作系统。有很多变量,所以我将在这里讨论最常见的情况。




  • 在所有浏览器中,我无法通过Javascript调用任何类型的print()方法,我只能使用PdfActions 。 OPENACTION会打印。我使用iText将这些嵌入到PDF中。


  • Chrome使用Adobe的查看器,它不提供对任何类型的print()方法的访问,但会执行PdfActions嵌入在PDF中。因此,您可以在PDF中嵌入OpenAction,并在打开任何查看这些操作的应用程序时打印PDF调用。

  • Firefox(以上一个特定版本,尽管所有最新版本)使用Windows中的Adobe查看器,该查看器还可识别PdfActions。但是,在OSX中,它失去了对Adobe浏览器的支持并切换到在Firefox浏览器中烘焙(pdf.js)。不支持PdfActions。 IE:我对IE没有太多的测试。主要是因为在Firefox没有在OSX上工作后,我放弃了使用JavaScript打印PDF文件(对我来说是必需的)。




我的PDF由我控制的服务器生成,因此我最终在服务器中进行了服务更改,并添加了一个获取PNG服务,该服务基于与PDF生成相同的标记生成了PNG。浏览器处理图像比我知道的PDF更好,但希望我能够重复使用PDF生成服务,因为它在我的代码中的其他地方使用过。



它没有回答这个问题,但这是我所有的信息。我对任何可能在将来会发现这一点的人的建议是:如果可能,在这种情况下尽量简化PDF。否则,如果您知道如何在OSX的FF preview pdf查看器中通过Javascript调用print(),请更新此问题。

-Phil


I have a PDF file and I am trying to print it via Javascript. I have tried this embed trick: Silent print a embedded PDF however, the print function never becomes available, it is always undefined.

I have tried the Iframe trick with this code:

function printPDF() {
if(document.getElementById("pdfDocument").contentWindow.document.readyState === "complete") {   
    document.getElementById("pdfDocument").focus();
    document.getElementById("pdfDocument").contentWindow.print();
} else {
    setInterval(printPDF(), 1000);
}
}

(pdfDocument is the ID of the iframe) This does pop up the print dialogue, but printing a blank page. I would love the embed tag way to work. But why is the print function never becoming available?

Most of the posts on this subject are quite old. What is the best HTML5/jQuery way to do it? (or just regular JS at this point)

EDIT:

here is the JS code for the embed tag:

function printPDF() {
alert(document.getElementById("pdfDocument").print);
//Wait until PDF is ready to print    
 if (typeof document.getElementById("pdfDocument").print == 'undefined') {
     setTimeout(function(){printPDF();}, 1000);
 } else {
     var x = document.getElementById("pdfDocument");
     x.print();
 }
}

This keeps altering "undefined" every second. The Print option is never available. Any Ideas?

解决方案

I put a bounty on this questions a week or so ago and it's expired. I'm going to post what I learned here after a lot of research for anyone in the future who might find this.

PDF's are displayed differently based on browser, browser version, browser configuration, and Operating System. There are a lot of variables so I'll talk about the most common situations here.

  • On all browsers I was unable to call any sort of print() method through Javascript, I was only able to use PdfActions. The OPENACTION would call print. I embedded these into the PDF using iText.

  • Chrome uses Adobe's viewer, which doesn't give access to any sort of print() method but does execute PdfActions embedded in the PDF. So you can embed an 'OpenAction' in a PDF and have the PDF call print whenever it's opened from any application that looks at those actions.

  • Firefox (above a certain version, all recent versions though) uses the Adobe viewer in Windows, which also recognizes PdfActions. However, in OSX it loses support for the Adobe viewer and switches to the baked in Firefox viewer (pdf.js). Which does not support PdfActions.

  • IE: I didn't really test much on IE. Mostly because I gave up on printing PDF's from Javascript after Firefox didn't work on OSX (a req. for me).

My PDF's were being generated by a server that I control so I ended up making service changes in my server and adding a get PNG service that generated a PNG based on the same markup that the PDF generation uses. Browsers handle images much better than PDFs, which I knew going in, but hoped that I would just be able to re-use the PDF generation service since it's used elsewhere in my code.

It doesn't answer the question, but it's all the information I have. My suggestion to anyone who might find this in the future: ditch PDF if possible in this case and go simpler. Otherwise, please update this question if you know how to call print() through Javascript in FF preview pdf viewer in OSX.

-Phil

这篇关于从javascript嵌入标签打印Pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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