html转为图片(三):Cssbox

  • 引入依赖包
<dependency>
    <groupId>net.sf.cssbox</groupId>
    <artifactId>cssbox</artifactId>
    <version>4.14</version>
</dependency>
  • 案例:
 public static void main(String[] args) throws Exception {
        ImageRenderer render = new ImageRenderer();
        render.setWindowSize(new Dimension(800, 1000), false);
        System.out.println("kaishi");
        String url = new File("G:/index.html").toURI().toString();
        FileOutputStream out = new FileOutputStream(new File("G:"+ File.separator+"cssbox.png"));
        Thread.sleep(10000);
        render.renderURL(url, out, ImageRenderer.Type.PNG);
        System.out.println("OK");
    }
  • 缺点:这个方法,不支持引用的外部js、css;其次,自定义设置的宽度有时候不起作用,代码内部有一个默认的宽度是2400,暂时没找到解决方法

猜你喜欢

转载自blog.csdn.net/qq_36029699/article/details/86507825