首页 文章

如何从groovy webApp在客户端打印pdf?

提问于
浏览
0

我知道开发一个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 回答

相关问题