html转为图片(一):java本身的API

  • 不需要引用任何的jar包,不支持写在style里面的css和外置的css文件,只能写在标签上,还有也不支持js
  • 案例:只能是html文件的url地址

public class JavaCore {
    public static void generateOutput() throws Exception {
        String url = new File("D:/index.html").toURI().toString();
        JEditorPane ed = new JEditorPane(new URL(url));
        ed.setSize(1000,1300);

        //create a new image
        BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        SwingUtilities.paintComponent(image.createGraphics(),
                ed,
                new JPanel(),
                0, 0, image.getWidth(), image.getHeight());
        //save the image to file
        ImageIO.write((RenderedImage)image, "png", new File("G:/javacore.png"));
    }
    public static void main(String[] args) {
        try {
            generateOutput();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36029699/article/details/86507687
今日推荐