简体   繁体   中英

How to display PDF in JSF, with content from ServletResponse

In my application, I use jsf & richfaces, I want to display a generated pdf in web browser , I have generated this PDF in serverside and it is available in ServletResponse, but I am unable to display it in my web page.

I have tried this question but, it did not solve my problem.

Is there any libraries or special ways to do this?

What I have tried to do is given below.

    <h:commandLink rendered="#{ fileImportOptionsViewBean.isFileExist(status.myPdf) }" action="#
    {fileImportOptionsViewBean.downloadFile(satus.myPdf)}" target="_blank">


<h:graphicImage url="../../resources/xyz/icons/pdf.png" /></h:commandLink>

downloadFile method

public void downloadFile(String filePath){
    try {           
        File file=new File(filePath);
        if(file.exists()){
            FacesContext fc=FacesContext.getCurrentInstance();
            ExternalContext ec=fc.getExternalContext();
            ec.responseReset();
            ec.setResponseContentType("application/pdf");
            ec.setResponseHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\""); 
            ec.setResponseBufferSize(8192);
            OutputStream output = ec.getResponseOutputStream();                       
            URL url = file.toURL();
                            try(
                                BufferedInputStream bis = new BufferedInputStream(url.openStream());
                                BufferedOutputStream bos = new BufferedOutputStream(output);             
                             ){
                                byte[] buff = new byte[8192];
                                int bytesRead;
                                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                                bos.write(buff, 0, bytesRead);              
                                 }                      
                            }
            fc.responseComplete(); 
        }


    } catch (Exception e) {

    }

}

Any help is appreciated,Thanks.

我目前的设置没有任何问题。问题很可能出在您的XHTML页面上,并且是导致h:commandLink无法触发该事件的原因。请参阅帖子以获取更多详细信息,当然这会对您有所帮助给你。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM