[reportlab-users] Setting TrueType fonts in table cell

Nils Smeds nils.smeds at gmail.com
Mon Oct 14 08:47:00 EDT 2013


I am pretty sure you are right - deriving my own Drawing class from
Flowable looks like the way to go.
Your example shows me what I need to implement in this class(and how).

Thanks for your help! Now I also know what sections in the manual to study
further and not to be deterred
by the comments in the manual ;-)


Class Flowable:

Abstract base class for things to be drawn. Key concepts:

1. It knows its size

2. It draws in its own coordinate system (this requires the base API to
provide a translate() function.

def drawOn(self, canvas, x, y, _sW=0):

Tell it to draw itself on the canvas. Do not override


/Nils



On Mon, Oct 14, 2013 at 12:30 PM, Robin Becker <robin at reportlab.com> wrote:


> Hi Nils,

>

> On 13/10/2013 07:42, Nils Smeds wrote:>> I think the error is that you are

> re-using the canvas at the end of the

> .............

>

> > When starting this project all I had as input was the users guide which

> in

> > all examples I read refers to the SimpleDocTemplate. I'd be happy to use

> a

> > more suitable setup if I could find references as to how to use a

> > "ComplexDocTemplate". Would it be possible to get some references to

> more

> > complex layouts and what is provided by the existing Templates (and how

> to

> > create your own if that is what it takes) on the FAQ page or in the use's

> > guide?

>

> Form the above I think you just need the ability to add drawn material of

> your own in a flowing document.

>

> A simple way to accomplish that would be to define a Flowable subclass

> that allows you to do whatever you want to a page with canvas commands, but

> uses the Flowable api. In the code below that's what the DrawingStuff class

> does.

>

> ##############################**##############################**

> ##################

> import sys

>

> from reportlab.lib.pagesizes import letter, A4

> from reportlab.pdfbase import pdfmetrics

> from reportlab.pdfbase.ttfonts import TTFont

> from reportlab.pdfbase.pdfmetrics import registerFontFamily

>

> from reportlab.lib.colors import black, red, blue, green

> from reportlab.lib.styles import getSampleStyleSheet

> from reportlab.platypus import Table,TableStyle

> from reportlab.platypus import Paragraph, SimpleDocTemplate, PageBreak,

> Flowable, FrameBreak, PageBegin

>

> # My Liberation fonts are installed in: /usr/share/fonts/liberation

> pdfmetrics.registerFont(**TTFont('SansR', '/windows/fonts/arial.ttf'))

> pdfmetrics.registerFont(**TTFont('SansB', '/windows/fonts/arialbd.ttf'))

> pdfmetrics.registerFont(**TTFont('SansI', '/windows/fonts/ariali.ttf'))

> pdfmetrics.registerFont(**TTFont('SansBI', '/windows/fonts/arialbi.ttf'))

> registerFontFamily('Sans',**normal='SansR',bold='SansB',**

> italic='SansI',boldItalic='**SansBI')

>

> def d0(canv):

> canv.setStrokeColor(red)

> canv.setFillColor(red)

> canv.setFont("Courier", 20)

> canv.drawString(72, 144, 'C TALLY HOOO!!! c')

> canv.setFont("SansB", 20)

> canv.drawString(72, 360, 'C TALLY HOOO!!! c')

>

> def d1(canv):

> canv.setStrokeColor(green)

> canv.setFillColor(green)

> canv.setFont("Courier", 20)

> canv.drawString(144, 144, 'C TALLY HOOO!!! c')

> canv.setFont("SansB", 20)

> canv.drawString(144, 360, 'C TALLY HOOO!!! c')

>

> def d2(canv):

> canv.setStrokeColor(blue)

> canv.setFillColor(blue)

> canv.setFont("Courier", 20)

> canv.drawString(72, 360, 'C TALLY HOOO!!! c')

> canv.setFont("SansB", 20)

> canv.drawString(360, 144, 'C TALLY HOOO!!! c')

>

> class DrawingStuff(Flowable):

> '''

> Flowable that allows arbitrary canvas operations

> DrawingStuff(myFunc)

> argument myFunc is a callable that will be passed the canvas at

> draw time.

> '''

> _ZEROSIZE=1

> _SPACETRANSFER = True

> def __init__(self,myDrawingFunc):

> self._myDrawingFunc = myDrawingFunc

>

> def __repr__(self):

> return "%s(%r)" % (self.__class__.__name__,self.**_myDrawingFunc)

>

> def wrap(self,aW,aH):

> return 0,0

>

> def drawOn(self,canvas,x,y,_sW=0):

> canvas.saveState()

> self._myDrawingFunc(canvas)

> canvas.restoreState()

>

> filename='mytest.pdf'

>

> page_width, page_height = A4

> doc = SimpleDocTemplate(filename,**pagesize=A4)

> story=[]

> story_add=story.append

>

> styles = getSampleStyleSheet()

> styleH = styles['Heading1']

> styleN = styles['Normal']

>

> base_style=[('GRID', (0,0), (-1,-1), 1, (0,0,0)),

> ('FONT', (1,0), (1,-1), 'SansR'),

> ('BOX', (0,0), (-1,-1), 3, (0,0,0)),

> ('ALIGN',(1,0),(1,-1),'RIGHT')**,

> ('ALIGN',(3,0),(3,-1),'RIGHT')**,

> ('VALIGN',(0,0),(-1,-1),'TOP')**]

>

> data=[('COL1','COL2','COL3','**COL4','COL5')]

> data.append(['COL1','COL2','**COL3','COL4','COL5'])

> data.append(['COL1','COL2','**COL3','COL4','COL5'])

> data.append(['COL1','COL2','**COL3','COL4','COL5'])

> t=Table(data,style=base_style)

> story_add(t)

>

> #generate some psecial pages

> story_add(PageBreak())

> story_add(DrawingStuff(d0))

> story_add(PageBreak())

> story_add(DrawingStuff(d1))

> story_add(PageBreak())

> story_add(DrawingStuff(d2))

>

> doc.build(story)

> ##############################**##############################**

> ##################

> --

> Robin Becker

>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20131014/83e0b9cd/attachment.html>


More information about the reportlab-users mailing list