thymeleaf是用来获取html数据的,毕竟是个模版,传参最终拼成html的字符串string。
获取html传入给相关组件。在网上找了N多方式。itext5,pdfbox,puppeteer,还有个google header命令行。还有问前端有没有什么好的推荐,前端直接让后端来搞。后两个没测试,看着网上说的效果不错。给我的感觉就是截图,转pdf,具体我并没有实现。itext5需要前端调整它能支持的样式。
最终选择,我的版本是最新的9.1.5
参考抄袭文章:
<!-- html 转 pdf 需要用的jar -->
<!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-itext5 -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>${flying-saucer-pdf-itext5.version}</version>
</dependency>
代码抄袭就不展示了,我主要是把thymleaf放到了业务工程,pdf转换是单独的插件。大致截图
业务端:
代码抄袭简单,我又试了另外一种,openhtmltopdf
<!-- https://mvnrepository.com/artifact/com.openhtmltopdf/openhtmltopdf-core -->
<!-- ALWAYS required. -->
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-core</artifactId>
<version>1.0.10</version>
</dependency>
<!-- Required for PDF output. -->
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>1.0.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.jsoup.nodes.Document;
import org.springframework.util.ResourceUtils;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class PdfUtil {
public static void buildPdf( String htmlContent) throws Exception {
Document document = Jsoup.parse(htmlContent);
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useFont(ResourceUtils.getFile("classpath:font/SimSun.ttc"), "simsun");
builder.useFont(ResourceUtils.getFile("classpath:font/msyh.ttc"), "msyh");
builder.useFont(ResourceUtils.getFile("classpath:font/msyhl.ttc"), "msyhl");
builder.useFont(ResourceUtils.getFile("classpath:font/msyhbd.ttc"), "msyhbd");
builder.useFastMode();
// builder.withHtmlContent(htmlContent, "").toString();
builder.withW3cDocument(new W3CDom().fromJsoup(document), "");
String outputPath = "D:\\svndocs\\4444.pdf";
try (OutputStream os = new FileOutputStream(outputPath)) {
builder.toStream(os);
builder.run();
}catch (Exception e)
{
e.printStackTrace();
}
}
public static void buildPdf2( String htmlContent) throws Exception {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useFont(ResourceUtils.getFile("classpath:font/SimSun.ttc"), "simsun");
builder.useFont(ResourceUtils.getFile("classpath:font/simsunb.ttf"), "simsunb");
builder.useFont(ResourceUtils.getFile("classpath:font/msyh.ttc"), "msyh");
builder.useFastMode();
builder.withHtmlContent(htmlContent, "").toString();
String outputPath = "D:\\svndocs\\55555.pdf";
try (OutputStream os = new FileOutputStream(outputPath)) {
builder.toStream(os);
builder.run();
}catch (Exception e)
{
e.printStackTrace();
}
}
两种写法效果一样。
写博客记录肯定是有不一样的,那么切记,html里一定要带上字体,纸张大小也可以。甚至页眉页脚水印都可以通过css来设置.尤其openhtmltopdf,代码没有直接支持水印什么的,就是让你在HTML里用样式处理。oepnhtmltopdf比上边第一种方式简单,生成的pdf也比第一种清晰,不知道第一种哪里可以设置。毕竟我只会百度,下次遇到直接博客里抄.
<style>
@page {
size: A4; /* 设置页面大小为A4 */
margin: 10mm; /* 设置页面边距为30毫米 */
}
html,body{
font-family: simsun,SimSun,simsunb,msyh;
}
</style>