我正在尝试使用 iText 和飞碟从 Html 生成 PDF。我使用了 SO 上建议的不同技术来快速启用DocumentBuilder
解析。
但是,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();
}