无法实现 PrimeFaces 媒体以显示 PDF 文件

问题描述 投票:0回答:0

我是 PrimeFaces 和 JSF 的新手,我正在尝试在我的页面中显示 PDF 预览,但我不知道如何实现这一点。根据 PrimeFaces 展示,放置相对文件路径就足够了,但它不起作用。

我尝试将文件复制到 mi XHTML 文件所在的同一文件夹中,并得到相同的结果。 我也尝试添加 cache="false" 和 player="pdf" 属性。

<p:media value="#{mb.getStream()}" width="100%" cache="false" player="pdf">
  Your browser can't display pdf,
  <h:outputLink value="#{request.contextPath}/TEST.pdf">click</h:outputLink> to download pdf instead.
</p:media>

编辑: 我已经向我的 Managedbean 添加了一个返回 StreamedContent() 的方法,但我仍然得到相同的结果,即“您的浏览器无法显示 pdf,请点击下载 pdf。”

public StreamedContent getStream() throws IOException {
    File f = new File("C:\\TEST.pdf");
    if (f.exists()) {
        System.out.println("********** EXISTS");
        try ( InputStream is = new FileInputStream(f) ) {
            return DefaultStreamedContent.builder()
                    .contentType("application/pdf")
                    .name(f.getName())
                    .stream(() -> is)
                    .build();
        }
    } else {
        System.out.println("*-----FILE DOESNT EXISTS");
        return new DefaultStreamedContent();
    }
}//method
java jsf primefaces
© www.soinside.com 2019 - 2024. All rights reserved.