Html 转pdf

将html格式的文件转为pdf文件

项目依赖jar 包

xml 复制代码
<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.11</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>4.0.3</version>
        </dependency>

静态文件html文件模板:

xml 复制代码
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>2</title>
    <style>
        /*这是将打印时的页面大小设置为A4大小,并保留好一定的边距 210×297mm*/
        /*@page {*/
        /*    size: 21cm 29.7cm;*/
        /*    font-size: 14pt;*/
        /*    color: black;*/
        /*    line-height: 1.4;*/
        /*    text-align: justify;*/
        /*    margin: 0;*/
        /*    padding: 0;*/
        /*}*/
        /*body {*/
        /*    font-family: FZLanTingHei-L-GBK;*/
        /*    width: 21cm;*/
        /*    height: 29.7cm;*/
        /*    margin: 0;*/
        /*    padding: 0;*/
        /*    overflow: hidden;*/
        /*}*/
        /*img{*/
        /*    width: 21cm;*/
        /*    height: 29.7cm;*/
        /*}*/
    </style>
</head>
<body>
<h1 th:text="${title}">Default Title</h1>
<p th:text="${content}">Default Content</p>
<div align="center">
    <img th:src="${imagePath}" alt="这是一张美丽的风景照片"/>

</div>
</body>
</html>

代码如下:

java 复制代码
public void generatePdf(HttpServletResponse response) throws IOException {
        // 将图片转换为Base64编码
//        byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
//        String base64Image = Base64.getEncoder().encodeToString(imageBytes);
        // 动态数据
        Map<String, Object> data = new HashMap<>();
        data.put("title", "Hello, World!");
        data.put("content", "This is a dynamic content.");
        data.put("imagePath", imagePath); // 路径
        // 使用 Thymeleaf 渲染模板
        Context context = new Context();
        context.setVariables(data);
        String htmlContent = templateEngine.process("templates", context);

        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            // 读取HTML模板
            HtmlConverter.convertToPdf(htmlContent, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
相关推荐
我命由我1234512 小时前
Java NIO 编程 - NIO Echo Server、NIO Client(NIO 异步客户端、NIO Selector 异步客户端)
java·开发语言·网络·java-ee·intellij-idea·intellij idea·nio
嗯、.12 小时前
使用Itext9生成PDF水印,兼容不同生成引擎的坐标系(如: Skia、OpenPDF)
java·pdf·itextpdf·openpdf·坐标变换矩阵
拓端研究室14 小时前
专题:2025AI产业全景洞察报告:企业应用、技术突破与市场机遇|附920+份报告PDF、数据、可视化模板汇总下载
大数据·人工智能·pdf
南风微微吹14 小时前
2026年新大纲普通话考试真题题库50套PDF电子版
pdf·普通话
断剑zou天涯15 小时前
【算法笔记】窗口内最大值或最小值的更新结构
java·笔记·算法
m***667315 小时前
SQL 实战—递归 SQL:层级结构查询与处理树形数据
java·数据库·sql
鲸沉梦落17 小时前
Java中的Stream
java
yihuiComeOn17 小时前
[源码系列:手写Spring] AOP第二节:JDK动态代理 - 当AOP遇见动态代理的浪漫邂逅
java·后端·spring
Porunarufu18 小时前
Java·关于List
java·开发语言
靠沿18 小时前
Java数据结构初阶——Collection、List的介绍与ArrayList
java·数据结构·list