大家好,我是傻明蚕豆,最近搞了一个html转pdf,在这里把知识记录下来,希望对大家有帮助。
废话不多说,直奔主题。首先把你想要生成的pdf内容做成html模版,然后把数据导入到模版中,再生成一个pdf文件。
准备html如下图所示:
html代码:
<html>
<head>
<link rel="stylesheet" href="F:/read/jianli/jianli.css"></link>
</head>
<body >
<div id="div1" style="background-image:url('F:/read/jianli/images/t01ed5a885549b6bf3b.jpg');">
<table border="1" width="100%" height="100%" align="center" valign="middle" align="center" cellpadding="10">
<tr>
<th colspan="7">我的个人简介</th>
</tr>
<tr>
<th>姓名:</th>
<td>${name}</td>
<th>年龄:</th>
<td>${age}</td>
<th>民族:</th>
<td>${nation}</td>
<td rowspan="3" width="100"><img src="F:/read/jianli/images/small.png"/></td>
</tr>
<tr>
<th >出生日期:</th>
<td>${birthday}</td>
<th >政治面貌:</th>
<td>${politicsVisage}</td>
<th >学历:</th>
<td>${educationBackground}</td>
</tr>
<tr>
<th >专业:</th>
<td>${major}</td>
<th >毕业学校:</th>
<td>${school}</td>
<th >邮编:</th>
<td>${postcode}</td>
</tr>
<tr>
<th >爱好:</th>
<td>${hobby}</td>
<th >籍贯:</th>
<td>${nativePlace}</td>
<th>邮箱:</th>
<td colspan="2">${email}</td>
</tr>
<tr>
<th height="160"> 自我介绍:</th>
<td colspan="6">
<p>来自广东省某个镇的一个小村庄里,我爱计算机,我爱...</p>
<p>来自广东省某个镇的一个小村庄里,我爱JAVA,我爱...</p>
<p>来自广东省某个镇的一个小村庄里,我爱Spring全家桶,我爱...</p>
<p>注意:标签必须闭合</p>
</td>
</tr>
</table>
</div>
</body>
</html>
css代码:
th{
background-color: BurlyWood;
}
maven依赖:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.0.3</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.26-incubating</version>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.2</version>
</dependency>
java代码:
private static void test(){
Map<String ,Object> map=new HashMap<>();
map.put("name","傻明蚕豆");
map.put("age","18");
map.put("nation","汉族");
map.put("birthday","19810808");
map.put("politicsVisage","团员");
map.put("educationBackground","大学");
map.put("major","电子技术");
map.put("school","青山大学");
map.put("postcode","528143");
map.put("hobby","吃喝拉撒");
map.put("nativePlace","粤");
map.put("email","1060120317@qq.com");
File modelFile = new File("F:/read/jianli");
String htmlName = "jianli.html";
String cssPath = "F:/read/jianli/jianli.css";
String pdfPath ="F:/read/jianli/"+System.currentTimeMillis()+".pdf";
try {
createPDF(modelFile,htmlName, cssPath, pdfPath, map);
System.out.println("success");
}catch (Exception e){
e.printStackTrace();
}
}
private static void createPDF(File modelFile,String htmlName,String cssPath,String pdfPath,Map<String, Object> paramMap) throws Exception{
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
configuration.setDefaultEncoding("UTF-8"); //这个一定要设置,不然在生成的页面中 会乱码
configuration.setDirectoryForTemplateLoading(modelFile);
//获取或创建一个模版。
Template template = configuration.getTemplate(htmlName);
StringWriter stringWriter = new StringWriter();
BufferedWriter writer = new BufferedWriter(stringWriter);
template.process(paramMap, writer); //把值写进模板
String htmlStr = stringWriter.toString();
writer.flush();
writer.close();
FileOutputStream outFile = new FileOutputStream(pdfPath);
InputStream cssInputStream = new FileInputStream(cssPath);
ByteArrayInputStream htmlInputStream = new ByteArrayInputStream(htmlStr.getBytes("UTF-8"));
Document document = new Document();
try {
PdfWriter pdfWriter = PdfWriter.getInstance(document, outFile);
document.open();
FontProvider provider = new FontProvider();
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, htmlInputStream, cssInputStream, Charset.forName("UTF-8"),provider);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally {
try {
document.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
htmlInputStream.close();
cssInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 字体
*/
private static class FontProvider extends XMLWorkerFontProvider {
public Font getFont(final String fontname, final String encoding, final boolean embedded, final float size, final int style, final BaseColor color) {
BaseFont bf = null;
try {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(bf, size, style, color);
font.setColor(color);
return font;
}
}
运行结果生成如下图:
注意:Html标签必须闭合
这时候发现背景图没有,经过一波操作,改css样式、把背景图放到table、设置浮动,结果还是不行。
希望有大神可以看到帮忙解决这个问题。
谢谢观看,再见!