java将html转成图片代码实例(html2image)

 更新时间:2024年09月12日 10:58:58   作者:归去来 兮  
这篇文章主要介绍了java将html转成图片的相关资料,在Java开发中,将HTML转换为图片可以使用html2image库,文中通过代码及图文介绍的非常详细,需要的朋友可以参考下

前言

最近有一个需求需要根据指定的样式生成图片,使用java原生技术有些麻烦,所以上网搜了下案例,最后发现最好用的还是html2image,这里进行简单总结下。

1.导入jar

 <!-- 用于将html转图片-->
 <dependency>
     <groupId>gui.ava</groupId>
     <artifactId>html2image</artifactId>
     <version>2.0.1</version>
 </dependency>

2.代码

下面是示例代码,这里使用String的方式传入html信息,除了直接传入还支持传入文件流、传入文件等其他方式,差距不大,这里不列举其他方式的细节了。

import gui.ava.html.parser.HtmlParser;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRenderer;
import gui.ava.html.renderer.ImageRendererImpl;

public class TestTableToImage {
    public static void main(String[] args) throws Exception {
        HtmlParser htmlParser = new HtmlParserImpl();
        String HtmlTemplateStr = "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>入库单</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "\t<!--外层的表格 -->\n" +
                "\t<table style=\"border: 2px solid black;padding: 10px \"  cellpadding=\"5\" align=\"center\" >\n" +
                "\t\n" +
                "\t\t<tr >\n" +
                "\t\t\t<th style=\"font-size:25px;height: 60px;\" align=\"center\">入库单</th>\n" +
                "\t\t</tr>\n" +
                "\t\t\n" +
                "\t\t<tr>\n" +
                "\t\t\t<th>\n" +
                "\t\t\t\t<table border=\"1\" style=\"border-color: #ccc;\" cellspacing=\"0\" cellpadding=\"15\" width=\"900px\">\n" +
                "\t\t\t\t\t<tr  style=\"color: #696969;height: 60px;\">\n" +
                "\t\t\t\t\t\t<th style=\"width: 150px;\">调度单号</th> <th colspan=\"2\" style=\"width: 300px;\">调度单号22</th>\n" +
                "\t\t\t\t\t\t<th style=\"width: 150px;\">客户名称</th> <th colspan=\"2\" style=\"width: 300px;\">客户名称22</th>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t\n" +
                "\t\t\t\t\t<tr  style=\"color: #696969;height: 50px;\">\n" +
                "\t\t\t\t\t\t<th >车牌照号</th> <th colspan=\"2\" style=\"width: 300px;\">车牌照号22</th>\n" +
                "\t\t\t\t\t\t<th>装运时间</th> <th colspan=\"2\" style=\"width: 300px;\">装运时间22</th>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t<tr height=\"50px;height: 50px;\">\n" +
                "\t\t\t\t\t\t<td colspan=\"6\" style=\"font-size:23px;color: #696969\" align=\"center\">货物信息</td>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t<tr  style=\"color: #696969;height: 50px;\">\n" +
                "\t\t\t\t\t\t<th>钢卷号</th> <th colspan=\"2\" style=\"width: 300px;\">钢卷号22</th>\n" +
                "\t\t\t\t\t\t<th>磅单号</th> <th colspan=\"2\" style=\"width: 300px;\">磅单号22</th>\n" +
                "\t\t\t\t\t\t\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t<tr  style=\"color: #696969;height: 50px;\">\n" +
                "\t\t\t\t\t\t<th>材质</th><th style=\"width: 150px;\">材质22</th>\n" +
                "\t\t\t\t\t\t<th>规格型号</th><th style=\"width: 150px;\">规格型号22</th>\n" +
                "\t\t\t\t\t\t<td>货物名称</td><th style=\"width: 150px;\">货物名称22</th>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t<tr  style=\"color: #696969;height: 50px;\" align=\"center\">\n" +
                "\t\t\t\t\t\t\n" +
                "\t\t\t\t\t\t<td>件数</td> <th colspan=\"2\" style=\"width: 300px;\">件数22</th>\n" +
                "\t\t\t\t\t\t<td>重量</td> <th colspan=\"2\" style=\"width: 300px;\">重量22</th>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t<tr  style=\"color: #696969;height: 50px;\" align=\"center\">\n" +
                "\t\t\t\t\t\t<td colspan=\"6\" style=\"font-size:23px;color: #696969\">客户信息</td>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t<tr  style=\"color: #696969;height: 50px;\">\n" +
                "\t\t\t\t\t\t<th>收货联系人</th> <th colspan=\"2\" style=\"width: 300px;\">收货联系人22</th>\n" +
                "\t\t\t\t\t\t<th>收货人电话</th> <th colspan=\"2\" style=\"width: 300px;\">收货人电话22</th>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t<tr  style=\"color: #696969;height: 50px;\">\n" +
                "\t\t\t\t\t\t<th >库房名称</th> <th colspan=\"2\" style=\"width: 300px;\">库房名称22</th>\n" +
                "\t\t\t\t\t\t<th >库房地址</th> <th colspan=\"2\" style=\"width: 300px;\">库房地址22</th>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t\n" +
                "\t\t\t\t\t<!--\n" +
                "\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\">\n" +
                "\t\t\t\t\t\t<th >收货地点</th> <th colspan=\"5\" style=\"width: 750px;\">收货地点22</th>\n" +
                "\t\t\t\t\t</tr>\n" +
                "\t\t\t\t\t -->\n" +
                "\t\t\t\t</table>\n" +
                "\t\t\t</th>\n" +
                "\t\t\t\n" +
                "\t\t</tr>\n" +
                "\t\t\n" +
                "\t\t<tr style=\"height: 150px;\">\n" +
                "\t\t\t<th></th>\n" +
                "\t\t</tr>\n" +
                "\t\t\n" +
                "\t</table>\n" +
                "</body>\n" +
                "</html>";
        htmlParser.loadHtml(HtmlTemplateStr);

        ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
        imageRenderer.setWidth(955);
        imageRenderer.saveImage("D:\\66.png");

    }
}

3.展示结果

执行上面的代码生成的图片如下,可以看到效果还是不错的,相比于其他的三方jar来说这个jar还是挺方便,笔者也是尝试了多个jar最后决定用的这个三方包,推进使用,可以看到生成的效果还是不错的。

4.注意事项

我去maven仓库中找这个jar时,发现这个包已经很多年没有更新了,不过还是很好用,可以不用在意这个事。

  • 需要注意的是有一些格式如果不放到style中直接写可能识别不了,所以在使用时尽量所有格式写在style中,防止不生效
  • 此外还有一点,自动生成图片的宽度需要自己调整,这个宽度可能会有一些白边,使用imageRenderer.setWidth进行调整宽度即可

附:转换jpg变红的问题处理

public class ImageRendererSubImpl extends ImageRendererImpl {

    public ImageRendererSubImpl(DocumentHolder documentHolder) {
        super(documentHolder);
    }

    private String getImageFormat(String filename) {
        if (this.getImageFormat() != null) {
            return this.getImageFormat();
        } else {
            return filename != null ? FormatNameUtil.formatForFilename(filename) : FormatNameUtil.getDefaultFormat();
        }
    }

    private FSImageWriter getImageWriter(String imageFormat) {
        FSImageWriter imageWriter = new FSImageWriter(imageFormat);
        imageWriter.setWriteCompressionMode(this.getWriteCompressionMode());
        imageWriter.setWriteCompressionQuality(this.getWriteCompressionQuality());
        imageWriter.setWriteCompressionType(this.getWriteCompressionType());
        return imageWriter;
    }

    public void saveImage(File file) {
        try {
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
            this.save(outputStream, file.getName(), true);
        } catch (IOException var3) {
            throw new RenderException("IOException while rendering image to " + file.getAbsolutePath(), var3);
        }
    }

    public void saveImage(String filename) {
        this.saveImage(new File(filename));
    }

    private void save(OutputStream outputStream, String filename, boolean closeStream) {
        try {
            String imageFormat = this.getImageFormat(filename);
            FSImageWriter imageWriter = this.getImageWriter(imageFormat);
            BufferedImage bufferedImage = this.getBufferedImage(getImageType(imageFormat));
            imageWriter.write(bufferedImage, outputStream);
        } catch (IOException var15) {
            throw new RenderException("IOException while rendering image", var15);
        } finally {
            if (closeStream) {
                try {
                    outputStream.close();
                } catch (IOException var14) {
                    ;
                }
            }

        }
    }

    /**
     * 获取图像类型
     * 根据图像的格式
     */
    public int getImageType(String imageFormat){
        if ("jpg".equalsIgnoreCase(imageFormat)){
            return BufferedImage.TYPE_3BYTE_BGR;
        }
        if ("bmp".equalsIgnoreCase(imageFormat)){
            return BufferedImage.TYPE_INT_RGB;
        }
        return BufferedImage.BITMASK;
    }

}

总结

到此这篇关于java将html转成图片的文章就介绍到这了,更多相关java将html转成图片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Mybatis-plus在新增或修改时如何自动插入或修改某个字段值

    Mybatis-plus在新增或修改时如何自动插入或修改某个字段值

    文章讲述了MyBatis-Plus中MetaObjectHandler接口的使用方法,通过在实体类中添加@TableField注解并实现MetaObjectHandler接口的insertFill和updateFill方法,为新增和更新操作时自动填充公共字段如createTime、updateTime等的默认值
    2026-04-04
  • Java String类和StringBuffer类的区别介绍

    Java String类和StringBuffer类的区别介绍

    这篇文章主要介绍了Java String类和StringBuffer类的区别, 关于java的字符串处理我们一般使用String类和StringBuffer类有什么不同呢,下面我们一起来看看详细介绍吧
    2022-03-03
  • Java实现单向链表的基本功能详解

    Java实现单向链表的基本功能详解

    这篇文章主要给大家介绍了关于Java实现单向链表基本功能的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • java如何远程加载class文件

    java如何远程加载class文件

    这篇文章主要介绍了java如何远程加载class文件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Java中的CompletableFuture使用解析

    Java中的CompletableFuture使用解析

    这篇文章主要介绍了Java中的CompletableFuture使用解析,为什么CompletableFuture要定制化线程池,因为默认的线程池是ForkJoinPool,这个线程池的最大线程数默认是你的电脑的线程数数减1,假如我线程电脑是4核8线程的,ForkJoinPool的最大线程数就是7,需要的朋友可以参考下
    2024-01-01
  • IDEA中Mybatis的MGB使用逆向工程配置的详细教程

    IDEA中Mybatis的MGB使用逆向工程配置的详细教程

    这篇文章主要介绍了IDEA中Mybatis的MGB使用逆向工程配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • 简单了解java数组传递方法

    简单了解java数组传递方法

    这篇文章主要介绍了简单了解java数组传递方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java Spring AOP详解

    Java Spring AOP详解

    这篇文章主要介绍了Java的Spring框架中的AOP实现实例,AOP面向切面编程其实也可以被看作是一个设计模式去规范项目的结构,需要的朋友可以参考下
    2021-09-09
  • Java实现后端跨域的常见解决方案

    Java实现后端跨域的常见解决方案

    跨源资源共享(CORS——Cross-Origin Resource Sharing,跨源资源共享,或通俗地译为跨域资源共享)是一种基于 HTTP 头的机制,跨域的解决方案有很多种,前后端都有,本文给大家主要介绍Java实现后端跨域的常见解决方案,需要的朋友可以参考下
    2024-04-04
  • Spring事务Transaction配置的五种注入方式详解

    Spring事务Transaction配置的五种注入方式详解

    这篇文章主要介绍了Spring事务Transaction配置的五种注入方式详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04

最新评论