在JSP中将原始字节作为PDF嵌入

编程入门 行业动态 更新时间:2024-10-27 04:31:18
本文介绍了在JSP中将原始字节作为PDF嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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嵌入

本文发布于:2023-10-24 09:22:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523564.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   字节   原始   JSP   PDF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!