【问题标题】:Print PDF report directly instead of downloading, opening and printing it [duplicate]直接打印PDF报告,而不是下载、打开和打印[重复]
【发布时间】:2012-11-23 17:10:29
【问题描述】:

我开发了一个 JSF 应用程序,它使用 JasperReports 在客户端机器上的每次交易后提供一个 pdf 文件作为下载。我跟着这个tutorial. 有什么方法可以直接打印而不是下载它,因为最终用户必须打开它并给出打印命令。 (客户说有很多交易,他们希望在独立的应用程序中以相同的方式打印收据,而不需要像打开打印对话框那样进行任何干预。)

【问题讨论】:

    标签: jsf jasper-reports


    【解决方案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 的步骤。

    【讨论】:

    • 正如 Dace Jarvis 已经说过的,您不能强制浏览器直接打印您的 pdf,但您可以做的是将您的 pdf 加载到一些隐藏的 iframe 中。在此 iFrame 上加载调用 .contentWindow.print() 后,您将直接获得打印对话框。看看这里:stackoverflow.com/questions/7430351/…
    【解决方案2】:

    您可以使用这些方法:

    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
    

    【讨论】:

    • 您好,问题是关于 Web 应用程序,而不是桌面应用程序。此答案假定为桌面应用程序,因此是错误的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 2019-03-15
    • 2018-08-07
    • 1970-01-01
    • 2014-08-04
    • 2014-03-14
    相关资源
    最近更新 更多