2013-04-30 58 views
0

我有一個要求,我必須生成一個PDF然後點擊按鈕「SHOW PDF」,我必須在另一個窗口上顯示。 我已經能夠使用IText生成pdf並存儲在我的機器上。我得到一個java.io.File對象作爲我需要顯示在屏幕上的後端庫的返回值。有人能指導我如何做到這一點?Primefaces生成pdf並點擊按鈕顯示

我的XHTML文件具有下面的代碼片段:

<h:commandLink action="PdfDisplayRedirect.xhtml" target="_blank">show PDF</h:commandLink> 

我PdfDisplayRedirect.xhtml具有下面的代碼:

<p:media value="#{pdfGenerationAction.fileName}" width="100%" height="300px"> 
Your browser can't display pdf, <h:outputLink value="InitialExamination33.pdf">click</h:outputLink> to download pdf instead. 

我支持bean具有下面的代碼:

private File initialExaminationFile; 
private generateFile(){ 
    this.initialExaminationFile = backendService.generateFile(); 
} 

點擊後,我得到一個打開的新窗口,但沒有顯示pdf文件..相反,我從那裏調用了命令的屏幕顯示在那裏。

任何幫助將非常感激。 謝謝

+0

您正在使用哪種版本的primefaces? – rags 2013-04-30 07:31:06

+0

你使用的是什麼瀏覽器? – rags 2013-04-30 07:39:19

+0

謝謝你的迴應。我正在使用primefaces 3.4.2和firefox作爲我的瀏覽器 – 2013-04-30 07:41:20

回答

2

感謝您的迴應,並沒有迴應。 我自己找到了一個我想發佈的解決方案,以便那些正在尋找解決方案的人可以使用它。

我的XHTML文件包含一個commandlink

<p:commandLink actionListener="#{pdfGenerationAction.generatePDF(initialExaminationEMRAction.patientID)}" oncomplete="window.open('PdfDisplayRedirect.xhtml')">broadcast Msg</p:commandLink> 

我pdfGenerationAction Bean文件有下面的代碼行:

FileInputStream fis = new FileInputStream(this.initialExaminationFile); 
    //System.out.println(file.exists() + "!!"); 
    //InputStream in = resource.openStream(); 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    byte[] buf = new byte[1024]; 
    try { 
     for (int readNum; (readNum = fis.read(buf)) != -1;) { 
      bos.write(buf, 0, readNum); //no doubt here is 0 
      //Writes len bytes from the specified byte array starting at offset off to this byte array output stream. 
      System.out.println("read " + readNum + " bytes,"); 
     } 
     this.reportBytes = buf; 
    } 

我轉換我的文件到bytearraystream,並使它在我的會話使用。然後我遵循BalusC給出的建議Unable to show PDF in p:media generated from streamed content in Primefaces