[英]How to display pdf on JSF
Hi my code is as follows: 嗨,我的代码如下:
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>
But the PDF file is not getting displayed on the page. 但是PDF文件没有显示在页面上。
Here is my sample, hope it helps, the code has the inline and attachment: 这是我的示例,希望对您有所帮助,代码具有内联和附件:
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();
}
Have you tried the Document Viewer component of PrimeFaces Extensions? 您是否尝试过PrimeFaces Extensions的Document Viewer组件?
I think its much easier to use than p:media and offers more features including ability to load it from a URL, a Stream, or a Resource. 我认为它比p:media更易于使用,并提供更多功能,包括从URL,流或资源加载它的能力。 That basic example shows you how to do it with all 3 types. 该基本示例向您展示了如何使用所有3种类型。
JAVA: 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: XHTML:
<pe:documentViewer height="500" width="1000" value="#{realReport.tempPdfFile}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.