使用javascript或jquery打印pdf [英] Print a pdf with javascript or jquery
问题描述
当我点击print.gif按钮时,我需要打印一个.pdf文件(我用jasperReport生成它并将其保存在服务器上)。
现在我发现这个解决方案适用于Firefox而不适用于IE!
I need to print a .pdf file (I generate it with jasperReport and I save it on a server) when I click on a print.gif button. Now I found this solution that works for Firefox but not for IE!
function printPdf(){
var url= document.getElementById('_url').value;
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write('<embed type="application/pdf" src="'+url+'"
id="pdfDocument" height="100%" width="100%" ></embed> <SCR'+'IPT
LANGUAGE="JavaScript">window.print();window.close();</SCR'+'IPT>');
}
我也试过 document.close();
window.focus();
window.print();
window.close();
,但它们不起作用!
I tried also document.close();
window.focus();
window.print();
window.close();
, but they didn't work!
我读了很多主题,但我找不到了这个问题的解决方案!
I read a lot of topics, but I can't find a solution for this issue!
推荐答案
在html中创建一个 iframe
: / p>
Create a iframe
in html:
<iframe id="pdf-iframe">
然后更改 src
c $ c> iframe 和加载
,打印出来。
Then change the src
of that iframe
and on load
, print it.
$('#pdf-iframe').attr("src", pdf_url).load(function(){
document.getElementById('pdf-iframe').contentWindow.print();
});
这篇关于使用javascript或jquery打印pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!