Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_css_stylesheet(self):
drawing = svglib.svg2rlg(io.StringIO(textwrap.dedent(u'''\
<svg xml:space="preserve" height="267" width="777">
<defs>
<style type="text/css">
#p1 { fill:rgb(255,0,0); }
#p2 { fill:rgb(255,0,0); }
.paths { stroke-width:1.5; }
</style>
</defs>
<g id="g1">
<path d="M 0,-100 V 0 H 50" class="paths" id="p1"></path>
<path d="M 0,100 V 0 H 50" style="fill: #000000" class="paths other" id="p2"></path>
</g>
</svg>
''')))
main_group = drawing.contents[0]
def test_use(self, drawing_source=None):
drawing = svglib.svg2rlg(io.StringIO(textwrap.dedent(drawing_source or '''\
<svg viewBox="0 0 100 30" height="3cm" width="10cm" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<rect height="10" width="60" id="MyRect"></rect>
</defs>
<rect stroke-width=".2" stroke="blue" fill="none" height="29.8" width="99.8" y=".1" x=".1"></rect>
</svg>
''')))
main_group = drawing.contents[0]
# First Rect
def svg_demo(image_path, output_path):
"""
Convert an SVG to a PDF
"""
drawing = svg2rlg(image_path)
doc = SimpleDocTemplate(output_path,
rightMargin=0,
leftMargin=0)
story = []
story.append(drawing)
doc.build(story)
def __init__(self,
filename,
width=None,
height=None,
kind='direct',
mask=None,
lazy=True,
srcinfo=None):
Flowable.__init__(self)
self._kind = kind
self._mode = 'svg2rlg'
self.doc = svg2rlg(filename)
self.imageWidth = width
self.imageHeight = height
x1, y1, x2, y2 = self.doc.getBounds()
# Actually, svg2rlg's getBounds seems broken.
self._w, self._h = x2, y2
if not self.imageWidth:
self.imageWidth = self._w
if not self.imageHeight:
self.imageHeight = self._h
self.__ratio = float(self.imageWidth)/self.imageHeight
if kind in ['direct', 'absolute']:
self.drawWidth = width or self.imageWidth
self.drawHeight = height or self.imageHeight
elif kind in ['bound', 'proportional']:
factor = min(
float(width) / self.imageWidth,
output = FilteredPDFOutputter(nlines+5, num_columns, output_path_prefix+".svg")
for line in lines:
if line.startswith("
def svg_demo(image_path, output_path):
"""
Convert an SVG to a PDF
"""
drawing = svg2rlg(image_path)
doc = SimpleDocTemplate(output_path)
story = []
story.append(drawing)
doc.build(story)
def add_section_methylation(self, coverage=[]):
section = self.get_section_number()
subsection = itertools.count(1)
self.story.append(Paragraph("{:d} Methylation".format(section), self.heading_style))
self.story.append(Spacer(1, 0))
if coverage:
self.story.append(Paragraph("{:d}.{:d} CpG coverage".format(section, next(subsection)), self.heading2_style))
for f, label in coverage:
im = svg2rlg(f)
im = Image(im, width=im.width * self.plot_scale, height=im.height * self.plot_scale)
im.hAlign = 'CENTER'
self.story.append(Paragraph(label, self.normal_style))
self.story.append(im)
self.story.append(Spacer(1, 0))
def add_image(image_path, scaling_factor):
"""
Scale an SVG and add it to a PDF
"""
my_canvas = canvas.Canvas('svg_scaled_on_canvas.pdf')
drawing = svg2rlg(image_path)
scaled_drawing = scale(drawing, scaling_factor=scaling_factor)
renderPDF.draw(scaled_drawing, my_canvas, 0, 40)
my_canvas.drawString(50, 30, 'My SVG Image')
my_canvas.save()
def draw_svg(self, canvas, path, xpos=0, ypos=0, xsize=None, ysize=None):
from reportlab.graphics import renderPDF
from svglib.svglib import svg2rlg
drawing = svg2rlg(path)
xL, yL, xH, yH = drawing.getBounds()
if xsize:
drawing.renderScale = xsize / (xH - xL)
if ysize:
drawing.renderScale = ysize / (yH - yL)
renderPDF.draw(drawing, canvas, xpos, ypos, showBoundary=self.show_boundaries)