[reportlab-users] SVG and EPS

Robin Becker robin at reportlab.com
Tue Aug 29 12:56:15 EDT 2006

Rob Schall wrote:
> Wow, most of that definitely went way over my head.  The code below I 
> assume will make a "object" from scratch. If I already have an svg file 
> or eps file, there isn't a function, or set of functions to simply 
> convert it to a drawing object, in which i can do things like drawOn? I 
> came across a svglib.py file, which allows me to import it and 
> supposedly draw it, but that isn't quite working.

unfortunately not. We have stuff for drawing to svg, but not parsing it. 
Likewise eps is a pretty big language. Usually I just look inside the eps and 
see if the operators make some sense. Then I can start creating a drawing object 
or function which abstracts the various movetos linetos curvetos etc etc to 
create a path which will draw the outline and or fill it.

Inside an eps I might see something like

331.7844 260.1304 m
320.6108 260.1304 314.558 265.8712 314.558 274.7176 c
314.558 322.98 L
303.5404 322.98 L
303.5404 327.7904 L
314.558 327.7904 L
314.558 354.6368 L
320.766 354.6368 327.2836 354.4816 333.646 354.6368 c
333.646 327.7904 L
349.4748 327.7904 L
349.4748 322.98 L
333.646 322.98 L
333.646 264.9408 L
349.7852 264.9408 L
349.7852 260.1304 L
331.7844 260.1304 l

well the m is a moveto, the c's are curvetos, the L's are linetos and the l is a 
  closing lineto (I think it means join back to the start of the path). The f 
says fill.

Well we can do that like this

from reportlab.graphics.shapes import Drawing, Group, definePath
from reportlab.colors import red, green
class HackPath(Drawing):
     def __init__(self,width=400,height=400,*args,**kw):
         self.P.fillColor = green
         self.P.strokeColor = red
         self.P.strokeWidth = 2

if __name__=="__main__": #NORUNTESTS

Which is something you can run and have a look at the output HackPath000.pdf.
Robin Becker

More information about the reportlab-users mailing list