如何将内存中的HTML代码呈现给图像?

时间:2012-02-02 14:30:50

标签: java flying-saucer xhtmlrenderer

我发现http://code.google.com/p/flying-saucer/应该是我项目最合适的解决方案。但文档似乎只能从文件中读取。

但我的情况是我想从代码渲染,我不想保存临时XML文件。例如renderToImageAutoSize

中的Graphics2DRenderer
  

static java.awt.image.BufferedImage     renderToImageAutoSize(java.lang.String url,int width,int   bufferedImageType)             一种静态实用程序方法,用于从文档自动创建图像,其中高度根据文档内容确定。

有没有办法让渲染器直接从内存中的HTML代码中读取?

1 个答案:

答案 0 :(得分:2)

Graphics2DRenderer不是实用工具类。你应该创建一个新的实例。

Graphics2DRenderer gr = new Graphics2DRenderer();
gr.setDocument(doc, uri);
gr.layout(g2, dim);
Rectangle minSize = r.getMinimumSize();
//create new image with this size
gr.render(newG2);

请参阅http://today.java.net/pub/a/today/2006/10/31/combine-facelets-and-flying-saucer-renderer.html#xhtml-to-image-conversion