简体   繁体   English

使用javascript或jquery打印pdf

[英]Print a pdf with javascript or jquery

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. 当我点击print.gif按钮时,我需要打印一个.pdf文件(我用jasperReport生成它并将其保存在服务器上)。 Now I found this solution that works for Firefox but not for IE! 现在我发现这个解决方案适用于Firefox而不适用于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>'); 
}

I tried also document.close(); 我也尝试过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! 我读了很多主题,但我找不到解决这个问题的方法!

Create a iframe in html: 在html中创建一个iframe

<iframe id="pdf-iframe">

Then change the src of that iframe and on load , print it. 然后更改该iframesrcon load时打印它。

$('#pdf-iframe').attr("src", pdf_url).load(function(){
    document.getElementById('pdf-iframe').contentWindow.print();
});

I believe the Problem is that the content is not loaded yet? 我相信问题是内容尚未加载? I had the same issue in IE8. 我在IE8中遇到了同样的问题。 I had to save the document, reopen it, print, and close the window again. 我不得不保存文档,重新打开,打印并再次关闭窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM