我知道开发一个webapp,用于创建pdf文档并在客户端打印 . 这是我的问题:
我使用itext创建了pdf并将其存储在共享文件夹中 . 我是在服务器端做的 . 现在我需要在客户端打印创建的pdf,客户端知道pdf的路径并可以访问它 . 为了在客户端,我试图使用javascript或jquery打印该文档 .
我尝试在我的HTML中使用嵌入但它没有用 .
感谢帮助,
这是服务器端的工作代码:
FileInputStream fis = new FileInputStream("test.pdf");
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc pdfDoc = new SimpleDoc(fis, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService services = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = services.createPrintJob();
job.print(pdfDoc, aset);
这是我在客户端尝试的内容:
// Grabs the Iframe
document.write('<embed type="application/pdf" src="\\SN782629\TempPartage\test.pdf" id="PDF" name="PDF" width="100%" height="100%" />');
var ifr = document.getElementById("PDF");
//PDF is completely loaded. (.load() wasn't working properly with PDFs)
ifr.onreadystatechange = function() {
if (ifr.readyState == 'complete') {
ifr.contentWindow.focus();
if ($.browser.msie) {
document.execCommand('print', false, null);
} else {
window.print();
}
}
ifr.parentNode.removeChild(ifr);
第二个代码是在ajax请求的成功部分,我可以根据需要放置整个函数 .
1 回答
fyi:Recommended way to embed PDF in HTML?
另一点:当从本地文件系统将pdf包含到通过HTTP加载的页面时,您将在不同的浏览器中遇到很多限制 .
我建议你在你的服务器上通过url公开你的pdf . 例如http://myhost/getPdf/SN782629/TempPartage/test.pdf而不是“"\SN782629\TempPartage\test.pdf"并在渲染页面中使用此链接 .