大家好,我是傻明蚕豆,最近搞了一个html转pdf,在这里把知识记录下来,希望对大家有帮助。
废话不多说,直奔主题。首先把你想要生成的pdf内容做成html模版,然后把数据导入到模版中,再生成一个pdf文件。
准备html如下图所示:
html代码:
我的个人简介 | ||||||
---|---|---|---|---|---|---|
姓名: | ${name} | 年龄: | ${age} | 民族: | ${nation} | |
出生日期: | ${birthday} | 政治面貌: | ${politicsVisage} | 学历: | ${educationBackground} | |
专业: | ${major} | 毕业学校: | ${school} | 邮编: | ${postcode} | |
爱好: | ${hobby} | 籍贯: | ${nativePlace} | 邮箱: | ${email} | |
自我介绍: |
来自广东省某个镇的一个小村庄里,我爱计算机,我爱...
来自广东省某个镇的一个小村庄里,我爱JAVA,我爱...
来自广东省某个镇的一个小村庄里,我爱Spring全家桶,我爱...
注意:标签必须闭合
|
css代码:
th{
background-color: BurlyWood;
}
maven依赖:
java代码:
private static void test(){
Map
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","[email protected]");
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
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、设置浮动,结果还是不行。
希望有大神可以看到帮忙解决这个问题。
谢谢观看,再见!