java代码
public static void createPdf(String content,String dest) throws IOException, DocumentException, com.lowagie.text.DocumentException {
ITextRenderer render = new ITextRenderer();
ITextFontResolver fontResolver = render.getFontResolver();
fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 解析html生成pdf
render.setDocumentFromString(content);
//解决图片相对路径的问题
render.getSharedContext().setBaseURL(LOGO_PATH);
render.layout();
render.createPDF(new FileOutputStream(dest));
}
html模板代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
<style>
body{
font-family:SimHei;
}
.blue{
color: blue;
}
.pos{
position:absolute;
left:100px;
top:150px
}
</style>
</head>
<body>
<table border="1">
<tr>
<th>user_address</th>
<th>nick_name</th>
<th>login_name</th>
<th>goods_name</th>
<th>selling_price</th>
<th>goods_cover_img</th>
</tr>
<tr>
<td>${user_address}</td>
<td>${nick_name}</td>
<td>${login_name}</td>
<td>${goods_name}</td>
<td>${selling_price}</td>
<td>${goods_cover_img}</td>
</tr>
</table>
</body>
</html>
注意:html模板中加入body样式字体,缺少无法显示中文,不要删除该样式,删除后无法显示中文
注意:ttf字体库文件和模板文件执行路径在class文件夹下