在python中将SVG转换为PDF [英] Convert SVG to PDF in python

查看:326
本文介绍了在python中将SVG转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用svglib和reportlab包将python中的SVG文件转换为PDF。这是SVG文件。

I am trying to convert an SVG file to PDF in python using the svglib and reportlab packages. Here is the 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">
  <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="blue" />
</svg>



这是我用来转换的代码(从svglib网页获取)。



>>> ;来自svglib.svglib import svg2rlg

>>>来自reportlab.graphics import renderPDF

>>> drawing = svg2rlg(file.svg)

>>> renderPDF.drawToFile(drawing,file.pdf)



代码运行正常,没有任何错误或异常,但由此生成的file.pdf是一个空白文件。我的意思是,当我打开这个PDF文件时,我只看到白色背景页面上没有任何内容。



我哪里错了?


Here is the code I am using to convert (got this from svglib webpage).

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

The code runs fine without any error or exception but the file.pdf thus generated is a blank file. I mean that when I open this PDF file I see nothing but only white background page with nothing on it.

Where am I going wrong ?

推荐答案

我刚刚做了类似的事情,并以此线程作为起点。



然而,我偶然发现了一个问题,这也可能是其他人感兴趣的:



当一个人通过使用物理单位(如英寸或毫米)实际定义了svg文件中的svg尺寸时, renderPDF.drawToFile方法将所有这些单位解释为Points。



因此,如果你想准备转换打印机,意味着在打印期间不需要缩放PDF,您可以这样做:



I have just worked on something similar and used this thread as a starting point.

However, I stumbled upon a problem, that may be of interest for others as well:

When one actually has defined the svg dimensions in the svg file by using physical units, like inches or mm, the renderPDF.drawToFile method will interprete all those units as Points.

So if you want to do the conversion printer ready, meaning there will be no scaling needed during printing of the PDF, you can do it like this:

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
drawing = svg2rlg("file.svg")
scaleFactor = 1./0.3527
drawing.width *= scaleFactor
drawing.height *= scaleFactor
drawing.scale(scaleFactor, scaleFactor)
renderPDF.drawToFile(drawing, "file.pdf")





上面的比例因子是从mm到Points。只需查看您需要的因素,例如这里:

http://en.wikipedia.org/wiki/Point_% 28typography%29#Current_DTP_point_system [ ^ ]


需要进行最后一行:

Need to make that last line:
renderPDF.drawToFile(drawing, "file.pdf", autoSize=0)



autoSize的正常参数值为1,这导致PDF与绘图的大小相同。



问题在于svg文件没有大小参数。你可以,例如将svg开始标记更改为:


The normal parameter value for autoSize is 1 which results in the PDF being the same size as the drawing.

The problem is with svg file having no size parameters. You can e.g. change the svg opening tag to:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="1000px" width="1000px">





to在不使用autoSize = 0



to get a similar (visible) result without using autoSize=0


这篇关于在python中将SVG转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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