我有一个bean,其中包含应用户要求生成的PDF的原始字节.我想向用户显示此PDF,而无需将PDF文件真正保留在服务器上.
I have a bean with the raw bytes of a PDF that is generated at the user's request. I want to display this PDF to the user without really persisting the PDF file on my server.
在我的jsp中,我尝试了类似
In my jsp I have tried tags like
<object data="#{bean.pdfBytes}" type="application/pdf" ></object> <object type="application/pdf" width="600" height="400"> <h:outputFormat value="#{bean.pdfBytes}" escape="false"/> </object>但是这非常失败.任何帮助,将不胜感激.预先感谢.
but this fails terribly. Any help would be appreciated. Thanks in advance.
推荐答案
我想向用户显示此PDF,而无需将PDF文件真正保留在服务器上.
将其写入响应的输出流.假设您正在使用iText生成PDF,并将响应的输出流传递到PdfWrter#getInstance().
Write it to the output stream of the response. Let's assume that you're using iText to generate the PDF, pass the response's output stream to PdfWrter#getInstance().
public void download() throws IOException { FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\""); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream()); document.open(); // Build document. context.responseComplete(); }但是,这将在浏览器中完整显示PDF.如果要进行另存为对话框,只需将标题中的inline部分更改为attachment.或者,如果您真的想将其嵌入到<object>中,则需要创建 servlet 并执行doGet()方法中的上述response作业,最后让<object>的URL指向该servlet.
This will however display the PDF in its entirety in the browser. If you want a Save As dialogue, just change the inline part in the header to attachment. Or if you really want to have it embedded in an <object>, then you'd need to create a servlet and do the above response job inside the doGet() method and finally let <object>'s URL point to that servlet.
更多推荐
在JSP中将原始字节作为PDF嵌入
发布评论