使用svglib包在python中进行SVG 2 PDF转换对我的svg文件不起作用 [英] SVG 2 PDF conversion in python using the svglib package does not work on my svg file

查看:198
本文介绍了使用svglib包在python中进行SVG 2 PDF转换对我的svg文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试使用svglib和reportlab python包将一堆svg文件转换为pdf。



我正在使用一个小程序,我在相邻的帖子中找到了这个程序(

在python中将SVG转换为PDF [ ^ ])



因此,组装后的转换器如下所示:



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 reportlab.graphics import renderPDF
drawing = svg2rlg("file.svg")
renderPDF.drawToFile(drawing, "file.pdf")





我使用以下svg文件测试它:





I tested it with the following svg file:

<?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 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>





这里没有运气,只是一个空白的pdf文件。

我哪里错了?

我很感激任何提示!如果您需要更多信息,请告诉我。



提前致谢!



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!

推荐答案

我不知道真的知道这是否有帮助,因为我不熟悉你正在使用的工具,但我尝试粘贴你的SVG下面我发现CodeProject的HTML东西在接受SVG时有点挑剔而且直到我才显示图像删除了CDATA部分中的空格和换行符(我实际上认为CDATA部分可能是真正的问题)。

再次,我不知道当你引入那些空格和换行符时将SVG元素粘贴到您的问题中,但我认为值得一试。



< svg baseProfile =fullheight =210.0version =1.1 viewBox =0,0,297.0,210.0width =297.0xmlns =http://www.w3.org/2000/svgxmlns:ev =http://www.w3.org/2001/xml -eventsxmlns:xlink =http://www.w3.org/1999/xlink>< defs> .line {stroke:firebrick;行程宽度:.1mm; } .blacksquare {填充:黑色; } .whitesquare {fill:white; }< g class =whitesquare>< rect height =35width =35x =8.5y =35.0>< rect height =35width =35x =8.5y =105.0>< rect height =35width =35x =8.5y =175.0>< rect height =35width =35x =43.5y =0.0>>< rect height =35width =35x =253.5y =0.0>< rect height =35width =35 x =253.5y =70.0>< rect height =35width =35x =253.5y =140.0>< g class =blacksquare>< rect height =35width =35x =8.5y =0.0>< rect height =35width =35x =8.5y =70.0>< rect height =35width =35x =8.5y =140.0>< rect height =35width =35x =253.5y =175.0>< g class =whitesquare>< rect height =17.5width =297.0x =0y =0>< rect height =17.5width =297.0x =0 y =192.5>< rect height =210.0width =26.0x =0y =0>< rect height =210.0width =26.0x =271.0 y =0>



BTW,图像非常无聊:)。



Soren Madsen
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


这篇关于使用svglib包在python中进行SVG 2 PDF转换对我的svg文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆