SVG Rendering in Java

This article describes how to render SVG in Java environment

 

SVG allows to store scalable graphics XML format. Once you have your graphics in SVG, you willwant to render it on the screen or to print it.

The core open-source Java technology used to manage SVG in Java is the Apache Batik project.

Formatting Objects Processor

Batik can be used with FOP to transcode an SVG image into a PDF document. The SVG is rendered into PDF by using PDF commands to draw and fill lines and curves. This means that the graphical objects created with this remain as vector graphics. The same applies to PostScript output. For other output formats the SVG graphic will be converted to a bitmap image.

There are a number of SVG things that cannot be converted directly into PDF. Parts of the graphic such as effects, patterns and images are inserted into the PDF as a raster graphic. The resolution of these raster images can be controlled through the "target resolution" setting in the configuration.

Currently transparency is limited in PDF so many svg images that contain effects or graphics with transparent areas may not be displayed correctly.

PDF generation in a server environment

Batik must be run in a graphical environment. It uses AWT classes for rendering SVG, which in turn require an X server on Unixish systems. If you run a server without X, or if you can't connect to the X server due to security restrictions or policies (a so-called "headless" environment), SVG rendering will fail.

Here are some workarounds:

  • If you are using JDK 1.4, start it with the -Djava.awt.headless=true command line option.
  • Install an X server which provides an in-memory framebuffer without actually using a screen device or any display hardware. One example is Xvfb.
  • Install a toolkit which emulates AWT without the need for an underlying X server. One example is the PJA toolkit, which is free and comes with detailed installation instructions.
Last Updated on Monday, 19 October 2009 09:55