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();
        }
    }
相关推荐
benben0442 分钟前
ReAct模式解读
java·ai
轮到我狗叫了40 分钟前
牛客.小红的子串牛客.kotori和抽卡牛客.循环汉诺塔牛客.ruby和薯条
java·开发语言·算法
正义的大古41 分钟前
OpenLayers常用控件 -- 章节六:全屏控件教程
前端·javascript·html·openlayers
OC溥哥9992 小时前
Flask论坛与个人中心页面开发教程完整详细版
后端·python·flask·html
Volunteer Technology2 小时前
三高项目-缓存设计
java·spring·缓存·高并发·高可用·高数据量
栗子~~2 小时前
bat脚本- 将jar 包批量安装到 Maven 本地仓库
java·maven·jar
Mr.Entropy2 小时前
ecplise配置maven插件
java·maven
叙白冲冲3 小时前
tomcat 为啥能一直运行?不像方法那样结束?
java·tomcat
CoderYanger3 小时前
MySQL数据库——3.2.1 表的增删查改-查询部分(全列+指定列+去重)
java·开发语言·数据库·mysql·面试·职场和发展
迷知悟道3 小时前
java面向对象四大核心特征之抽象---超详细(保姆级)
java·后端