簡體   English   中英

如何在JSF上顯示pdf

[英]How to display pdf on JSF

嗨,我的代碼如下:

public StreamedContent getTempPdfFile() throws IOException {
    File testPdfFile = new File("D:\\AFC150_20180819_0103.pdf");
    streamedContent = new DefaultStreamedContent(new FileInputStream(testPdfFile), "application/pdf",
            "AFC150_20180819_0103.pdf");
    return streamedContent;
}
<h:panelGrid columns="1" cellpadding="5">
    <p:media value="#{realReport.tempPdfFile}" player="pdf" width="1000px" height="300px">
        <f:param name="id"  />
    </p:media>
</h:panelGrid>

但是PDF文件沒有顯示在頁面上。

這是我的示例,希望對您有所幫助,代碼具有內聯和附件:

void sendBackPDFToClient()
{
        //File temp = File.createTempFile(fileName, ".pdf");
        File testPdfFile = new File("D:\AFC150_20180819_0103.pdf");
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();

        ec.responseReset(); 
        ec.setResponseContentType("application/pdf"); 
        ec.setResponseContentLength((int)testPdfFile.length()); 

        //Inline
        //ec.setResponseHeader("Content-Disposition", "inline; filename=\"" + testPdfFile.getName() + "\""); 

        //Attach for Browser
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + testPdfFile.getName() + "\""); 

        OutputStream output = ec.getResponseOutputStream();
        Files.copy(testPdfFile.toPath(), output);
        fc.responseComplete();


}

您是否嘗試過PrimeFaces Extensions的Document Viewer組件?

我認為它比p:media更易於使用,並提供更多功能,包括從URL,流或資源加載它的能力。 該基本示例向您展示了如何使用所有3種類型。

JAVA:

public StreamedContent getTempPdfFile() throws IOException {
     File testPdfFile = Paths.get("D:\\AFC150_20180819_0103.pdf").toFile();
     return new DefaultStreamedContent(new FileInputStream(testPdfFile), "application/pdf",
                "AFC150_20180819_0103");
}

XHTML:

<pe:documentViewer height="500" width="1000" value="#{realReport.tempPdfFile}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM