iText - 无需PDF文件即可生成文件 [英] iText - generating files on the fly without needing a PDF file

查看:190
本文介绍了iText - 无需PDF文件即可生成文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iText生成pdf文件,我对这一代有疑问。我想将PDF提供给浏览器,以便浏览器显示它,而无需实际创建文件。

I am trying to use iText for pdf file generation and I have a question regarding the generation. I would like to serve the PDF to the browser so that the browser displays it, without actually creating a file.

实现这一目标的最佳方法是什么?

What would be the best approach to achieve this?

一个限制是我需要在JSP页面中使用它 - 这会绕过getOutputStream已被调用一次错误是我正在寻找的。

One limitation is that I would need to use it from a JSP page - something that would circumvent the "getOutputStream has already been called once" error is what I am looking for.

推荐答案


我想将PDF提供给浏览器,以便浏览器显示没有实际创建文件。

只需传递 responsegetOutputStream()而不是新的FileOutputStream PdfWriter

PdfWriter pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());
// ...








一个限制是我需要在JSP页面中使用它 - 这将绕过getOutputStream已被调用一次错误是我正在寻找的。

只需删除< %%> 任何空格$ c>在JSP中,包括换行符。它们被响应编写者隐式发送给响应。

Just remove any whitespace outside <% %> in JSP, including newlines. They are implicitly sent to the response by the response writer.

I.e。不要

<% page import="foo" %>
<% page import="bar" %>

<%
   for (int i = 0; i < 1000; i++) {
       out.println("I should not use scriptlets.");
   }
%>

(newline here)

但更多

<% page import="foo" %><% page import="bar" %><%
   for (int i = 0; i < 1000; i++) {
       out.println("I should use servlets.");
   }
%>

或者更好的是,不要将Java代码放在JSP文件中。 JSP文件旨在呈现像HTML一样的模板文本,而不是完全不同的东西。在像servlet这样的普通Java类中执行此操作。

Or better, don't put Java code in JSP files. JSP files are designed to present template text like HTML, not to do entirely different things. Do that in a normal Java class like a servlet.

这篇关于iText - 无需PDF文件即可生成文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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