Image::CairoSVG - render SVG into a Cairo surface


This example converts an SVG into a PNG:

    use FindBin '$Bin';
    use Cairo;
    use Image::CairoSVG;
    my $cairosvg = Image::CairoSVG->new ();
    my $surface = $cairosvg->render ("$Bin/locust.svg");
    $surface->write_to_png ("$Bin/locust.png");

This renders the following image:

(This example is included as in the distribution. The input and output files are also included.)


This documents Image::CairoSVG version 0.11 corresponding to git commit 20ccb770f1e9457986846e90d3dc8183040fabdb released on Mon Jul 8 07:27:15 2019 +0900.


This module renders some kinds of SVG ("Scalable Vector Graphics") instructions into a Cairo surface.



    my $cairosvg = Image::CairoSVG->new ();

The user can supply a Cairo surface:

    my $cairosvg = Image::CairoSVG->new (surface => $surface);

For example,

    my $cairosvg = Image::CairoSVG->new (
        surface => Cairo::ImageSurface->create ('argb32', 100, 100)

The user can also supply a Cairo context:

    my $cairosvg = Image::CairoSVG->new (context => $cr);

If a Cairo context is supplied, the value of surface is ignored, and the image is drawn using the context value.

For a simple drawing task, use the surface generated by the module, the return value from "render". Only supply a surface if the module gets the dimensions of your image wrong. Only use a Cairo context if you want to include the image in some other image or rescale it.


    my $surface = $cairosvg->render ('some.svg');

Draw an SVG file into a Cairo surface. The return value is the surface drawn into.

If the user did not supply a context or a surface in "new", a new Cairo::ImageSurface object is generated. If the user supplied a context with "new", the return value is undefined and should be ignored. If the user supplied a surface with "new", the return value is just that surface.

If the call value is a scalar containing what looks like XML, it is parsed from the scalar instead.

If the user does not specify a surface, the generated surface returned by render is based on the attributes of the <svg> element, specifically either the width and height attributes, or the width and height specified in the viewBox attribute.

Calling with a scalar containing XML was added in version 0.08.


All of these methods take the attributes of the specific element after which they're named. So, for example, if you have an SVG line element, you can parse its attributes with an XML parser, then send the hash of key/value pairs in the attributes to "line".


    $cairosvg->line (%attr);

Render an SVG line onto the surface specified by $cairosvg. Given SVG input of the form <line >, this renders it onto the Cairo surface.


    $cairosvg->path (%attr);

Given an SVG path element, send its attribute key / value pairs as %attr to render into the Cairo surface of $cairosvg. This uses Image::SVG::Path to render the "d" attibute of the path. It also converts quadratic bezier curves into cubic bezier curves.


    $cairosvg->rect (%attr);


    $cairosvg->ellipse (%attr);


    $cairosvg->circle (%attr);


    $cairosvg->polygon (%attr);

Draws a polygon based on the points attribute, closing the path.


    $cairosvg->polyline (%attr);

The same as "polygon" except that it doesn't close the path.



Cairo is used for rendering the image.


Carp is used for reporting errors.


Image::SVG::Path is used for parsing the "path" information of the SVG.


XML::Parser is used for parsing SVG files.



CairoSVG is a Python SVG renderer in Cairo. Parts of the code here are based on it.


This is for reading the "d" attribute of SVG paths.


This extends the Marpa::R2 parser to parse SVG.


This is for generating SVG documents.


Perl extension for a Gnome library called librsvg which converts SVG to PNG or JPEG, etc. I have not tested this library.


Rasterize SVG content to pixel graphics.

More information

Perl Maven article

SVG - Scalable Vector Graphics with Perl - article at Perl Maven


This module is a "least effort" attempt to get the parts of SVG which the author needs rendered, rendered. It doesn't even pretend to be a full SVG renderer. So, if you find the module doesn't do some part of SVG which you want done, please add that to the module, and contribute your addition to this module via the github repository.


Ben Bullock, <>


This package and associated files are copyright (C) 2014-2019 Ben Bullock.

You can use, copy, modify and redistribute this package and associated files under the Perl Artistic Licence or the GNU General Public Licence.

cairosvg licence

Some parts of the module (specifically the SVG arc drawing code) are translations from the above-mentioned Python program "cairosvg", which is under the "GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007". I'm not really sure how or if this affects the code, but just in case it causes legal issues for someone downstream, I'm mentioning it here.