使用itext html2pdf 将html转换成pdf
thymeleaf html模板生成html
html2pdf html转pdf
之前有试过 xmlworker, flying-saucer-pdf, 但是对css3不太支持.
看itext 官网介绍是支持css3和html5的.具体移步itext官方网站: https://itextpdf.com/en/products/itext-7/pdfhtml
多html合并一个pdf也是支持的.中文支持
完整代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;
public class Html2PdfUtil {
private TemplateResolver templateResolver;
private TemplateEngine templateEngine;
public Html2PdfUtil(final String templatePrefix, final String templateSuffix) {
this(templatePrefix, templateSuffix, "HTML5", "UTF-8");
}
public Html2PdfUtil(final String templatePrefix, final String templateSuffix, final String templateMode,
final String templateEncoding) {
this(new ClassLoaderTemplateResolver());
this.templateResolver.setPrefix(templatePrefix);
this.templateResolver.setSuffix(templateSuffix);
this.templateResolver.setTemplateMode(templateMode);
this.templateResolver.setCharacterEncoding(templateEncoding);
}
public Html2PdfUtil(TemplateResolver templateResolver) {
this.templateResolver = templateResolver;
}
public Html2PdfUtil(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}
private TemplateEngine getTemplateEngine() {
if (templateEngine == null) {
templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
}
return templateEngine;
}
/**
* html转换pdf
*
* @param ouputPDF
* @param template
* @param model
* @param pageSize default A4
*/
public void html2Pdf(File ouputPDF, String template, Map<String, ?> model, PageSize pageSize) {
final Context ctx = new Context();
ctx.setVariables(model);
String htmlContent = getTemplateEngine().process(template, ctx);
if (model.get("specifyPage") != null) { // 动态替换@page配置 (@page为html打印选项配置)
htmlContent = htmlContent.replace("@page{}", model.get("specifyPage").toString());
}
/**
* 如果html有换页的话,可以添加一个class会强制下一页; 如: .pageNext{page-break-after: always;}
**/
/** ***************** 开始转换pdf**************************** **/
try {
FileOutputStream outputStream = new FileOutputStream(ouputPDF);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));
pdfDoc.setDefaultPageSize(pageSize == null ? PageSize.A4 : pageSize);// 自定义pageSize
ConverterProperties props = new ConverterProperties();
FontProvider fp = new FontProvider();
fp.addFont("templates/simsun.ttf"); // 添加字体文件
props.setFontProvider(fp);
HtmlConverter.convertToPdf(htmlContent, pdfDoc, props);
pdfDoc.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("转换pdf出错", e);
}
}
private final static Logger logger = LoggerFactory.getLogger(Html2PdfUtil.class);
}