How can i easily convert html to image and then to byte array without create it
如何轻松地将html转换为图像然后转换为字节数组而不创建它
thanks
谢谢
4 个解决方案
#1
13
If you do not have any complex html you can render it using a normal JLabel
. The code below will produce this image:
如果您没有任何复杂的html,可以使用普通的JLabel进行渲染。下面的代码将生成此图像:
<html>
<h1>:)</h1>
Hello World!<br>
<img src="http://img0.gmodules.com/ig/images/igoogle_logo_sm.png">
</html>
public static void main(String... args) throws IOException {
String html = "<html>" +
"<h1>:)</h1>" +
"Hello World!<br>" +
"<img src=\"http://img0.gmodules.com/ig/images/igoogle_logo_sm.png\">" +
"</html>";
JLabel label = new JLabel(html);
label.setSize(200, 120);
BufferedImage image = new BufferedImage(
label.getWidth(), label.getHeight(),
BufferedImage.TYPE_INT_ARGB);
{
// paint the html to an image
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
label.paint(g);
g.dispose();
}
// get the byte array of the image (as jpeg)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte[] bytes = baos.toByteArray();
....
}
If you would like to just write it to a file:
如果您想将其写入文件:
ImageIO.write(image, "png", new File("test.png"));
#2
4
I think you can use the library
我想你可以使用这个库
html2image-0.9.jar
html2image-0.9.jar
you can download this library at this page: http://code.google.com/p/java-html2image/
您可以在此页面下载此库:http://code.google.com/p/java-html2image/
#3
3
What about using an in memory ByteArrayStream
instead of a FileOutputStream
in the code above? That would be a byte array, at least ...
在上面的代码中使用内存ByteArrayStream而不是FileOutputStream怎么样?这将是一个字节数组,至少......
#4
0
This is nontrivial because rendering a HTML page can be quite complex: you have text, images, CSS, possibly even JavaScript to evaluate.
这非常重要,因为渲染HTML页面可能非常复杂:您可以使用文本,图像,CSS,甚至可以评估JavaScript。
I don't know the answer, but I do have something that might help you: code for iText (a PDF writing library) to convert a HTML page to a PDF file.
我不知道答案,但我确实有一些可能对你有帮助的东西:用于将HTML页面转换为PDF文件的iText(PDF编写库)代码。
public static final void convert(final File xhtmlFile, final File pdfFile) throws IOException, DocumentException
{
final String xhtmlUrl = xhtmlFile.toURI().toURL().toString();
final OutputStream reportPdfStream = new FileOutputStream(pdfFile);
final ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(xhtmlUrl);
renderer.layout();
renderer.createPDF(reportPdfStream);
reportPdfStream.close();
}
#1
13
If you do not have any complex html you can render it using a normal JLabel
. The code below will produce this image:
如果您没有任何复杂的html,可以使用普通的JLabel进行渲染。下面的代码将生成此图像:
<html>
<h1>:)</h1>
Hello World!<br>
<img src="http://img0.gmodules.com/ig/images/igoogle_logo_sm.png">
</html>
public static void main(String... args) throws IOException {
String html = "<html>" +
"<h1>:)</h1>" +
"Hello World!<br>" +
"<img src=\"http://img0.gmodules.com/ig/images/igoogle_logo_sm.png\">" +
"</html>";
JLabel label = new JLabel(html);
label.setSize(200, 120);
BufferedImage image = new BufferedImage(
label.getWidth(), label.getHeight(),
BufferedImage.TYPE_INT_ARGB);
{
// paint the html to an image
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
label.paint(g);
g.dispose();
}
// get the byte array of the image (as jpeg)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte[] bytes = baos.toByteArray();
....
}
If you would like to just write it to a file:
如果您想将其写入文件:
ImageIO.write(image, "png", new File("test.png"));
#2
4
I think you can use the library
我想你可以使用这个库
html2image-0.9.jar
html2image-0.9.jar
you can download this library at this page: http://code.google.com/p/java-html2image/
您可以在此页面下载此库:http://code.google.com/p/java-html2image/
#3
3
What about using an in memory ByteArrayStream
instead of a FileOutputStream
in the code above? That would be a byte array, at least ...
在上面的代码中使用内存ByteArrayStream而不是FileOutputStream怎么样?这将是一个字节数组,至少......
#4
0
This is nontrivial because rendering a HTML page can be quite complex: you have text, images, CSS, possibly even JavaScript to evaluate.
这非常重要,因为渲染HTML页面可能非常复杂:您可以使用文本,图像,CSS,甚至可以评估JavaScript。
I don't know the answer, but I do have something that might help you: code for iText (a PDF writing library) to convert a HTML page to a PDF file.
我不知道答案,但我确实有一些可能对你有帮助的东西:用于将HTML页面转换为PDF文件的iText(PDF编写库)代码。
public static final void convert(final File xhtmlFile, final File pdfFile) throws IOException, DocumentException
{
final String xhtmlUrl = xhtmlFile.toURI().toURL().toString();
final OutputStream reportPdfStream = new FileOutputStream(pdfFile);
final ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(xhtmlUrl);
renderer.layout();
renderer.createPDF(reportPdfStream);
reportPdfStream.close();
}