xhtmlrenderer创建长度为0的PDF [英] xhtmlrenderer creating PDFs of length 0
问题描述
我是 org.xhtmlrenderer.pdf.ITextRenderer
的新手,并遇到此问题:
I am new to org.xhtmlrenderer.pdf.ITextRenderer
and have this problem:
PDF我的测试servlet流到我的下载文件夹实际上是空文件。
The PDFs that my test servlet streams to my Downloads folder are in fact empty files.
相关方法 streamAndDeleteTheClob
如下所示。
第一个尝试块肯定不是问题。
The first try block is definitely not a problem.
服务器在第二个尝试块中花费了大量时间。没有异常抛出。
The server spends a lot of time in the second try block. No exception thrown.
任何人都可以建议解决这个问题或者调试它的好方法吗?
Can anyone suggest a solution to this problem or a good approach to to debugging it?
有人能指出基本相似的代码吗?
Can anyone point me to essentially similar code that really works?
任何帮助都会非常感激。
Any help would be much appreciated.
res.setContentType("application/pdf");
ServletOutputStream out = res.getOutputStream();
...
private boolean streamAndDeleteTheClob(int pageid,
Connection con,
ServletOutputStream out) throws IOException, ServletException {
Statement statement;
Clob htmlpage;
StringBuffer pdfbuf = new StringBuffer();
final String pageToSendQuery = "SELECT text FROM page WHERE pageid = " + pageid;
// create xhtml file as a CLOB (Oracle large character object) and stream it into StringBuffer pdfbuf
try { // definitely no problem in this block
statement = con.createStatement();
resultSet = statement.executeQuery(pageToSendQuery);
if (resultSet.next()) {
htmlpage = resultSet.getClob(1);
} else {
return true;
}
final Reader in = htmlpage.getCharacterStream();
final char[] buffer = new char[4096];
while ((in.read(buffer)) != -1) {
pdfbuf.append(buffer);
}
} catch (Exception ex) {
out.println("buffering CLOB failed: " + ex);
}
// create pdf from StringBuffer
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(pdfbuf.toString())));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(out);
out.close();
} catch (Exception ex) {
out.println("streaming of pdf failed: " + ex);
}
deleteClob(con, pageid);
return false;
}
推荐答案
使用DocumentBuilder.parse方法将尝试解析XHTML页面中引用的DTD。这需要很长时间。如果您使用Flying Saucer(xhtmlrenderer),最简单的方法是以这种方式创建文档:
Using the DocumentBuilder.parse this way will try to resolve the DTD referenced in the XHTML page. It takes a really long time. The easyest way to aviod that if you are using the Flying Saucer (xhtmlrenderer), is to create the document this way:
Document myDocument = XMLResource.load(myInputStream).getDocument();
请注意,您也可以将XMLResource.load与Reader一起使用。
Note that you can use XMLResource.load with a Reader too.
这篇关于xhtmlrenderer创建长度为0的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!