读取HTML模板转流生成图片

读取HTML模板转流生成图片

通过读取HTML定义好的模板, 动态装载数据, 获取到输入流, 然后生成PNG图片

1.0 依赖

<!-- 图片上传 start -->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.4.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-io</artifactId>
                    <groupId>commons-io</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--thymeleaf模板依赖-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
	   <!--html转图片-->
        <dependency>
            <groupId>net.sf.cssbox</groupId>
            <artifactId>cssbox</artifactId>
            <version>5.0.0</version>
        </dependency>
<!-- 图片上传 end -->

2.0 生成图片工具类

同时使用到了自定义加载项目目录中的字体文件, 解决部署到服务器中文乱码

字体文件与HTML模板都在resource/目录下各种文件夹

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.*;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @Author: ZhiHao
 * @Date: 2021/8/6 10:51
 * @Description: 生成图片
 * @Versions 1.0
 **/
@Component
@Lazy
@Slf4j
public class GeneratePictureUtil implements InitializingBean {
   

    @Autowired
    private ImageUpLoadServiceApi imageUpLoadServiceApi;

    private GraphicsEnvironment graphicsEnvironment;
    private Font fuFont;
    private TemplateEngine engine;

    /**
     * Java原生-生成表格带数据图片
     *
     * @param generativeImageData 生成表格数据
     * @return UploadIMGResp 图片
     * @author: ZhiHao
     * @date: 2021/8/6
     */
    public UploadIMGResp graphicsTableGeneration(GenerativeTableImageData generativeImageData) {
   
        Graphics2D graphics = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        UploadIMGResp uploadImg = null;
        try {
   
            List<List<String>> twoDimensionalList = this.getTwoDimensionalList(generativeImageData);
            // 字体大小
            int fontTitileSize = 15;
            // 横线的行数 - (内容值长度 +1标题)
            int totalrow = twoDimensionalList.size() + 1;
            // 竖线的行数
            int totalcol = twoDimensionalList.get(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懵懵懂懂程序员

如果节省了你的时间, 请鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值