How to run Processing headlessly & export your Processing visualization as an .svg with Apache Batik

edited July 2017 in Library Questions

Hello everyone!

I am very new to Processing and I am evaluating it for our application. So my main question is if it is possible in any way to use Processing headlessly and export .svgs from a Processing drawing. For the latter I have seen that maybe there exists a bridge to Apache Batik via the basic awt.Graphics2D, but I am hoping that somebody maybe already did this before me and can give me guidance. Thanks!



  • edited July 2017

    Ok. It seems that I could make it work, although a bit hacky. I just replaced the g2 JavaGraphics2D canvas with an Apache Batik SVG generator (which inherits from JavaGraphics2D and is meant to be dropped into any awt compatible drawing method). The replacing however must take place after the beginDrawing() is called, as the beginDrawing() replaces the g2.

    import java.awt.Dimension;
    import org.apache.batik.dom.GenericDOMImplementation;
    import org.apache.batik.svggen.SVGGraphics2D;
    import org.apache.batik.svggen.SVGGraphics2DIOException;
    import org.w3c.dom.DOMImplementation;
    import org.w3c.dom.Document;
    import processing.awt.PGraphicsJava2D;
    import processing.core.*;
    public class MyProcessingSketch extends PApplet {
        public static void main(String[] args) {
            PApplet.main(new String[] {"MyProcessingSketch"});
        @ Override
        public void settings() {
        PGraphics pg;
        PGraphicsJava2D pgj;
        SVGGraphics2D svgGenerator;
        @ Override
        public void setup() {
            // Get a DOMImplementation.
            DOMImplementation domImpl =
            // Create an instance of org.w3c.dom.Document.
            String svgNS = "";
            Document document = domImpl.createDocument(svgNS, "svg", null);
            // Create an instance of the SVG Generator.
            svgGenerator = new SVGGraphics2D(document);
            svgGenerator.setSVGCanvasSize(new Dimension(400, 200));
            pg = createGraphics(400, 200, JAVA2D);
            pgj = (PGraphicsJava2D) pg;
        @ Override
        public void draw() {
            pgj.g2 = svgGenerator;
            pgj.ellipse(30, 30, 60, 60);
            // Finally, stream out SVG to the standard output using
            // UTF-8 encoding.
            boolean useCSS = false; // we want to use CSS style attributes
            try {
                Writer out = new OutputStreamWriter(System.out, "UTF-8");
      , useCSS);
            } catch (SVGGraphics2DIOException e) {
                // TODO Auto-generated catch block
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block

    Please forgive my usage of Processing. I am very new to it. This code outputs the following on your commandline:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
    <svg fill-opacity="1" xmlns:xlink="" color-rendering="auto" color-interpolation="auto" text-rendering="auto" stroke="black" stroke-linecap="square" width="400" stroke-miterlimit="10" shape-rendering="auto" stroke-opacity="1" fill="black" stroke-dasharray="none" font-weight="normal" stroke-width="1" height="200" xmlns="" font-family="'Dialog'" font-style="normal" stroke-linejoin="miter" font-size="12px" stroke-dashoffset="0" image-rendering="auto"
    ><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
      ><g fill="rgb(51,51,51)" stroke="rgb(51,51,51)"
        ><rect x="0" width="400" height="200" y="0" stroke="none"
        ><g fill="white" stroke="white"
        ><circle fill="none" r="30" cx="30" cy="30"

    Any suggestions on how to improve this? Trim off the edges that I may have overlooked?

  • edited July 2017

    I could turn this into some pretty beautiful examples. Check it out:


    Git repo:

Sign In or Register to comment.