【问题标题】:How to display pdf on JSF如何在 JSF 上显示 pdf
【发布时间】:2018-08-21 10:26:01
【问题描述】:

您好,我的代码如下:

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 文件没有显示在页面上。

【问题讨论】:

标签: jsf primefaces


【解决方案1】:

这是我的示例,希望对你有帮助,代码有内联和附件:

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();


}

【讨论】:

  • 如何在 jsf 上调用它?
  • 我认为创建另一个链接,例如“getpdf.xhtml”,将函数 sendBackPDFToClient() 放入 backbean,然后创建一个框架 ,(将Content-Disposition修改为Inline)。
【解决方案2】:

您是否尝试过 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}"/>

【讨论】:

  • 我尝试使用文档查看器,但出现此错误:检索 PDF 时出现意外的服务器响应 (403)
  • 403 是一个安全例外,这意味着您无权访问该网址。可以贴你的。您使用的示例代码以便我可以看到?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多