Edd*_*Edd 2 java pdf performance xhtml flying-saucer
我已经按照这篇文章使用FlyingSaucer将XHTML转换为PDF,它很棒,但有一个主要的垮台......它的速度非常慢!
我发现从XHTML渲染PDF需要1到2分钟,无论该页面有多简单.
基本代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
public class FirstDoc {
public static void main(String[] args) throws IOException, DocumentException {
String inputFile = "firstdoc.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
Run Code Online (Sandbox Code Playgroud)
示例XHTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My First Document</title>
<style type="text/css"> b { color: green; } </style>
</head>
<body>
<p>
<b>Greetings Earthlings!</b>
We've come for your Java.
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
有谁知道如何提高FlyingSaucer的性能?
如果不这样做,是否有人能够推荐一个替代的Java库,它有效地将PDF从URL渲染到带有外部CSS的(X)HTML文档和从URL生成的图像?
Mis*_*son 15
我遇到了和Edd一样的问题.
可悲的是,接下来的方法不适用于Java DocumentBuilder:xml解析速度很慢?由Marek Piechut完全为我 - 我的HTML实体在途中迷路了.
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setNamespaceAware(false);
fac.setValidating(false);
fac.setFeature("http://xml.org/sax/features/namespaces", false);
fac.setFeature("http://xml.org/sax/features/validation", false);
fac.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
fac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder builder = fac.newDocumentBuilder();
Run Code Online (Sandbox Code Playgroud)
最终诀窍是这些线:
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
builder.setEntityResolver(FSEntityResolver.instance());
Run Code Online (Sandbox Code Playgroud)
通过使用内置的Java EntityResolver来解析DTD,它获得了更快的速度.
| 归档时间: |
|
| 查看次数: |
7984 次 |
| 最近记录: |