Click here to Skip to main content
15,867,997 members
Please Sign up or sign in to vote.
5.00/5 (1 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:

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


I tested it with the following svg file:

XML
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
 
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="1000.0" width="1000.0">
  <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="blue" />
</svg>


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

XML
<?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="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/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!
Posted

1 solution

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="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/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
 
Share this answer
 
Comments
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">
with
<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 codeproject.com and its already solved in less than 24 h.
Thank you again Soren! :)

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

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900