User:Alexander Roidl/reportlab
< User:Alexander Roidl
Revision as of 11:16, 24 March 2018 by Alexander Roidl (talk | contribs)
Reportlab
Using the Base Doc Template
import the BaseDocTemplate
from reportlab.platypus import BaseDocTemplate
all other dependencies of Reportlab nee to be imported as well
for example a few imports:
from reportlab.pdfgen import canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import cm
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import A5
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_JUSTIFY
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.pdfmetrics import registerFontFamily
from reportlab.platypus.tableofcontents import TableOfContents
from reportlab.graphics.shapes import Drawing, Rect, Line
from reportlab.pdfbase.ttfonts import TTFont
initialize the Doc Template
doc = BaseDocTemplate('layout.pdf',showBoundary=0, pagesize=A5, leftMargin=0.7*cm, bottomMargin=1.5*cm, topMargin=0.7*cm, rightMargin=0.7*cm)
layout.pdf is the outputname showBoundary can be set to 1 to show the borders (for debugging)
in this Template you can draw Frames where the Text will flow in: (this example is for making a 2 columns layout)
frame1 = Frame(doc.leftMargin+innerMargin, doc.bottomMargin, (doc.width-innerMargin)/2-6, doc.height, leftPadding=0, rightPadding=0, topPadding=0, bottomPadding=0,id='col1')
frame2 = Frame(doc.leftMargin+innerMargin+(doc.width-innerMargin)/2+6, doc.bottomMargin, (doc.width-innerMargin)/2-6, doc.height,leftPadding=0, rightPadding=0, topPadding=0, bottomPadding=0, id='col2')
you need to add this frames to the Template:
doc.addPageTemplates([PageTemplate(id='TwoCol1',frames=[frame1,frame2]), ])
In order to format your Text you need to make styles:
style = ParagraphStyle(
name='Normal',
fontName='MyFont',
fontSize=9,
leading=12,
leftIndent=0,
rightIndent=0,
firstLineIndent=0,
alignment=TA_LEFT,
spaceBefore=0,
spaceAfter=0,
bulletFontName='Times-Roman',
bulletFontSize=10,
bulletIndent=0,
textColor= black,
backColor=None,
wordWrap=None,
borderWidth= 0,
borderPadding= 0,
borderColor= None,
borderRadius= None,
allowWidows= 0,
allowOrphans= 0,
textTransform=None, # 'uppercase' | 'lowercase' | None
endDots=None,
splitLongWords=1,
)
Now you can draw to the Frame with:
Elements=[]
content = ''' This is what my content is '''
Elements.append(Paragraph(content,style))
Elements.append(PageBreak())
doc.multiBuild(Elements)
More on Frames
…
More on Templates
…
More on Fonts
…