javascript - 使用 javascript 或 jquery 打印 pdf

标签 javascript pdf printing

当我单击 print.gif 按钮时,我需要打印 .pdf 文件(我使用 jasperReport 生成它并将其保存在服务器上)。 现在我发现这个解决方案适用于 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>'); 
}

我也尝试了 document.close(); window.focus(); window.print(); window. close();,但它们不起作用!

我阅读了很多主题,但找不到该问题的解决方案!

最佳答案

在 html 中创建一个 iframe:

<iframe id="pdf-iframe">

然后更改该 iframesrc加载时,打印它。

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

关于javascript - 使用 javascript 或 jquery 打印 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16400508/

相关文章:

r - 禁止打印 auto.arima 模型上的摘要

javascript - Windows 上的 Python 标准输出

javascript - jquery .find()方法——参数由逗号连接的dom组成

javascript - 表的引导分页不起作用

javascript - 如何使用 Javascript 为动态创建的元素添加选项卡索引?

java - 点 -> 像素 iText (im)精度

c# - 从 Windows 服务打印 EMF

R Shiny iframe 更新后不再显示 .pdf 源代码?

html - 如何在没有mysql数据库中的html标签的情况下打印pdf?

html - Angular Material - 如何将 CSS 样式传递给新窗口