HTML存为图片的Java类库:Html2Image

jopen 11年前

Html2Image 是一个 Java 类库用于将 HTML标记存为 GIF\PNG 和 JPG 图片,并通过HTML的<map> 标签展示该图片。

使用案例:

  • Programmatically compose images - Use case: You need to compose images from other images and texts.
    • Solution - Create a web page using plain HTML, CSS and image and use Html2Image to convert it to an image.
  • Improve your spam - Use case: You need to send decorative HTML email.
    • You craft your HTML using CSS, images and links.
    • Alas, your clients open their Outlook, Gmail or any other mail client mailbox, and find your mail all scrambled up (merely resembles the presentation of the same HTML in a browser).
    • That's because Outlook and other clients only support limited functionality of HTML and have different implementations and bugs.
    • Solution - Use Html2Image to convert your original mail's HTML to a new HTML containing only image (<img/>) and client-side image-map (<map/>) for the links in the original HTML.
    • This also allow you to easily use unique fonts in your mail.
  • Prevent spamming - Use case: Your website has a list of contacts and their email addresses.
    • The email addresses are in plain text, allowing robots and spiders to harvest these addresses and spam your team with Viagra ads.
    • Solution - Use Html2Image to convert those email addresses to images.

示例代码:

HtmlImageGenerator imageGenerator = new HtmlImageGenerator();  imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");  imageGenerator.saveAsImage("hello-world.png");  imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");

项目主页:http://www.open-open.com/lib/view/home/1350864881680