如何在JSF上显示pdf [英] How to display pdf on JSF

查看:77
本文介绍了如何在JSF上显示pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

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

But the PDF file is not getting displayed on the page.

推荐答案

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

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


}

这篇关于如何在JSF上显示pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆