Server-Side SVG

From Svg wiki

SVG Can be generated on the web server using any server-side method: JSP, Servlets, Perl, PHP, Ruby, ASP, etc. It is a good idea to set the MIME_Type on the server if you are generating svg that will reside as files on the server - but if you want to stream SVG you need to set the content-type. There is a survey of server-side SVG techniques at PinkJuice and the general principles described on the Pinkjuice page on serverside SVG with Ruby apply to other languages.

Contents

JSP

Tag library for SVG: http://www.printx.org/svgembed/index.php (Compressed SVG output up to 20% of original size)

Dynamic SVG

<% taglib uri="http://www.printx.org/svgembed" prefix="svg" %>
<svg:embed name="example1" width="100%" height="100%">
   <?xml version="1.0" standalone="no"?>
   <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//E "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd
   <svg width="100%" height="100%"
      <text x="31px" y="55px" font-size:24;font-family:Batang"><%= request.getParameter("hello")%></text>
   </svg>
</svg:embed>

Loading SVG from external JSP modules

<% taglib uri="http://www.printx.org/svgembed" prefix="svg" %>
<svg:embed name="example1" width="225" height="166">
   <%
 include file="/svgexamples/external.jsp" %>
</svg:embed>

Loading SVG from static JSP modules

<% taglib uri="http://www.printx.org/svgembed" prefix="svg" %>
<svg:embed name="bezier" width="225" height="166">
   <%
 include file="/svgexamples/bezier.svg" %>
</svg:embed>

PHP

Some wrapper classes for SVG: http://www.phpclasses.org/browse.html/package/457.html

ASP

In ASP, one critical step is setting the content type with:

response.contenttype="image/svg+xml"

In the basic sense you can just do something like:

<html>
<embed height="400" width="300" src="page.asp"
pluginspage="http://www.adobe.com/svg/viewer/install/"
type="image/svg+xml" />
</html>
<%
Response.contenttype="image/svg+xml"
Response.write("<svg width='400' height='300'>")
Response.write("<rect width='100' height='100' x='50' y='50' rx='5' fill='blue'/>")
Response.write("</svg>")
%>

http://www.medisciencetech.com/weather/weather.asp describes one technique of updating SVG between server and client.

mapping .svg to asp.dll

You may wish to serve your ASP generated SVG with a .svg file extension. To do this, a minor adjustment must be made to the application configuration for the virtual server / directory. Check out the screen shot iisconfig.png (IIS v5).

perl

There are serveral perl modules available including the CPAN SVG module available on the SVG server and lib-svg toolkit. The perl SVG tutorials offers a number of tutorials and examples. You can download working Perl sample files from the Perl SVG Zone Sample Repository

Drawing a circle using perl:

circle.pl

#!/usr/bin/perl -w
use strict;
use SVG;

#print the header manually if we are not using the CGI module
print 'Content-type: image/svg+xml';

# create an SVG object
my $svg= SVG->new(width=>200,height=>200);

# draw a circle at position (100,100)  with ID 'this_circle'
$svg->circle(id=>'this_circle',cx=>100,cy=>100,r=>50);
my $out = $svg->xmlify;

print $out;
Content-type: image/svg+xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
  <svg height="200" width="200">
      <circle id="this_circle" cx="100" cy="100" r="50" />
  </svg>

Ruby

Ruby is a nice OO language from Japan. For generating SVG, one can use REXML, a nice XML toolkit.