求助:如何将一段包含HTML标签的String输出为PDF文件。
如题,现在有一个String类型的字符串,其实就是一个HTML文件的内容,怎么把它导出为PDF文件呢?OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/fonts/simsun.ttc",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
StringBuffer html = new StringBuffer();
// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">")
.append("<head>")
.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")
.append("<style type=\"text/css\">img {width: 700px;}</style>")
.append("</head>")
.append("<body>");
TraceInstanceIndex tii = getTraceInstanceIndex("TRACIN11122700068ED1");
//traceContent是一个HTML文件的内容
String traceContent = OaTools.decodeBase64String(tii.traceContent());
html.append(traceContent);
html.append("</body></html>");
renderer.setDocumentFromString(html.toString());
renderer.layout();
renderer.createPDF(os);
os.close();
运行时报错,
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException: Open quote is expected for attribute "border" associated with an element type "TABLE". --------------------编程问答-------------------- iText不太了解,你确定他可以htmltopdf?
可以尝试一下wkhtmltopdf
支持js支持html5中的canvas,而且输出的结果是我试过的几个htmltopdf里最好的 --------------------编程问答--------------------
如果不加,html.append(traceContent);
导出没有问题,但是现在我要做的,就是把traceContent里面的内容导出来。 --------------------编程问答-------------------- 用flying saucer做吧,里面集成itext,可以显示css,好用多了 --------------------编程问答-------------------- 试试看吧,搞了好多天了,还没弄出来。
补充:Java , Web 开发