首页 前端知识 java 使用Html2Image将html转图片

java 使用Html2Image将html转图片

2024-06-05 13:06:30 前端知识 前端哥 155 379 我要收藏

" \n" +

" <tr style=“width: 700px;”>\n" +

" <td style=“border:1px solid #e6e6e6;border-top:none;text-align: center;color: #666666;height: 30px;”>交易类型\n" +

" <td style=“border:1px solid #e6e6e6;border-top:none;text-align: center;color: #666666;border-left: 0px;height: 30px;”>捐赠\n" +

" \n" +

" <tr style=“width: 700px;”>\n" +

" <td style=“border:1px solid #e6e6e6;border-top:none;text-align: center;color: #666666;height: 30px;”>交易金额\n" +

" <td style=“border:1px solid #e6e6e6;border-top:none;text-align: center;color: #666666;border-left: 0px;height: 30px;”>“+saleOrder.getProductPrice()+”\n" +

" \n" +

" <tr style=“width: 700px;”>\n" +

" <td style=“border:1px solid #e6e6e6;border-top:none;text-align: center;color: #666666;height: 30px;”>付款人\n" +

" <td style=“border:1px solid #e6e6e6;border-top:none;text-align: center;color: #666666;border-left: 0px;height: 30px;”>“+saleOrder.getUserName()+”\n" +

" " +

“”;

imageGenerator.loadHtml(htmlstr);

BufferedImage bufferedImage = getGrayPicture(imageGenerator.getBufferedImage());

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

try {

ImageIO.write(bufferedImage, “jpg”, outputStream);

String base64Img = Base64.encodeBase64String(outputStream.toByteArray());

String res = “data:image/jpg;base64,” + base64Img.toString();

modelAndView.addObject(“imageres”, res);

} catch (IOException e) {

e.printStackTrace();

}finally {

if(outputStream != null){

try {

outputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

这个是用于重新设置画布背景颜色的

public BufferedImage getGrayPicture(BufferedImage originalImage)

{

BufferedImage grayPicture;

int imageWidth = originalImage.getWidth();

int imageHeight = originalImage.getHeight();

grayPicture = new BufferedImage(imageWidth, imageHeight,

BufferedImage.TYPE_INT_RGB);

ColorConvertOp cco = new ColorConvertOp(ColorSpace

.getInstance(ColorSpace.CS_GRAY), null);

cco.filter(originalImage, grayPicture);

return grayPicture;

}

前端:

效果:

loadUrl(url) (从url载入html)

loadHtml(html) (载入本地html)

saveAsImage(file) (以图片形式保存html)

写在最后

可能有人会问我为什么愿意去花时间帮助大家实现求职梦想,因为我一直坚信时间是可以复制的。我牺牲了自己的大概十个小时写了这片文章,换来的是成千上万的求职者节约几天甚至几周时间浪费在无用的资源上。

复习一周,字节跳动三场技术面+HR面,不小心拿了offer

复习一周,字节跳动三场技术面+HR面,不小心拿了offer

上面的这些(算法与数据结构)+(Java多线程学习手册)+(计算机网络顶级教程)等学习资源

无用的资源上。

[外链图片转存中…(img-sjh3DCgJ-1714302398119)]

[外链图片转存中…(img-HgnnwmhI-1714302398119)]

上面的这些(算法与数据结构)+(Java多线程学习手册)+(计算机网络顶级教程)等学习资源

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

转载请注明出处或者链接地址:https://www.qianduange.cn//article/10807.html
标签
评论
发布的文章

TEGG学习总结

2024-08-07 00:08:45

ajax笔记二

2024-03-12 01:03:25

jQuery 密码验证

2024-08-07 00:08:10

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!