将html转换为字节数组java中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4437806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:29:58  来源:igfitidea点击:

convert html to image in byte array java

javahtmlimagerendererlayout-engine

提问by cls

How can i easily convert html to image and then to byte array without create it

我如何轻松地将 html 转换为图像,然后转换为字节数组而不创建它

thanks

谢谢

回答by Wouter Lievens

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.

我不知道答案,但我确实有一些可以帮助你的东西:iText(一个 PDF 编写库)的代码,用于将 HTML 页面转换为 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();
}

回答by dacwe

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>

alt text

替代文字

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"));

回答by Edwin Pomayay Yaranga

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/

回答by phil

What about using an in memory ByteArrayStreaminstead of a FileOutputStreamin the code above? That would be a byte array, at least ...

在上面的代码中使用内存ByteArrayStream而不是 aFileOutputStream怎么样?那将是一个字节数组,至少......