org.xhtmlrenderer.simple
Class Graphics2DRenderer

java.lang.Object
  extended by org.xhtmlrenderer.simple.Graphics2DRenderer

public class Graphics2DRenderer
extends Object

Graphics2DRenderer supports headless rendering of XHTML documents, and is useful for rendering documents directly to images.

Graphics2DRenderer supports the XHTMLPanel.setDocument(Document), Container.layout(), and XHTMLPanel#render() methods from XHTMLPanel, as well as easy-to-use static utility methods. For example, to render a document in an image that is 600 pixels wide use the renderToImageAutoSize(String,int,int) method like this:

 BufferedImage img = Graphics2DRenderer.renderToImage( "test.xhtml", width);
 

Author:
Joshua Marinacci

Field Summary
protected  Dimension dim
          Dimensions of the image to render, in pixels.
protected  XHTMLPanel panel
          The panel we are using to render the document.
 
Constructor Summary
Graphics2DRenderer()
          Creates a new renderer with no document specified.
 
Method Summary
 Rectangle getMinimumSize()
          Returns the size image needed to render the document without anything going off the side.
 XHTMLPanel getPanel()
          Returns the panel used internally for rendering.
 SharedContext getSharedContext()
          Gets the SharedContext for layout and rendering.
 void layout(Graphics2D g2, Dimension dim)
          Lay out the document with the specified dimensions, without rendering.
 void render(Graphics2D g2)
          Renders the document to the given canvas.
static BufferedImage renderToImage(String url, int width, int height)
          A static utility method to automatically create an image from a document; the image supports transparency.
static BufferedImage renderToImage(String url, int width, int height, int bufferedImageType)
          A static utility method to automatically create an image from a document.
static BufferedImage renderToImageAutoSize(String url, int width)
          A static utility method to automatically create an image from a document, where height is determined based on document content.
static BufferedImage renderToImageAutoSize(String url, int width, int bufferedImageType)
          A static utility method to automatically create an image from a document, where height is determined based on document content.
 void setDocument(Document doc, String base_url)
          Sets the document to render, lays it out, and renders it.
 void setDocument(String url)
          Set the document to be rendered, lays it out, and renders it.
 void setSharedContext(SharedContext ctx)
          Sets the SharedContext for rendering.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

panel

protected XHTMLPanel panel
The panel we are using to render the document.


dim

protected Dimension dim
Dimensions of the image to render, in pixels.

Constructor Detail

Graphics2DRenderer

public Graphics2DRenderer()
Creates a new renderer with no document specified.

Method Detail

layout

public void layout(Graphics2D g2,
                   Dimension dim)
Lay out the document with the specified dimensions, without rendering.

Parameters:
g2 - the canvas to layout on.
dim - dimensions of the container for the document

render

public void render(Graphics2D g2)
Renders the document to the given canvas. Call layout() first.

Parameters:
g2 - Canvas to render to.

setDocument

public void setDocument(String url)
Set the document to be rendered, lays it out, and renders it.

Parameters:
url - the URL for the document to render.

setDocument

public void setDocument(Document doc,
                        String base_url)
Sets the document to render, lays it out, and renders it.

Parameters:
doc - the Document to render
base_url - base URL for relative links within the Document.

setSharedContext

public void setSharedContext(SharedContext ctx)
Sets the SharedContext for rendering.

Parameters:
ctx - The new renderingContext value

getMinimumSize

public Rectangle getMinimumSize()
Returns the size image needed to render the document without anything going off the side. Could be different than the dimensions passed into layout because of text that couldn't break or a table that's set to be too big.

Returns:
A rectangle sized to the minimum size required for the document.

getSharedContext

public SharedContext getSharedContext()
Gets the SharedContext for layout and rendering.

Returns:
see desc

getPanel

public XHTMLPanel getPanel()
Returns the panel used internally for rendering.


renderToImage

public static BufferedImage renderToImage(String url,
                                          int width,
                                          int height)
A static utility method to automatically create an image from a document; the image supports transparency. To render an image that does not support transparency, use the overloaded version of this method renderToImage(String, int, int, int).

Parameters:
url - URL for the document to render.
width - Width in pixels of the layout container
height - Height in pixels of the layout container
Returns:
Returns an Image containing the rendered document.

renderToImage

public static BufferedImage renderToImage(String url,
                                          int width,
                                          int height,
                                          int bufferedImageType)
A static utility method to automatically create an image from a document. The buffered image type must be specified.

Parameters:
url - URL for the document to render.
width - Width in pixels of the layout container
height - Height in pixels of the layout container
bufferedImageType - On of the pre-defined image types for a java.awt.image.BufferedImage, such as TYPE_INT_ARGB or TYPE_INT_RGB.
Returns:
Returns an Image containing the rendered document.

renderToImageAutoSize

public static BufferedImage renderToImageAutoSize(String url,
                                                  int width)
A static utility method to automatically create an image from a document, where height is determined based on document content. To estimate a size before rendering, use setDocument(String) and then getMinimumSize(). The rendered image supports transparency.

Parameters:
url - java.net.URL for the document to render.
width - Width in pixels of the layout container
Returns:
Returns an java.awt.Image containing the rendered document.

renderToImageAutoSize

public static BufferedImage renderToImageAutoSize(String url,
                                                  int width,
                                                  int bufferedImageType)
A static utility method to automatically create an image from a document, where height is determined based on document content. To estimate a size before rendering, use setDocument(String) and then getMinimumSize().

Parameters:
url - java.net.URL for the document to render.
width - Width in pixels of the layout container
bufferedImageType - On of the pre-defined image types for a java.awt.image.BufferedImage, such as TYPE_INT_ARGB or TYPE_INT_RGB.
Returns:
Returns an java.awt.Image containing the rendered document.


Copyright © 2013. All Rights Reserved.