Java中将Html转图片

方式一

引入Html2Image的Maven依赖

<dependency>
	<groupId>com.github.xuwei-kgroupId>
	<artifactId>html2imageartifactId>
	<version>0.1.0version>
dependency>

import gui.ava.html.image.generator.HtmlImageGenerator;

public static void main(String[] args) {
	HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
    //加载html模版
    imageGenerator.loadHtml(htmlTemplate);
    //把html写入到图片
    imageGenerator.saveAsImage("E:\\hello-world.png");
}

方法二(推荐)

<dependency>
	<groupId>gui.avagroupId>
	<artifactId>html2imageartifactId>
	<version>2.0.1version>
dependency>
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 static void main(String[] args) {
	HtmlParser htmlParser = new HtmlParserImpl();
	htmlParser.loadHtml(HtmlTemplateStr);
	// html 是我的html代码
	ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
	imageRenderer.saveImage("E:\\lcxq1.png");
}
static String HtmlTemplateStr = "\t
\n" + "\t\t这个是一个div\n" + "\t\t

标题

\n" + "\t\t
    \n" + "\t\t\t
  1. a
  2. \n" + "\t\t
\n" + "\t\t\n" + "\t
";

转换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;
    }

}

参考:
https://blog.csdn.net/superit401/article/details/82629524
https://www.cnblogs.com/zxf330301/p/5666592.html
https://www.cnblogs.com/wikid/p/3913070.html
https://blog.csdn.net/lijinzhou2017/article/details/88714381

jar包下载:https://download.csdn.net/download/qq_25775675/12511219

方法三(推荐)

使用 wkhtmltox 进行转换:
https://blog.csdn.net/qq_25775675/article/details/110425304

你可能感兴趣的:(Java,java,html,html转图片)