我正在使用飞碟将xhtml转换为pdf。但方法setDocument在执行时需要2分钟。 我尝试使用这里使用的内容:Is there any way improve the performance of FlyingSaucer?但不起作用。 html的长度不是问题,我可以制作1页的pdf,并且与87页中的一页相同。
我正在使用flying-saucer-pdf-9.1.7,flying-saucer-core-9.1.13,itext-4.2.1,org.apache.poi-3.17,xercesImpl-2.9.1和java 8。
这是我正在使用的代码:
try {
String k = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\r\n" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head>\r\n" +
" <title></title>\r\n" +
" <link rel=\"stylesheet\" href=\"http://www.dynamicdrive.com/ddincludes/mainstyle.css\" type=\"text/css\" />\r\n" +
" <style type=\"text/css\">\r\n" +
" .divisor{\r\n" +
" width: 100%;margin-bottom: 20px;border-bottom: 3px solid rgb(211, 207, 207);\r\n" +
" }\r\n" +
" #labelKey{\r\n" +
" width:100px;\r\n" +
" height:12px;\r\n" +
" font-weight: 5px;\r\n" +
" background:rgb(211, 207, 207);\r\n"
+ "font-size:12px;\r\n" +
" }\r\n"
+" div.header {\n" +
" display: block; " +
" position: running(header);"
+ "margin-bottom:35px}\n" +
" div.footer {\n" +
" display: block; text-align: center;\n" +
" position: running(footer);}\n"
+ "div.content {margin-top:5cm}" +
" @page { @top-center { content: element(header) }}\n" +
" @page { @bottom-center { content: element(footer) }}\n"+
" @page {margin-top: 2.5cm;}" +
" </style>\r\n" +
" </head>"
+ "<body>"
+ "<div class='header'>"
+ " <p style=\"font-size:12px;\"> "
+ encabezado
+ "</p>"
+ "</div>"
+ "<div class='footer'>"
+ " <p style=\"padding-bottom: 15px;font-size:12px;\"> "
+ pie
+ "</p>"
+ "</div>"
+ "<div class='content'>"
+ html
+ "</div>"
+ "</body>"
+ "</html>";
try {
try (PrintWriter out = new PrintWriter(configuration.getConfig("files.location") + File.separator + "busqueda-" + idBusqueda + ".xhtml")) {
out.println(k);
}
System.out.println("antes del inputStream");
ITextRenderer renderer2 = new ITextRenderer();
System.out.println("antes de armar documento");
ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer2.getOutputDevice());
callback.setSharedContext(renderer2.getSharedContext());
renderer2.getSharedContext ().setUserAgentCallback(callback);
Document doc = XMLResource.load(new InputSource(configuration.getConfig("files.location") + File.separator + "busqueda-" + idBusqueda + ".xhtml")).getDocument();
OutputStream out = new FileOutputStream(new File(configuration.getConfig("files.location") + File.separator + "reporte-" + idBusqueda + ".pdf"));
renderer2.setDocument(doc, configuration.getConfig("files.location") + File.separator + "busqueda-" + idBusqueda + ".xhtml");
System.out.println("antes del layout");
renderer2.layout();
System.out.println("antes del createPDF");
renderer2.createPDF(out);
out.close();
} catch (DocumentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new File(configuration.getConfig("files.location") + File.separator + "reporte-" + idBusqueda + ".pdf");
} catch (Exception e) {
e.printStackTrace();
}
return null;
任何人都知道为什么执行setDocument可能需要两分钟? 我尝试使用setDocumentFromString(String string),setDocument(String url),setDocument(Document doc)并注意。 如果没有解决方案,我可以使用其他库将html转换为使用css的PDF格式?
非常感谢。