[reportlab-users] Integrate "Flowable" (SVG) with caching

Robin Becker robin at reportlab.com
Thu Nov 16 08:38:33 EST 2017


On 16/11/2017 12:57, Dinu Gherman wrote:
> Hi Yannick,
> 
> have you tried using svglib that I originally wrote in the context of reportlab? Using it you can import an SVG file and render it as a reportlab flowable. I’m just not sure if it will be cached and reused the same way as bitmap images over many pages. And I haven’t really produced reportlab-generated docs in a long time.
> 
> https://github.com/deeplook/svglib
> 
> Cheers,
> 
> Dinu
>
The current code draws directly into the canvas, so if a repeated image is 
required the code for drawing is repeated and document gets larger.

My hack solution would be to draw into a form and then render the form as often 
as required.

Yannick is correct in saying that the form is created in it's own space, but if 
that's zero based then when you use the form you can surround the doForm with

saveState
scale translate etc etc
doForm
restoreState

to position the image anywhere on the page see below.

Obviously this is more work than just using a built in method, but it should be 
easy enough to make an svg cache.

EG

############################################
from reportlab.pdfgen.canvas import Canvas

canv= Canvas('tform.pdf')

#define a form
canv.beginForm('myform')
canv.setFont('Times-Roman',10)
canv.setFillColor((1,0,0))
canv.setStrokeColor((0,1,0))
canv.setLineWidth(2)
s='In myform'
w=canv.stringWidth(s)
canv.drawString(2,4,s)
canv.rect(0,0,2+w,14,stroke=1)
canv.endForm()

canv.drawString(10,10,'Hello World')


canv.saveState()
canv.translate(100,100)
canv.lines([(0,-5,0,5), (-5,0,5,0)])
canv.doForm('myform')
canv.restoreState()

canv.saveState()
canv.translate(200,200)
canv.scale(1.5,1.5)
canv.lines([(0,-5,0,5), (-5,0,5,0)])
canv.doForm('myform')
canv.restoreState()

canv.saveState()
canv.translate(300,300)
canv.lines([(0,-5,0,5), (-5,0,5,0)])
canv.doForm('myform')
canv.restoreState()

canv.showPage()
canv.save()
############################################




>> Am 15.11.2017 um 22:23 schrieb Yannick Gosteli @ PETZI <yannick.gosteli at petzi.ch>:
>>
>> Dear,
>>
>> I generate a document that contains a SVG repeated several times.
>> I wonder if there is a way to integrate this SVG with "caching" the same way it works with Canvas.drawImage*.
>>
............
-- 
Robin Becker


More information about the reportlab-users mailing list