使用Java将HTML转换为图像 [英] Converting HTML to image using java
问题描述
我在使用Java将html转换为图像时遇到了一些问题我正在使用html2image [java]
它创建图像,但是问题是它仅在html的一小部分上创建图像.我怎样才能使整个html的图像.谢谢
这是我的代码
import gui.ava.html.image.generator.HtmlImageGenerator;导入java.io.File;公开课测试{公共静态void main(String [] args){HtmlImageGenerator imageGenerator =新的HtmlImageGenerator();字符串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>封面< a></li></ol></nav></body></html>
@Pralay ,不幸的是
imageGenerator.setSize(new Dimension(1024,768));
和
imageGenerator.getDefaultSize().setSize(1024,768);
没有帮助.
无论如何, html2image 使用的 ImageRenderer
中的默认大小为1024x768.查看 ImageRendereImpl
类的摘录:
公共类ImageRendererImpl实现ImageRenderer {公共静态最终int DEFAULT_WIDTH = 1024;公共静态最终int DEFAULT_HEIGHT = 768;...私有整数宽度= DEFAULT_WIDTH;私人int高度= DEFAULT_HEIGHT;private boolean autoHeight = true;...
但是请注意 autoHeight
字段.在 ImageRendererImpl
类内的下面,您可以看到:
if(autoHeight){//使用临时缓冲区进行布局Graphics2D graphics2D =(Graphics2D)bufferedImage.getGraphics();renderer.layout(graphics2D,新Dimension(宽度,高度));graphics2D.dispose();矩形大小= renderer.getMinimumSize();最后的int autoWidth =(int)size.getWidth();最后的int autoHeight =(int)size.getHeight();bufferedImage = new BufferedImage(autoWidth,autoHeight,imageType);尺寸=新尺寸(autoWidth,autoHeight);
如果 authHeight
为true(默认情况下实际上为 true
) org.xhtmlrenderer.simple.Graphics2DRenderer的
类将被调用.从 getMinimumSize()
方法
PS :我已经通过 html2image v. 2.0-SNAPSHOT 调查并解决了该问题.至于 0.9 (位于Maven Central中),您需要修改源代码( 0.9 既旧又不灵活).
PS2 在纯Java中,您可以尝试以下操作:
公共类Example1 {私有静态最终int WIDTH = 1204;私有静态最终int HEIGHT = 768;公共静态void main(String [] args)引发IOException {//打开HTML页面JEditorPane editorPane =新的JEditorPane();editorPane.setEditable(false);URL urlToPage = new File("/home/me/Temp/cover.html").toURI().toURL();editorPane.setPage(urlToPage);editorPane.setSize(WIDTH,HEIGHT);//呈现页面BufferedImage renderingImage =新的BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);editorPane.print(renderedImage.getGraphics());//将结果写入文件ImageIO.write(renderedImage,"PNG",新File("/home/me/Temp/hello-world.png")));}}
PS3 正如我所见, html2image 现在尚未维护(要使用v. 2.0 ,您需要通过以下方式下载并编译该文件:你自己).也许,这个图书馆有些活生生的分支.或者只是尝试另一个HTML渲染库.
i'm facing some problem converting html to image using java im using html2image[java]
it create an image, but the problem is it only create an image on a small part of the html. how can i make it to make an image of the whole html. thank you
this is my code
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");
}
}
i also use saveAsHtmlWithMap to save the html file. and that html file written by the program to the harddisk is also a small one.
this is the html code of cover.thml
<!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>
@Pralay, unfortunately,
imageGenerator.setSize(new Dimension(1024, 768));
and
imageGenerator.getDefaultSize().setSize(1024, 768);
didn't help.
Anyway, default size in ImageRenderer
used by html2image is 1024x768. Look at the excerpt from ImageRendereImpl
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;
...
But pay attention to the autoHeight
field. Below inside ImageRendererImpl
class you can see:
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);
If authHeight
is true (actually it's true
by default) getMinimumSize()
method of org.xhtmlrenderer.simple.Graphics2DRenderer
class will be invoked. As can be concluded from the corresponding Javadoc the minimal possible area will be used. This is why you get too little image.
Setting autoHeight
to false
solves the issue:
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");
}
}
So I've got
PS: I've investigated and overcame the issue with aid of html2image v. 2.0-SNAPSHOT. As for v. 0.9 (which is in Maven Central) you need to modify the source code (v. 0.9 is old and not flexible).
PS2 In pure Java you can try something like this:
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 As I see html2image is not maintained now (in order to use to v. 2.0 you need to download and compile it by yourself). Perhaps, there are some living forks of this library. Or just try another HTML rendering library.
这篇关于使用Java将HTML转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!