彩票走势图

彩票走势图 > 慧问 > 频道

html中有表格转换成word再转换成pdf 样式 错误

发表于2022-03-14 回复:0 查看:2622  |  
public static void test2(){
String template = "<table style=\"border-collapse: collapse; width: 80%; border-width: 1px; border-spacing: " +
"0px; border-style: solid;\" align=\"center\"><tbody><tr><td style=\"border-style: solid; width: 18%;" +
" border-width: 1px;\" valign=\"center\" width=\"18%\"><p style=\"text-align: center;\"><strong><span" +
" style=\"font-family: 宋体; font-size: 16px;\">测试测试会测试情况及测试意见</span></strong></p&gt;</td><td " +
"style=\"border-style: solid; width: 80%; border-width: 1px;\" colspan=\"4\" valign=\"center\" " +
"width=\"80%\"><p style=\"text-indent: 32px; line-height: 24px;\"><span style=\";font-family: 宋体; " +
"font-size: 16px;\">测试测试会根据测试测试规定的测试程序和测试测试对</span><span style=\"font-family: 宋体; font-size: 16px;" +
"\">1</span><span style=\";font-family: 宋体; font-size: 16px;" +
"\">家测试人的测试测试进行了认真的测试,具体情况如下:</span></p></td>;</tr></tbody></table><p>&nbsp;</p>";

String srcPath = "/home/imic/project/test.docx";
String destPath = "/home/imic/project/test.pdf";

try {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(template);
doc.save(srcPath, SaveFormat.DOCX);
} catch (Exception e) {
e.printStackTrace();
}


FileOutputStream os = null;
try {
long old = System.currentTimeMillis();
//新建一个空白pdf文档
File pdfFile = FileUtils.createFile(destPath);
os = new FileOutputStream(pdfFile);

LoadOptions opt = new LoadOptions();
opt.setEncoding(Charset.forName("utf8"));
Document doc = new Document(srcPath, opt);

PdfSaveOptions pso = new PdfSaveOptions();
pso.setCompliance(PdfCompliance.PDF_17);

//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
log.info("转换了{}, 共耗时{}", doc.getPageCount(), ((now - old) / 1000.0));
} catch (Exception e) {
throw new BaseException("转换错误:", e);
}finally {
}
}
0个回答

打破零回复...

回复

登录 慧都网发表评论

扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP