springboot 使用 thymeleaf模板引擎 html转pdf,设置边距,设置背景图

  • 引入的jar文件
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>5.0.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.itextpdf/layout -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>8.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>8.0.5</version>
        </dependency>
  • 相关工具类代码
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.layout.font.FontProvider;

import java.io.ByteArrayOutputStream;

public class HtmlToPdfConverter {

    /**
     * HTML 转 PDF
     *
     * @param content       html内容
     * @param marginTop     上边距(可选)
     * @param marginRight   右边距(可选)
     * @param marginBottom  下边距(可选)
     * @param marginLeft    左边距(可选)
     * @param backgroundImgPath 背景图片路径(可选)
     * @return PDF字节数组
     */
    public static byte[] html2Pdf(String content,
                                  Float marginTop,
                                  Float marginRight,
                                  Float marginBottom,
                                  Float marginLeft,
                                  String backgroundImgPath) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            // 创建转换器属性对象
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setCharset("UTF-8");

            // 设置字体提供程序,以支持中文等非拉丁字符集
            FontProvider fontProvider = new FontProvider();
            fontProvider.addSystemFonts(); // 添加系统字体
            converterProperties.setFontProvider(fontProvider);

            // 构建带有样式的 HTML 字符串
            StringBuilder htmlWithStyles = new StringBuilder();
            htmlWithStyles.append("<html><head><style>");

            // 如果有边距参数,则添加到样式中
            if (marginTop != null || marginRight != null || marginBottom != null || marginLeft != null) {
                htmlWithStyles.append("@page { ");
                if (marginTop != null) htmlWithStyles.append("margin-top: ").append(marginTop).append("mm; ");
                if (marginRight != null) htmlWithStyles.append("margin-right: ").append(marginRight).append("mm; ");
                if (marginBottom != null) htmlWithStyles.append("margin-bottom: ").append(marginBottom).append("mm; ");
                if (marginLeft != null) htmlWithStyles.append("margin-left: ").append(marginLeft).append("mm; ");
                htmlWithStyles.append("}");
            }

            // 如果有背景图片路径,则添加到样式中
            if (backgroundImgPath != null && !backgroundImgPath.isEmpty()) {
                htmlWithStyles.append("body { background-image: url('").append(backgroundImgPath).append("');");
                htmlWithStyles.append(" background-repeat: no-repeat; background-position: center; background-size: cover; }");
            }

            htmlWithStyles.append("</style></head><body>").append(content).append("</body></html>");

            // 将 HTML 转换为 PDF 并写入输出流
            HtmlConverter.convertToPdf(htmlWithStyles.toString(), outputStream, converterProperties);
        } catch (Exception e) {
            System.out.println("生成失败:" + e.getMessage());
        }
        return outputStream.toByteArray();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值