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();
        }
    }
相关推荐
@yanyu66616 分钟前
springboot实现查询学生
java·spring boot·后端
ascarl201021 分钟前
准确--k8s cgroup问题排查
java·开发语言
magic 24525 分钟前
Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误
java
爱敲代码的憨仔32 分钟前
分布式协同自动化办公系统-工作流引擎-流程设计
java·flowable·oa
纪元A梦1 小时前
分布式拜占庭容错算法——PBFT算法深度解析
java·分布式·算法
卿着飞翔1 小时前
RabbitMQ入门4.1.0版本(基于java、SpringBoot操作)
java·rabbitmq·java-rabbitmq
陈阿土i1 小时前
SpringAI 1.0.0 正式版——利用Redis存储会话(ChatMemory)
java·redis·ai·springai
安全系统学习1 小时前
【网络安全】Qt免杀样本分析
java·网络·安全·web安全·系统安全
鱼馅饼2 小时前
vscode使用系列之快速生成html模板
ide·vscode·html
SoFlu软件机器人2 小时前
智能生成完整 Java 后端架构,告别手动编写 ControllerServiceDao
java·开发语言·架构