我正尝试使用iText和Flying Saucer从Html生成PDF。我已经使用了SO上建议的不同技术来快速解析 。Flying Saucer - ITextRenderer createPdf极其缓慢
但是,ITextRenderer createPdf(outputstream)
已成为瓶颈。这是非常缓慢的,我不知道如何提高过程的速度。
任何帮助将非常感激。
private Document getDocument(String htmlContent) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//For faster document.
factory.setNamespaceAware(false);
factory.setValidating(false);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(FSEntityResolver.instance());
return builder.parse(new ByteArrayInputStream(htmlContent.getBytes()));
}
public void printHtmlToPdf(final String htmlContent, String tempFile, String title) throws Exception
{
Document document = getDocument(htmlContent);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(document, null);
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile));
renderer.layout();
renderer.createPDF(outputStream);
outputStream.close();
}
对这个问题的答案可以帮助:[链接](http://stackoverflow.com/questions/5431646/is-there-any-way-improve-the-performance-of-flyingsaucer) – Edd
在我的情况,加载更多的CSS(例如:bootstrap.css)和图像导致layout()任务缓慢。 – YROjha
您是否找到解决方案? – s1moner3d