Server-Side SVG

A Survey

Keywords: SVG server-side, ASP.NET, PHP, Perl, JSP

Christian Wenz
Hauser & Wenz


Christian Wenz is an author, trainer and consultant with focus on Web technologies. He frequently contributes to IT magazines and speaks at conferences around the globe. Among the over three dozen books he wrote or co-wrote is "SVG Unleashed" by Sams Publishing. Christian lives and works in Munich in southern Germany.

Tobias Hauser
Hauser & Wenz


Tobias Hauser works as an author, trainer and consultant in the field of web design and web graphics. He is author or co-author of more than three dozen books on various Web topics, including "SVG Unleashed" by Sams Publishing. Tobias lives and works in southern Germany.


Two years after the first SVG Open, it is time to have a look how SVG has been accepted by the public. In this survey, we analyze how the various server-side techniques (PHP, ASP.NET, Perl, ...) have jumped on the bandwagon, which SVG-related modules they offer and where the programmer has to do the dynamic SVG generation manually.

Table of Contents

1. Introduction
2. Prequisites
3. PHP
4. .NET
5. Perl
6. Conclusion

1. Introduction

Despite it is currently mostly used in the field of geography, SVG is a web technology and therefore should be embraced by various server-side technologies. When we first talked about this at SVG Open 2002 [SVG Open 2002] , the support was still fairly limited. Now, two years later, we look again.

Our main interest is in built-in support. Since Open Source offers a variety of modules, it is impossible to list them all. Furthermore, only built-in modules or packages in the official extension repositories will help to spread SVG further among server-side developers.

2. Prequisites

From a theoretical point of view, dynamically generating SVG is not so hard. In order to let the browser pick the right viewer for data retrieved from a remote server, the file type must be set correctly. In the web, this is done using the MIME type and sending it in the Content-type HTTP header:

HTTP/1.1 200 OK
Content-Type: image/svg+xml
Content-Length: 123

content ...

Since November 2000, the MIME type for SVG files is image/svg+xml, although some sites still use the old type image/svg-xml. Configuring a web server to send out the correct MIME type according to the file extension is easy. Under Windows, the MIME type can be configured either system-wide or just for the web server using the appropriate GUI managers; Apache users may edit the file mime.types and add the following entry:

image/svg+xml svg

Server-side scripts, on the other hand, generally do not have the file extension .svg. There, the MIME type has to be set manually for SVG content. Although this differs from language to language, the result is always the same: The MIME type is passed to the browser via an HTTP header entry. Here is the code used in PHP:

  header("Content-type: image/svg+xml");

In ASP.NET (and in ASP, as well), the property Response.ContentType must be set to "image/svg+xml". Alternatively, this can also be done in a page-wide directive:

<%@ page contentType="image/svg+xml"%>

The very same code can be used in JSP scripts to achieve the same goal.

Other server-side scripting languages like Perl and Ruby create the complete data sent to the client, so they have to provide the complete HTTP header:

print "Content-type: image/svg+xml\n\n";

The script may then be included in an HTML page using either <object> (standards-compliant, but not supported by some browsers) or <embed> (works excellent, not standards-compliant). However especially the Microsoft Internet Explorer sometimes ignores the MIME type set. In this case, however, a simple trick makes the browser cooperate: Add ?IE=.svg to the URL. This does not change the output of the server-side script at all (in most cases), but it tricks Internet Explorer into thinking that the file extension of the URL is .svg, so it must be SVG content.

So it is easy to create SVG dynamically on the fly. However, using an object-oriented approach to the SVG makes programming both easier and less error-prone. Therefore, we have a look at which packages or modules the various technologies have to offer.

3. PHP

PHP is a recursive acronym and stands for PHP: Hypertext Preprocessor. It is currently the most widely-used scripting technology. PEAR is the pendant to Perl's CPAN and contains a collection of high-quality modules written in PHP itself for various tasks. An early attempt to bring SVG to PEAR (and, as a consequence, to PHP), failed, but in 2003, the package XML_SVG was accepted into PEAR. The package homepage is It can be installed using the PEAR command-line installer:

pear install XML_SVG

Afterwards, there is an object-oriented API to creating SVG content. Here is an example

  require_once 'XML/SVG.php';
  $svg = &new XML_SVG_Document(array('width' => 150,
                                     'height' => 40));
  $t = &new XML_SVG_Text(array('text' => 'SVG Open 2004', 
                               'x' => '30', 
                               'y' => '25'));

Figure 1: The (Trivial) Sample SVG File

It is also to be noted that there exists a PEAR package Image_GIS that depends on XML_SVG.

There are two other SVG-specific packages in PEAR: XML_image2svg converts a bitmap graphic to SVG, XML_svg2image rasters an SVG file to an image. This, however, is done by using the Java extension of PHP to call a Batik installation; Batik then does the transformation. Unfortunately, the Java extension is still experimental, hard to install and does not work with the current new version 5 of PHP. [PHP 5 Kompendium] Nevertheless, the combination of Batik and PHP can certainly be used:

  require_once 'XML/svg2image.php';
  $batik = new XML_svg2image();

XML_SVG is a quite useful package, and even XML_svg2image (and to a lesser extent XML_image2svg) can provide reasonable results. Unfortunately it seems that development has stalled, there are no new releases on the horizon.

4. .NET

Microsoft's .NET technology/strategy relies heavily on XML. For instance, all configuration files, previously in the INI file format, are not XML files (although they have the extension .config, not .xml). Not to mention the excellent XML Web Services support of .NET. However, in the whole .NET Framework there is no single class specifically for SVG. However, there exists an open source project that tries to create a renderer and platform for SVG content: SVG#. It can be found at and at its SourceForge page Among other things, it is possible to create a web renderer for SVG content. Here is the code for SVG# version 0.2x:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="SharpVectors.Dom.Svg" %>
<%@ Import Namespace="SharpVectors.Renderer.Gdi" %>
<script runat="server">
void Page_Load() {
  SvgDocument svg = new SvgDocument();
  GdiRenderer r = new GdiRenderer(150, 40);
  Bitmap b = r.GetBitmapImage();
  b.Save(@"C:\output.png", ImageFormat.Png);

Unfortunately, this project seems to be in hibernation since several months. Also, Microsoft announced XAML [XAML Overview] for their next Windows version, Longhorn. XAML is an XML-based markup language that bears some similarities with SVG (and XUL), but is not SVG. So it could be that SVG# will be orphaned and ressources be moved to work on XAML-related things.

5. Perl

Although Perl is not used that much in the WWW anymore, it still is one of the premier scripting languages, and a very mighty one. Unlike PHP's PEAR, Perls code repository CPAN does not have such strict regulations about what may be uploaded in the system. However this does not mean that the quality of the code is always worse. As a matter of fact, the SVG module, available at, is an excellent one. Here is a code sample:

use SVG;

my $svg = SVG->new(width=>150, height>=40);
my $text = $svg->text(x=>30, y=>25)->cdata('SVG Open 2004');
print $svg->xmlify;'s author, Ronan Oger, is an active member on the svg-developers mailing list and therefore very close to the latest changes in the SVG community. Although the SVG module has not seen an update in several months, here the chances for ongoing development on support are the highest of all modules presented here.

6. Conclusion

One might think that SVG somehow has lost its momentum in the Web. There seems to be no progress in terms of SVG viewers for web browsers, and also server-side technologies somehow lack their support for SVG. Although there do exist packages — quite good ones! — they have not seen an update in the recent past. But probably these to factors affect each other: New viewer versions might generate a greater user interest in SVG, which in turn could motivate developers to work on their modules, which in turn could make more web developers actively use SVG.


  1. Multipurpose Internet Mail Extensions

  2. The foundation of .NET: a huge class library

  3. eXtensible Application Markup Language


[SVG Open 2002]
Generating SVG on the fly, Christian Wenz and Tobias Hauser, Proceedings SVG Open 2002, Zurich. Available online at
[PHP 5 Kompendium]
PHP 5 Kompendium, Christian Wenz and Tobias Hauser, Pearson Education Germany, 2004, to appear.
[XAML Overview]
"Longhorn" Markup Language (code-named "XAML") Overview, Microsoft Corporation, 2004,

XHTML rendition created by gcapaper Web Publisher v2.0, © 2001-3 Schema Software Inc.