Java HTML转图片(FreeMarker + html2img)

Java HTML转图片(FreeMarker + html2img),第1张

第一步

引入pom.xml依赖。

<!-- html转img -->
<dependency>
    <groupId>gui.ava</groupId>
    <artifactId>html2image</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
第二步

在【resources】目录下,创建一个文件夹【templates】并在该目录下创建一个ftl文件。

demo.ftl文件内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>药品列表</title>
</head>
<body>
<table style="color:red">
    <tr>
        <td>药品名称</td>
        <td>类型</td>
    </tr>
    <tr>
        <td>${name}</td>
        <td>${type}</td>
    </tr>
</table>
</body>
</html>
第三步

写一个Html转换工具类。

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import gui.ava.html.Html2Image;
import gui.ava.html.renderer.ImageRenderer;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Html转换图片
 *
 * @author :Mall
 * @date :Created in 2022-04-25
 */
public class HtmlConvertImgHelper {
    Configuration configuration;

    /**
     * 初始化
     */
    public HtmlConvertImgHelper() {
        if (configuration == null) {
            configuration = BeanHelper.getBean(Configuration.class);
        }
    }

    /**
     * freemarker转Image
     *
     * @param fileName   ftl文件名称,需要在resources/templates目录下
     * @param formatType
     * @return
     * @throws IOException
     */
    public byte[] htmlConvertImg(String fileName, Object map, String formatType) throws IOException, TemplateException {
        String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(fileName), map);
        return htmlConvertImg(htmlText, formatType);
    }

    /**
     * 根据HTML内容转Image
     *
     * @param htmText    HTML文本字符串
     * @param formatType 图片类型
     */
    public byte[] htmlConvertImg(String htmText, String formatType) throws IOException {
        //最终返回的byte流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Html2Image html2Image = Html2Image.fromHtml(htmText);
        ImageRenderer imageRenderer = html2Image.getImageRenderer();
        BufferedImage grayPicture = imageRenderer.getBufferedImage(BufferedImage.TYPE_INT_RGB);
        ImageIO.write(grayPicture, formatType, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }
}
第四步

写测试类测试功能是否可用。

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@Slf4j
public class Test {
   @org.junit.Test
    public void ccc() throws IOException, TemplateException {
        Map<String, String> map = new HashMap<>();
        map.put("name", "鹤顶红");
        map.put("type", "毒药");
    
        byte[] bytes = new HtmlConvertImgHelper().htmlConvertImg("demo.ftl", map, "png");
        OutputStream os = new FileOutputStream("G:/text.png");
        os.write(bytes, 0, bytes.length);
        os.flush();
        os.close();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/735633.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-27
下一篇2022-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存