Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting from svg to pdf

Tags:

python

pdf

svg

I'm trying to convert a svg file to a pdf with the following code, but got an error

src:

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

error:

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

Moreover I'd like to avoid hard-file and use StringIO to create a temporary svg/pdf file, but I guess that svg2rlg only accept a path as input.

like image 447
gerod Avatar asked Apr 28 '14 16:04

gerod


People also ask

Can SVG be converted to PDF?

This is what you need to do in Inkscape to convert the svg to PDF: Open Inkscape and click File->Open to load the file for conversion. Click File then Print or press Ctrl+P and in the Print window select novaPDF as the printer. Click on Print and the SVG will be saved as PDF.

Is SVG a PDF vector file?

SVG is a vector graphic image file extension that contains scalable images. This XML based file extension supports animation that can contains vector graphics, raster graphics, and text. It uses lossless data compression algorithm to contain data. It can be edited drawing software as well as text editors also.


1 Answers

For me to get it working was hand edit of:

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

and adding this missing

import string

like image 191
andilabs Avatar answered Oct 10 '22 05:10

andilabs