使用Java将HTML转换为图像

3

我遇到了一些问题,使用Java将HTML转换为图像。我正在使用html2image [java]。

它可以创建图像,但问题是它只能在HTML的一小部分上创建图像。我该如何使其创建整个HTML的图像。谢谢。

这是我的代码:

import gui.ava.html.image.generator.HtmlImageGenerator;
import java.io.File;


public class test {
    public static void main(String[] args) {
        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        String uri = new File("C:\\cover.html").toURI().toString();
        imageGenerator.loadUrl(uri);
        imageGenerator.saveAsImage("hello-world.png");
        imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");
    }
}

我使用saveAsHtmlWithMap来保存HTML文件。由程序写入硬盘的HTML文件也很小。
以下是cover.thml的HTML代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="***">
    <head>
        <title></title>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=1200, height=1200"/>
        <link rel="stylesheet" type="text/css" href="main.css"/>
    </head>
    <body id="cover_page">
        <nav id="cover" >
            <ol>
                <li id="nav1">
                    <a>cover</a>
                </li>
            </ol>
        </nav>
    </body>
</html>

1
你能否包含生成的HTML和图片? - Tim Biegeleisen
1
我更新了问题。生成的图像是HTML的一小部分。 - audrey ruaburo
可能与您在HTML中定义的视口有关,所以只有页面的(默认?)可见部分被渲染为图像。 - Uwe Allner
3个回答

2
抱歉,@Pralay,执行以下操作:
imageGenerator.setSize(new Dimension(1024, 768));

imageGenerator.getDefaultSize().setSize(1024, 768);
都没有帮助。
无论如何,html2image使用的ImageRenderer中的默认大小为1024x768。请看ImageRendereImpl类的摘录:
public class ImageRendererImpl implements ImageRenderer {
    public static final int DEFAULT_WIDTH = 1024;
    public static final int DEFAULT_HEIGHT = 768;   
    ...
    private int width = DEFAULT_WIDTH;
    private int height = DEFAULT_HEIGHT;
    private boolean autoHeight = true;
    ...

但是请注意autoHeight字段。在ImageRendererImpl类内部下面你可以看到:

if (autoHeight) {
    // do layout with temp buffer
    Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
    renderer.layout(graphics2D, new Dimension(width, height));
    graphics2D.dispose();

    Rectangle size = renderer.getMinimumSize();
    final int autoWidth = (int) size.getWidth();
    final int autoHeight = (int) size.getHeight();
    bufferedImage = new BufferedImage(autoWidth, autoHeight, imageType);
    dimension = new Dimension(autoWidth, autoHeight);

如果authHeight为true(默认值为true),则会调用org.xhtmlrenderer.simple.Graphics2DRenderer类的getMinimumSize()方法。从相应的Javadoc可以得出,将使用最小可能的区域。这就是为什么您会得到太少的图像的原因。
autoHeight设置为false即可解决此问题。
public class Test {
    public static void main(String[] args) throws {
        File inputFile = new File("/home/me/Temp/cover.html");
        Html2Image imageGenerator = new Html2Image();
        imageGenerator.getParser().load(inputFile);
        imageGenerator.getImageRenderer().setAutoHeight(false);
        imageGenerator.getImageRenderer().saveImage("/home/me/Temp/hello-world.png");
    }
}

我有一个问题要解决:

enter image description here

提示: 我使用了 html2image v. 2.0-SNAPSHOT 成功解决了这个问题。至于 v. 0.9(Maven Central中的版本),你需要修改源代码(v. 0.9 老旧且不灵活)。

提示2: 在纯Java中,你可以尝试类似以下的方法:

public class Example1 {

    private static final int WIDTH = 1204;
    private static final int HEIGHT = 768;

    public static void main(String[] args) throws IOException {
        // open HTML page
        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditable(false);
        URL urlToPage = new File("/home/me/Temp/cover.html").toURI().toURL();
        editorPane.setPage(urlToPage);
        editorPane.setSize(WIDTH, HEIGHT);

        // render the page
        BufferedImage renderedImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
        editorPane.print(renderedImage.getGraphics());

        // write result to file
        ImageIO.write(renderedImage, "PNG", new File("/home/me/Temp/hello-world.png"));
    }
}

PS3 我看到 html2image现在没有维护了(如果要使用v.2.0,需要自己下载和编译)。也许有一些现存的分支库。或者尝试另一个HTML渲染库。


1
我参考了HtmlImageGenerator的源代码,并找到了那些setter方法和默认值。请查看:https://github.com/hkirk/java-html2image/blob/master/html2image/src/main/java/gui/ava/html/image/HtmlImageGenerator.java - deadpool
1
是的,https://github.com/hkirk/java-html2image/ 是原始 html2image 的一个分支。看起来比原始版本的 html2image v. 0.9 要新。只需使用新版本即可解决问题。 - flaz14

1

HtmlImageGenerator默认创建并设置一个java.awt.Dimension对象(800,800)。您可以使用HtmlImageGenerator类的以下setter方法传递自定义大小的新Dimension(x,y):

 public void setSize(Dimension dimension);

我希望它能有所帮助。


0

我对html2image不太熟悉,但可能它只能解析内联CSS,这就是为什么生成的图像与浏览器中呈现的不同的原因。

尝试将main.css的内容添加为内联样式到你的HTML中,然后再保存一张图片。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接