网上找到的html2image教程都是0.9版本的,但是该版本转换时img标签宽高样式会失效,因此改用2.0.1版本。该版本可以解决样式问题,但写法有所不同,记录一下。
引入依赖:
<dependency>
<groupId>gui.ava</groupId>
<artifactId>html2image</artifactId>
<version>2.0.1</version>
</dependency>
代码实现:
String htmlStr = "<p>这是一张图片<img style=\"width:118px;height:65px\" src=\"https://xxx.png\"></img></p>";
Html2Image html2Image = Html2Image.fromHtml(htmlStr);
BufferedImage bufferedImage = html2Image.getImageRenderer().getBufferedImage();
ImageIO.write(bufferedImage, "png", new File("D:\\88.png"));