2012-11-23 90 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

相關問題