从svg转换为pdf

时间:2014-04-28 16:06:12

标签: python pdf svg

我尝试使用以下代码将svg文件转换为pdf,但收到错误

的src:

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

错误:

pydev debugger: starting
Traceback (most recent call last):
  File "C:\Users\Stone\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1446, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\Users\Stone\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1092, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "C:\Users\Stone\Documents\FastDose\src\Beagle\Prove e test\test_renderpdf.py", line 9, in <module>
    drawing = svglib.svg2rlg("test.svg")
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 1178, in svg2rlg
    svgRenderer.render(svg)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 511, in render
    self.render(child, self.mainGroup)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 534, in render
    item = self.renderG(n)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 626, in renderG
    item = self.render(child, parent=gr)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 534, in render
    item = self.renderG(n)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 626, in renderG
    item = self.render(child, parent=gr)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 534, in render
    item = self.renderG(n)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 626, in renderG
    item = self.render(child, parent=gr)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 554, in render
    shape = getattr(self.shapeConverter, methodName)(n)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 875, in convertPath
    normPath = normaliseSvgPath(d)
  File "C:\Python27\lib\site-packages\svglib\svglib.py", line 146, in normaliseSvgPath
    a = string.replace(a, 'e-', 'ee')
NameError: global name 'string' is not defined

此外,我想避免使用硬文件并使用StringIO创建临时的svg / pdf文件,但我想svg2rlg只接受一个路径作为输入。

1 个答案:

答案 0 :(得分:6)

让我开始工作的是手工编辑:

/Users/<MY_USER>/.virtualenvs/<MY_VENV>/lib/python2.7/site-packages/svglib/svglib.py

并添加此缺失的

import string