html转成图片
依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
import org.aspectj.util.FileUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class HtmlToImage {
public static void main(String[] args) throws IOException {
String outPath = "D:\\work\\project\\jar\\wordtopdf-server\\createWordBytemplate\\src\\test\\java\\simple.png";
String htmlTemplateStr = "D:\\work\\project\\jar\\wordtopdf-server\\createWordBytemplate\\src\\test\\java\\simple.html";
String html = FileUtil.readAsString(new File(htmlTemplateStr));
// String html = "<html><body>Hello, World!<p>This is a simple HTML to Image example.</p></body></html>";
try {
BufferedImage image = createImageFromHtml(html);
ImageIO.write(image, "png", new File(outPath));
} catch (IOException e) {
e.printStackTrace();
}
}
public static BufferedImage createImageFromHtml(String html) {
Document doc = Jsoup.parse(html);
String text = doc.body().text();
// Create a BufferedImage
BufferedImage bufferedImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
// Set Background Color
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, 800, 600);
// Set Text properties
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("Arial", Font.PLAIN, 24));
// Draw Text
g2d.drawString(text, 50, 50);
g2d.dispose();
return bufferedImage;
}
}
【Java利用poi-tl实现富文本HTML转Word - CSDN App】https://blog.csdn.net/wdy_2099/article/details/117951909?sharetype=blog&shareId=117951909&sharerefer=APP&sharesource=m0_49128301&sharefrom=link