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

发布于 2025-01-08 13:08:20 字数 384 浏览 1 评论 0原文

我有一个包含根据用户请求生成的 PDF 原始字节的 bean。我想向用户显示此 PDF,而不真正将 PDF 文件保留在我的服务器上。

在我的 jsp 中,我尝试过类似的标签,

<object data="#{bean.pdfBytes}" type="application/pdf" ></object>
<object type="application/pdf" width="600" height="400">
    <h:outputFormat value="#{bean.pdfBytes}" escape="false"/>
</object>

但这非常失败。任何帮助将不胜感激。提前致谢。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2025-01-15 13:08:21

我想向用户显示此 PDF,而不是真正将 PDF 文件保留在我的服务器上。

将其写入响应的输出流。假设您使用 iText 生成 PDF,将响应的输出流传递给 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。如果您想要“另存为”对话框,只需将标题中的“内联”部分更改为“附件”即可。或者,如果您确实希望将其嵌入 中,那么您需要创建一个 servlet 并在 doGet() 方法中执行上述 response 工作,最后让 的 URL 指向到该 servlet。

I want to display this PDF to the user without really persisting the PDF file on my server.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文