thymeleaf导出pdf文件

编程

<dependency>

<groupId>org.xhtmlrenderer</groupId>

<artifactId>flying-saucer-pdf</artifactId>

<version>9.0.7</version>

</dependency>

<dependency>

<groupId>org.thymeleaf</groupId>

<artifactId>thymeleaf</artifactId>

<version>2.1.4.RELEASE</version>

</dependency>

2.创建相关的pdf模板,网上不同的创建pdf模板方式,这里用的是html方式,方便后续进行模板调整,

<h1 class="h1_style">标题</h1>

<div >

<div class="row " style="font-size: 1.75rem;padding-left: 25%;">

<div class="div-inline" >班级</div>

<div style="display: inline-block;padding-left: 3.125rem;">人数:<span>55</span></div>

</div>

</div>

通过thymeleaf语法替换后:

<h1 class="h1_style" th:text="${title}"></h1>

<div th:each="contentObj:${contentList}">

<div class="row " style="font-size: 1.75rem;padding-left: 25%;">

<div class="div-inline" th:text="${contentObj.className}"></div>

<div style="display: inline-block;padding-left: 3.125rem;">人数:<span th:text="${contentObj.totalNum}"></span></div>

</div>

</div>

注:html页面需要引用thymeleaf语法组件:<html xmlns:th="http://www.thymeleaf.org">

3.pdf生成工具类

import com.lowagie.text.DocumentException;

import com.lowagie.text.pdf.BaseFont;

import org.springframework.util.ResourceUtils;

import org.thymeleaf.TemplateEngine;

import org.thymeleaf.context.Context;

import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import org.thymeleaf.templateresolver.TemplateResolver;

import org.xhtmlrenderer.pdf.ITextFontResolver;

import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Map;

public class PDFGenerator {

private TemplateResolver templateResolver;

private TemplateEngine templateEngine;

public PDFGenerator(

final String templatePrefix,

final String templateSuffix){

this(templatePrefix, templateSuffix, "HTML5", "UTF-8");

}

public PDFGenerator(

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 PDFGenerator(TemplateResolver templateResolver){

this.templateResolver = templateResolver;

}

public PDFGenerator(TemplateEngine templateEngine){

this.templateEngine = templateEngine;

}

private TemplateEngine getTemplateEngine() {

if(templateEngine == null){

templateEngine = new TemplateEngine();

templateEngine.setTemplateResolver(templateResolver);

}

return templateEngine;

}

/**

* Process the template and generate a PDF of this rendered template.

*

* @param ouputPDF Target pdf file.

* @param template Source template.

* @param model The data for the template.

* @throws FileNotFoundException

* @throws DocumentException

*/

public void generate(File ouputPDF, String template, Map<String, ?> model) throws FileNotFoundException, DocumentException {

final Context ctx = new Context();

ctx.setVariables(model);

final TemplateEngine templateEngine = getTemplateEngine();

String htmlContent = templateEngine.process(template, ctx);

ITextRenderer renderer = new ITextRenderer();

ITextFontResolver fontResolver = renderer.getFontResolver();

try {

//解决中文支持

String path = ResourceUtils.getFile("classpath:templates/simsun.ttf").getPath();

fontResolver.addFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// 解决图片的相对路径问题

//renderer.getSharedContext().setBaseURL("file:/C:/Users/Administrator.WIN7-1610080938/Desktop/word2pdf/");

renderer.setDocumentFromString(htmlContent);

renderer.layout();

renderer.createPDF(new FileOutputStream(ouputPDF));

}

}

引用对应字体文件:simsun.ttf【宋体】,注:linux系统中需要设置其他字体,需要自己动手找资料设置,pdf文件中需要展示中文,需要在html页面中加入样式

 html{ font-family: SimSun;}

4.构建展示数据,数据格式最好是Map类型

  map.put("title","欢迎来到");

map.put("contentList",contentList);

//设置模板路径

PDFGenerator pdfGenerator = new PDFGenerator("templates/",".html");

File file = new File(fileBasePath+filePdfName);

//模板名称

pdfGenerator.generate(file,"temp",map);

 

 

 

以上是 thymeleaf导出pdf文件 的全部内容, 来源链接: utcz.com/z/513563.html

回到顶部