使用java将HTML转换为图像
我在使用java将html转换为图像时遇到了一些问题 我在使用html2image[java] 它创建一个图像,但问题是它只在html的一小部分上创建一个图像。如何制作整个html的图像。多谢各位 这是我的密码使用java将HTML转换为图像,java,Java,我在使用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) {
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>
盖
我对html2image没有经验,但可能它只能解析内联CSS,这就是原因,因为生成的图像与浏览器中呈现的图像不同
尝试将包含
main.css
内容的内联样式添加到HTML中,然后再次保存图像。HtmlImageGenerator默认情况下创建并设置(800800)的java.awt.Dimension对象。
您可以使用以下HtmlImageGenerator类的setter传递自定义大小的新维度(x,y):
public void setSize(Dimension dimension);
我希望有帮助。@Pralay,不幸的是
imageGenerator.setSize(新尺寸(1024768))代码>
及
imageGenerator.getDefaultSize().setSize(1024768)代码>
没有帮助
无论如何,html2image使用的ImageRenderer
中的默认大小是1024x768。查看ImageRenderImpl
class中的摘录:
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
字段。在下面的ImageRenderImpl
类内部,您可以看到:
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
)getMinimumSize()
将调用org.xhtmlender.simple.graphics2denderer
类的方法。从图中可以得出结论,将使用尽可能小的面积。这就是为什么你得到的图像太少
将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");
}
}
所以我有
PS:我在html2image的帮助下调查并解决了这个问题2.0-快照。至于v0.9(位于Maven Central中)您需要修改源代码(v.0.9很旧,不灵活)
PS2在纯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渲染库。您可以包含HTML和生成的图像吗?我更新了问题。生成的图像只是html的一小部分。它可能与您在html中定义的视口有关吗?因此,页面中只有一个(默认?)可见部分被呈现为图像。我参考了HtmlImageGenerator的源代码,找到了那些setter方法和默认值。请看一看:是的,是一个原始html2image的叉子。看起来比原来的html2image v更新。0.9. 只要使用新版本,问题就会消失。