Jsf 直接打印PDF报告,而不是下载、打开和打印

Jsf 直接打印PDF报告,而不是下载、打开和打印,jsf,jasper-reports,Jsf,Jasper Reports,我开发了一个JSF应用程序,它使用JasperReports在客户机上的每个事务之后提供一个pdf文件作为下载。我遵循了这一点,我们可以直接打印它,而不是下载它,因为最终用户必须打开它并发出打印命令。 (客户表示有很多交易,他们希望在独立应用程序中以相同的方式打印收据,而无需任何干预,如打开打印对话框。)如果不显示打印对话框,则无法强制浏览器打印 但是,可以设置内容配置,以便浏览器在浏览器中打开可以打印的PDF。例如: /** * Sets the regular HTTP heade

我开发了一个JSF应用程序,它使用JasperReports在客户机上的每个事务之后提供一个pdf文件作为下载。我遵循了这一点,我们可以直接打印它,而不是下载它,因为最终用户必须打开它并发出打印命令。
(客户表示有很多交易,他们希望在独立应用程序中以相同的方式打印收据,而无需任何干预,如打开打印对话框。)

如果不显示打印对话框,则无法强制浏览器打印

但是,可以设置内容配置,以便浏览器在浏览器中打开可以打印的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。如果更改内容配置,浏览器将以内联方式显示PDF,而不提示保存。这将跳过打开PDF的步骤。

您可以使用以下方法:

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

正如Dace Jarvis已经说过的,您不能强制浏览器直接打印pdf,但您可以做的是将pdf加载到某个隐藏的iframe中。在这个iFrame上加载call.contentWindow.print()之后,您将直接获得打印对话框。请看这里:您好,问题是关于web应用程序,而不是桌面应用程序。此答案假定为桌面应用程序,因此是错误的。