Print PDF report directly instead of downloading, opening and printing it [duplicate]

假如想象 提交于 2020-01-11 11:58:10

问题


I developed a JSF application which gives a pdf file as a download after each transaction at the client machine using JasperReports. I followed this tutorial. Is there any way we can directly print it rather than downloading it as the end user has to open it and give the print command. (Customers say that there are lot of transactions and they want to print the receipt in the same manner in a standalone application without any intervention like bringing the print dialog.)


回答1:


You cannot force the browser to print without a print dialog appearing.

You can, however, set the Content-Disposition such that the browser opens up a PDF in the browser that can be printed. For example:

  /**
   * 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() );
  }

This will prompt the user to save the PDF. If you change the Content-Disposition, the browser will display the PDF inline without prompting to save. This will skip the step of having to open the PDF.




回答2:


You can use these methods:

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


来源:https://stackoverflow.com/questions/13533339/print-pdf-report-directly-instead-of-downloading-opening-and-printing-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!