路径问题值得注意,我采用的是通过类找的路径,读取freemarker代码要求也很严格
package com.yiyon.assets.utils;
import com.itextpdf.text.DocumentException;
import com.yiyon.assets.entity.AssetsRepair;
import com.yiyon.assets.web.ViewProgress;
import freemarker.template.Configuration;
import freemarker.template.Template;
import net.roseboy.jeee.workflow.entity.Task;
import net.roseboy.jeee.workflow.service.AuditBussService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.itextpdf.text.pdf.BaseFont;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;
public class JavaToPdfFreemarker {
@Autowired
private AuditBussService auditBussService;
private static final String DEST = "F:/test.pdf";
private static final String HTML = "print-repair.ftl";
// private static final String FONT = "simhei.ttf";
private static final String LOGO_PATH = "file://"+/*PathUtil.getCurrentPath()+*/"/logo.png";
private static Configuration freemarkerCfg = null;
static {
freemarkerCfg =new Configuration();
//freemarker的模板目录
try {
freemarkerCfg.setClassForTemplateLoading(JavaToPdfFreemarker.class,"/templates");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
//获取当前路径F:\yiyon\yiyon-server-tianjintaida
// System.getProperty("user.dir");
AssetsRepair bill = new AssetsRepair();
bill.setCode("ZCWX20200710");
bill.setBillDate(new Date());
bill.setDemand("dfjjakfjakfksajf");
bill.setBudget(new BigDecimal(20000));
List<Task> tasks = new ArrayList<>();
Map<String,Object> data = new HashMap();
data.put("bill",bill);
data.put("list", ViewProgress.getList(tasks));
String content = JavaToPdfFreemarker.freeMarkerRender(data,HTML);
JavaToPdfFreemarker.createPdf(content,DEST);
}
/**
* freemarker渲染html
*/
public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
Writer out = new StringWriter();
try {
// 获取模板,并设置编码方式
Template template = freemarkerCfg.getTemplate(htmlTmp);
template.setEncoding("UTF-8");
// 合并数据模型与模板
template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
out.flush();
return out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
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));
}
}
对于freemarker不显示中文的问题,除了引入字体,在标签上加是style="font-family=’ ‘’"之外,没发现其他的方法,但这种方法也不是完全管用。