需要引用包,在pom.xml中添加引用:
<dependency>
<groupId>net.sf.cssbox</groupId>
<artifactId>cssbox</artifactId>
<version>4.12</version>
</dependency>
实现代码:
package com.dx.project01; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import org.fit.cssbox.demo.ImageRenderer;
import org.xml.sax.SAXException; /**
* Hello world!
*/
public class App {
public static void main(String[] args) throws IOException, SAXException {
ImageRenderer render = new ImageRenderer();
System.out.println("start...");
String url = "https://mvnrepository.com/artifact/net.sf.cssbox/cssbox/4.12";
FileOutputStream out = new FileOutputStream(new File("D:" + File.separator + "html.png"));
render.renderURL(url, out, ImageRenderer.Type.PNG);
out.close();
System.out.println("OK");
}
}
示例:实现截图地址https://mvnrepository.com/artifact/net.sf.cssbox/cssbox/4.12
截图效果: