2017-08-29 32 views
1

我試圖替代試圖dispaly在Web瀏覽器中的PDF十萬呈現StreamedContent到媒體,這是我的XHTML文件的一部分:IMPOSIBLE使用primefaces

而且控制器:

@ManagedBean 
@SessionScoped 
public class RegisterController implements Serializable { 

private static final long serialVersionUID = 1L; 
StreamedContent showFile; 

public StreamedContent getShowFile() { 

    Path pdfPath = Paths.get("path\\to\\image"); 
    byte[] pdf = null; 

    try { 
     pdf = Files.readAllBytes(pdfPath); 
    } catch (Exception e) {} 

    ByteArrayInputStream b = new ByteArrayInputStream(pdf); 
    DefaultStreamedContent d = new DefaultStreamedContent(b, "application/pdf"); 
    return d; 
} 

public void setPintaArchivo(StreamedContent showFile) { 
    this.showFile = showFile; 
    } 
} 

在控制檯的瀏覽器中,我得到404錯誤,這裏有一些recomendations我沒有成功已經休耕:

回答

-1

如果你有一個完整的路徑(從資源文件夾)已經生成的PDF文件,你可以在你的XHTML加載:

<object id="pdf_document" type="application/pdf" data="#{youBean.documentPath}?pfdrid_c=true" width="100%" height="500px">Your browser does not support this feature!</object> 

好,如果您想顯示webservice返回的字節流中的pdf內容,則可以使用primefaces-extensions的documentViewer組件(使用PDF.js)

所以,你需要在你的豆,裝入一個字節流,併產生StreamedContent

@ManagedBean 
@SessionScoped 
public class StreamBean implements Serializable { 

    private static final long serialVersionUID = 1L; 

    private StreamedContent pdfContent; 

    public void load() { 
     try { 
      // Simulate getting byte[] from external source, like webservice 
      ClassLoader classloader = Thread.currentThread().getContextClassLoader(); 
      InputStream inputStream = classloader.getResourceAsStream("file-sample.pdf"); 

      int byteReaded = 0; 
      ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
      byte[] data = new byte[1024]; 

      while ((byteReaded = inputStream.read(data, 0, data.length)) != -1) 
       buffer.write(data, 0, byteReaded); 
      buffer.flush(); 
      byte[] pdfBytes = buffer.toByteArray(); 

      // Create the StreamedContent with readed pdf file 
      pdfContent = new DefaultStreamedContent(new ByteArrayInputStream(pdfBytes), "application/pdf"); 

     } catch (IOException e) { 
      // Manage your exception here 
      e.printStackTrace(); 
     } 
    } 

    public StreamedContent getPdfContent() { 
     return pdfContent; 
    } 
} 

重要:不要把@PostConstruct過載()方法時,它會被稱爲在PreRender事件:

<ui:composition template="/your-template.xhtml" ... xmlns:pe="http://primefaces.org/ui/extensions"> 

    <ui:define name="pageBody"> 

     <f:metadata> 
      <f:event type="preRenderView" listener="#{streamBean.load()}" /> 
     </f:metadata>     

     <pe:documentViewer height="450" value="#{streamBean.pdfContent}" download="file-sample.pdf" /> 

    </ui:define> 

</ui:composition> 

注:小心與primefaces和primefaces的擴展版本,在這種情況下,我已經使用:

<dependency> 
    <groupId>org.primefaces</groupId> 
    <artifactId>primefaces</artifactId> 
    <version>6.1</version> 
</dependency> 
<dependency> 
    <groupId>org.primefaces.extensions</groupId> 
    <artifactId>primefaces-extensions</artifactId> 
    <version>6.1.0</version> 
</dependency> 
+0

這是正確的,從來沒有我需要消費一個Web服務,返回一個包含pdf文檔的字節數組。 –

+0

@OliverSoria好的,但你需要在你的問題中指定源(不是文件)。希望答案更新對您有所幫助 –