textpdf渲染xml
之前做文档存根,使用到了pdf技术。大致是创建一个word模板,然后转pdf,最后通过adobe工具转pdf制作好了一个pdf模板文件;通过textpdf工具来操作这个模板文件,最后导出。经过上面的一堆操作后,发现问题是络绎不绝,特别是在样式上,存在着许许多多的问题。这里介绍另一种方法来解决。
通过创建xml模板,然后在通过textpdf工具的方法转换,直接转为pdf文件。因为是做web开发,结合xml工具也很方便便捷。操作如下:
导入maven依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.20</version>
</dependency>
<!--pdf支持-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<!--动态模板支持-->
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>
<!--css高级特性支持-->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-itext5 -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.5</version>
</dependency>
</dependencies>
模板文件准备
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
<style>
body{
font-family:SimHei;
}
.color{
color: green;
}
.pos{
position:absolute;
left:200px;
top:5px;
width: 200px;
font-size: 10px;
}
</style>
</head>
<body>
<img src="logo.png" width="600px"/>
<div class="color pos">
你好,${test}
</div>
</body>
</html>
api操作
private static final String DEST = "target/HelloWorld_CN_HTML.pdf";
private static final String HTML = App.class.getClassLoader().getResource("static/template.ftl").getPath();
private static final String FONT = "simhei.ttf";
public static void main(String[] args) throws IOException, InvocationTargetException, IllegalAccessException, DocumentException, TemplateException {
// DemoTest pojo1 = new DemoTest("username1", "password1", null);
// DemoTest pojo2 = new DemoTest("username1", null, "password2");
// DemoTest demoTest = BeanUtil.copyProperties(pojo1, DemoTest.class);
// BeanUtil.copyProperties(pojo1,pojo2,CopyOptions.create().ignoreNullValue());
// System.out.println(pojo2);
/**
* 动态模板渲染
* 直接通过freeMaker去渲染
*/
Configuration config = new Configuration();
config.setDirectoryForTemplateLoading(new File(App.class.getClassLoader().getResource("static").getPath()));
Template template = config.getTemplate("template.ftl");
HashMap<String, String> map = new HashMap<>();
map.put("test","hellowWorld!你好!世界");
ByteArrayOutputStream out = new ByteArrayOutputStream();
template.process(map,new PrintWriter(out));
// FileWriter fileWriter = new FileWriter("./demo.html");
// fileWriter.write(new String(out.toByteArray()));
// fileWriter.close();
/**
* 普通模板渲染
*/
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
// step 3
document.open();
// step 4 textPdf关键的方法!
// XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
// fontImp.register();
// XMLWorkerHelper.getInstance().parseXHtml(writer, document,
// new FileInputStream(HTML), null, Charset.forName("UTF-8"));
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new ByteArrayInputStream(out.toByteArray()), null, Charset.forName("UTF-8"));
// step 5
document.close();
}