java 使用Html2Image将html转图片

String htmlstr = “<table style=“width: 700px;font-size:16px;font-family: ‘Microsoft YaHei’;” cellpadding=“0” cellspacing=“0”>\n” +

" \n" +

" <th style=“background-color: #f2f2f2;height: 30px;width: 700px; border:1px solid #e6e6e6;border-top:none;text-align: center;color: #333333;” colspan=“2”>收据\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;”>“+DateUtil.format(saleOrder.getCreateTime(), “yyyy-MM-dd HH:mm:ss”)+”\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.getOrderCode()+”\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;”>捐赠\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;

}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值