2012-11-23 91 views
1

我开发了一个JSF应用程序,该应用程序使用JasperReports在客户端计算机的每个事务之后给出PDF文件作为下载。我跟着这个tutorial.有没有什么办法可以直接打印它,而不是下载它,因为最终用户必须打开它并给出打印命令。 (客户说有很多交易,他们希望在独立应用程序中以相同的方式打印收据,而不需要任何干预,如打开对话框。)直接打印PDF报告而不是下载,打开和打印它

回答

1

您不能强制浏览器在没有打印对话框的情况下打印出现。

但是,您可以设置Content-Disposition,以便浏览器在可打印的浏览器中打开PDF。例如:

/** 
    * Sets the regular HTTP headers, regardless of whether this report is 
    * embedded in the browser window, or causes a "Save As" dialog prompt to 
    * download. 
    */ 
    protected void setHeaders() { 
    getServletResponse().setHeader("Cache-Control", getCacheControl()); 
    } 

    /** 
    * Sets the HTTP headers required to indicate to the browser that the 
    * report is to be downloaded (rather than displayed in the current 
    * window). 
    */ 
    protected void setDownloadHeaders() { 
    HttpServletResponse response = getServletResponse(); 
    response.setHeader("Content-Description", getContentDescription()); 
    response.setHeader("Content-Disposition", "attachment, filename=" 
     + getFilename()); 
    response.setHeader("Content-Type", getContentType()); 
    response.setHeader("Content-Transfer-Encoding", 
     getContentTransferEncoding()); 
    } 

这会提示用户保存PDF。如果更改Content-Disposition,浏览器将在不提示保存的情况下内联显示PDF。这将跳过必须打开PDF的步骤。

+0

由于鲮鱼贾维斯已经说过了,你不能强制浏览器直接打印您的PDF,但你可以做的是你的PDF加载到一些隐藏的iframe。在此iFrame上加载调用.contentWindow.print()后,您将直接进入打印对话框。看看这里:http://stackoverflow.com/questions/7430351/jsf-seam-how-to-download-and-print-a-dynmically-generated-pdf-file-without-user – stg

-1

您可以使用以下方法:

JasperPrintManager.printPage(jasperPrint, 0, true);//for Direct print 
         * True : It Shows "Printrer Dialog also" 

JasperPrintManager.printPage(jasperPrint, 0, false);//for Direct print 
         * fasle : It can't Show "Printrer Dialog", it will print the report directly 
+1

嗨,问题是关于一个Web应用程序,而不是桌面应用程序。这个答案假设一个桌面应用程序,因此是错误的。 – BalusC

相关问题