5

I want to create a SVG file with PHP, and then include it in a HTML file. Here's what I have so far (following this tutorial)

svg.php:

<?php
header("Content-type: image/svg+xml");
?>
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/
REC-SVG-20010904/DTD/svg10.dtd">

<svg width="310" height="140" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <g style="stroke:black;fill:lightgreen" transform="translate(30,30)">
        <rect x="10" y="10" width="100" height="30" style="stroke-width:4"/>
        <circle cx="170" cy="25" r="20" style="stroke-width:4"/>
        <line x1="265" y1="10" x2="200" y2="70" style="stroke-width:4"/>
        <text x="80" y="90" style="font:size: 8">Basic shapes</text>
    </g>
</svg>

index.html

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html,body, #MOSVG { 
    height: 100%; 
    width: 100%;
    padding:0;
    margin:0;
}
</style>
</head>
<body>
    <object data="svg.php" type="image/svg+xml" id="MOSVG" />

</body>
</html>

When I look at index.html all I see is a blank page. There's no warnings/errors on the console. Apache error logs (I'm using XAMPP) give [Mon Jul 09 14:36:15 2012] [error] [client 127.0.0.1] script 'C:/xampp/htdocs/svgtest/public/svg.php' not found or unable to stat, referer: http://localhost/svgtest/public/. They're in the same directory.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • Found/fixed already, still getting the issue. – SomeKittens Jul 09 '12 at 19:33
  • 1
    if you remove the line it renders the svg – Gntem Jul 09 '12 at 19:40
  • Thanks, that worked. Why it's set up to break in the tutorial is beyond me.... – SomeKittens Jul 09 '12 at 19:42
  • 3
    Check whether [short-open-tag](http://www.php.net/manual/en/ini.core.php#ini.short-open-tag) is enabled in php.ini, it's known to cause weird problems with xml output - also see the workaround presented in the referred doc. – fvu Jul 09 '12 at 20:25

1 Answers1

8

GeoPhoenix answered above, I figured I'd put it into an answer so that there's an offical one:

if you remove the <?xml?> line it renders the svg

PHP can see <? as the opening tag for PHP code (rather than an XML declaration), thanks to the short-open-tag setting (thanks to fvu). This is an easy fix, I just replaced

<?xml version="1.0" encoding="iso-8859-1"?>

with

<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>';?>

This has the same intended result and doesn't cause syntax errors.

Community
  • 1
  • 1
SomeKittens
  • 38,868
  • 19
  • 114
  • 143