Click here to Skip to main content
14,644,142 members
Rate this:
Please Sign up or sign in to vote.
Hi there,

I am trying to convert a bunch of svg files to pdf, using the svglib and reportlab python packages.

I am using a small program, that I found in a neighboring thread (
Convert SVG to PDF in python[^] )

So, after assembling the converter looks like this:

from svglib.svglib import svg2rlg
from import renderPDF
drawing = svg2rlg("file.svg")
renderPDF.drawToFile(drawing, "file.pdf")

I tested it with the following svg file:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 

<svg xmlns="" version="1.1" height="1000.0" width="1000.0">
  <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="blue" />

Works like a charm.
My real file is this one:

<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="full" height="210.0" version="1.1" viewBox="0,0,297.0,210.0" width="297.0" xmlns="" xmlns:ev="" xmlns:xlink=""><defs><style type="text/css"><![CDATA[
    .line { stroke: firebrick; stroke-width: .1mm; }
    .blacksquare { fill: black; }
    .whitesquare { fill: white; }
]]></style></defs><g class="whitesquare"><rect height="35" width="35" x="8.5" y="35.0" /><rect height="35" width="35" x="8.5" y="105.0" /><rect height="35" width="35" x="8.5" y="175.0" /><rect height="35" width="35" x="43.5" y="0.0" />><rect height="35" width="35" x="253.5" y="0.0" /><rect height="35" width="35" x="253.5" y="70.0" /><rect height="35" width="35" x="253.5" y="140.0" /></g><g class="blacksquare"><rect height="35" width="35" x="8.5" y="0.0" /><rect height="35" width="35" x="8.5" y="70.0" /><rect height="35" width="35" x="8.5" y="140.0" /><rect height="35" width="35" x="253.5" y="175.0" /></g><g class="whitesquare"><rect height="17.5" width="297.0" x="0" y="0" /><rect height="17.5" width="297.0" x="0" y="192.5" /><rect height="210.0" width="26.0" x="0" y="0" /><rect height="210.0" width="26.0" x="271.0" y="0" /></g></svg>

No luck here, just a blank pdf file.
Where am I wrong?
I am thankful for any hint! Please tell me if you need more information.

Thanks in advance!

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I don't really know if this helps because I am not familiar with the tool you are using, but I tried pasting your SVG below and I found that CodeProject's HTML thingy is a bit picky when it comes to accepting SVG and it did not display the image until I removed the spaces and line breaks within the CDATA section (I am actually thinking the CDATA section could be the real problem).
Again, I don't know if those spaces and line breaks were introduced when you pasted the SVG element into your question, but I think it is worth a shot.

<svg baseProfile="full" height="210.0" version="1.1" viewBox="0,0,297.0,210.0" width="297.0" xmlns="" xmlns:ev="" xmlns:xlink=""><defs>.line {stroke: firebrick; stroke-width: .1mm; }.blacksquare { fill: black; }.whitesquare { fill: white; }<g class="whitesquare"><rect height="35" width="35" x="8.5" y="35.0"><rect height="35" width="35" x="8.5" y="105.0"><rect height="35" width="35" x="8.5" y="175.0"><rect height="35" width="35" x="43.5" y="0.0">><rect height="35" width="35" x="253.5" y="0.0"><rect height="35" width="35" x="253.5" y="70.0"><rect height="35" width="35" x="253.5" y="140.0"><g class="blacksquare"><rect height="35" width="35" x="8.5" y="0.0"><rect height="35" width="35" x="8.5" y="70.0"><rect height="35" width="35" x="8.5" y="140.0"><rect height="35" width="35" x="253.5" y="175.0"><g class="whitesquare"><rect height="17.5" width="297.0" x="0" y="0"><rect height="17.5" width="297.0" x="0" y="192.5"><rect height="210.0" width="26.0" x="0" y="0"><rect height="210.0" width="26.0" x="271.0" y="0">

BTW, the image is pretty boring :).

Soren Madsen
The Physicist 9-Apr-14 18:39pm
Thanks for this idea! I removed all line break and spaces in the CDATA section but it doesn't help. Conversion is still not working. :(

Well, the image is indeed boring, because it's only a small part of the full image, whose lengthy code I wanted to spare you. ;)
SoMad 9-Apr-14 20:48pm
Yeah, I figured it was a long-shot. I just noticed there is an error, but it could be due to a copy-paste problem when you added the question: In the 4th rect (I think), you have an extra '>' ...y="0.0" />><rect height="35"...
I have no idea if that could mess things up badly enough.

Soren Madsen
SoMad 9-Apr-14 21:09pm
I was experimenting on a file I render locally in my browser (IE11). If I completely take out the <defs><style.....></defs> part, I see the same image as in my Solution, but with the style in there, I see 4 black boxes. What is it supposed to look like?

Soren Madsen
SoMad 9-Apr-14 21:44pm
Forget about my previous question. I modified the white colored rects to see where you were going, so it looks like it is not rendered correctly in my Solution. That is not really surprising since CodeProject has restrictions on what can be displayed in the posts.

I guess I would try to replace the
<g class="whitesquare">
<g style="fill: white">
And remove the <style> part - just to see if it will work.

Soren Madsen
The Physicist 10-Apr-14 12:41pm
Thank you so much!

I removed the whole <defs> part and replaced
<g class="whitesquare"> with <g style="fill: white"> and
<g class="blacksquare"> with <g style="fill: black">

SVG image is (of course) still unchanged, but the conversion is working perfectly now.

My first question on and its already solved in less than 24 h.
Thank you again Soren! :)
SoMad 10-Apr-14 14:01pm
That is great!

I have not done a lot of work with SVGs, but I have learned that they can be problematic. Different browsers have issues with different things and I believe the VS2010 IDE barked at me for having <circle .... /> instead of <circle ....></circle>. That might just have been IntelliSense, but still...

Anyway, I am glad I could help.

Soren Madsen
The Physicist 10-Apr-14 15:31pm
They really are problematic. Just spent some more hours figuring out, how to get the scaling right (see Solution 2)[^], when one uses physical units (in, mm et cetera), so that there has to be no scaling during print out of the PDF.
SoMad 10-Apr-14 15:48pm
I feel like passing on a related trick that we had to use when printing an SVG from a web page. It is a bit different than your solution since it looks like you want a fixed size, whereas we want the SVG to auto-scale to max size when printing, but we ran into problems with making it scale the height correctly.

I will put a link to the solution we went with here in the hope that someone will find it useful:[^]
The key to solving our problem, was setting padding-bottom to adjust the total height.

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100