springboot thymeleaf html转pdf两种实现

本文介绍了在SpringBoot项目中使用Thymeleaf生成HTML,并通过Flying Saucer和OpenHTMLtoPDF两种库将其转换为PDF的过程。对比了两种方法的实现细节和效果,强调了HTML中添加字体、纸张大小设置以及通过CSS处理页眉、页脚和水印的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

thymeleaf是用来获取html数据的,毕竟是个模版,传参最终拼成html的字符串string。

获取html传入给相关组件。在网上找了N多方式。itext5,pdfbox,puppeteer,还有个google header命令行。还有问前端有没有什么好的推荐,前端直接让后端来搞。后两个没测试,看着网上说的效果不错。给我的感觉就是截图,转pdf,具体我并没有实现。itext5需要前端调整它能支持的样式。

        最终选择,我的版本是最新的9.1.5

参考抄袭文章:

SpringBoot Thymeleaf企业级真实应用:使用Flying Saucer结合iText5将HTML界面数据转换为PDF输出(四) 表格中断问题_flying saucer html转pdf表格分页断开-CSDN博客

        <!-- 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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值