【发布时间】:2014-05-14 09:09:57
【问题描述】:
我已经成功地在 jsp 中编写了一个代码来使用 itext 生成一个 pdf 文件。它工作正常,但不是立即生成 pdf,而是部分生成。只有当我切换到另一个选项卡或窗口并切换回原来的选项卡或窗口时,它才能正确显示整个内容。
解决方案是在服务器中创建一个 pdf 文件,在浏览器窗口中打开它,并在打开后立即将其从服务器内存中删除。任何人都可以帮助我吗?
[这是该程序的精简近似值。]
<%@ page trimDirectiveWhitespaces="true" %>
<%@
page import="java.servlet.*,
javax.servlet.http.*,
java.io.*,
java.util.*,
com.lowagie.text.pdf.*,
com.lowagie.text.*"
%>
<%@ include file="connection.jsp" %>
<%
response.setContentType("application/pdf");
Document document = new Document();
try
{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
document.open();
PdfPTable table = new PdfPTable(1);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
document.add(table);
document.close();
DataOutputStream dataOutput = new DataOutputStream(response.getOutputStream());
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for(int i = 0; i < bytes.length; i++)
dataOutput.writeByte(bytes[i]);
dataOutput.flush();
dataOutput.close();
return;
}
catch(DocumentException e)
{
e.printStackTrace();
}
%>
【问题讨论】:
-
在 jsp 中使用 iText 的任何理由都被认为是不好的约定。而是使用 servlet
-
我同意 @sankrish...尝试使用 servlet 并告诉我们...