def setTtfFonts(familyName, font_dir, normal=(None, None), bold=(None, None), italic=(None, None), bold_italic=(None, None)): """ Sets fonts for True Type Fonts """ normalName, normalFile = normal boldName, boldFile = bold italicName, italicFile = italic bold_italicName, bold_italicFile = bold_italic pdfmetrics.registerFont( TTFont(normalName, os.path.join(font_dir, normalFile))) pdfmetrics.registerFont(TTFont(boldName, os.path.join(font_dir, boldFile))) pdfmetrics.registerFont( TTFont(italicName, os.path.join(font_dir, italicFile))) pdfmetrics.registerFont( TTFont(bold_italicName, os.path.join(font_dir, bold_italicFile))) addMapping(familyName, 0, 0, normalName) addMapping(familyName, 1, 0, boldName) addMapping(familyName, 0, 1, italicName) addMapping(familyName, 1, 1, bold_italicName) base_fonts().update({"normal": getFont(normalName).fontName}) base_fonts().update({"bold": getFont(boldName).fontName}) base_fonts().update({"italic": getFont(italicName).fontName}) base_fonts().update({"bold_italic": getFont(bold_italicName).fontName})
def drawLaterLandscape(canv, doc): """ This is the Template of any later drawn Landscape Oriented Page """ canv.saveState() #set Page Size and #some variables``````````` frame, pagesize = doc.getFrame(temp_name=doc.template_id) print(doc.template_id, frame._width, frame._height) canv.setPageSize(pagesize) canv.setFont(base_fonts()["normal"], doc.fontSize) # doc.centerM = (frame._width - (frame._leftPadding + frame._rightPadding))/2 # doc.leftM = frame._leftPadding # doc.rightM = frame._width - frame._rightPadding # doc.headM = (frame._height - frame._topPadding) + doc.topM # doc.bottomM = frame._bottomPadding - doc.topM #addPlugin(canv, doc, frame="Later") canv.restoreState()
def __init__(self, gridded=False, leftTablePadding=0, hTableAlignment=None, colWidths=None): """ :param gridded: if True, the table style is gridded, default is False :type gridded: bool :param leftTablePadding: if a number > 0 is inserted, the table gets a left empty column to be like left-padded :type leftTablePadding: int """ self.hTableAlignment = hTableAlignment or TA_CENTER self.colWidths = colWidths self.rightPadding = 0 self.spaceBefore = 0 self.spaceAfter = 0 self.tableData = list() self.tableStyleCommands = list() # for finalizing specific cell formats self.tableExtraStyleCommands = list() self.fontsize = 10 self.font = getFont(base_fonts()["normal"]) self.addTableStyleCommand( ('FONT', (0, 0), (-1, -1), base_fonts()["normal"])) if gridded: self.addTableStyleCommand( ("GRID", (0, 0), (-1, -1), 0.5, colors.black)) if leftTablePadding: self.offsetCol = 1 self.leftTablePadding = leftTablePadding else: self.offsetCol = 0 self.leftTablePadding = 0 self.headerRow = 0 self.title = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(5)) # BUG: VALIGN does not work with different font sizes !!! self.addTableStyleCommand(('VALIGN', (0, 0), (-1, -1), 'BOTTOM')) self.addTableStyleCommand( ('FONTSIZE', (0, 0), (-1, -1), self.fontsize))
def drawFirstPortrait(canv, doc): """ This is the Title Page Template (Portrait Oriented) """ canv.saveState() #set Page Size frame, pagesize = doc.getFrame(doc.template_id) canv.setPageSize(pagesize) canv.setFont(base_fonts()["normal"], doc.fontSize) addPlugin(canv, doc, frame="First") canv.restoreState()
def drawLaterSpecialPortrait(canv, doc): """ This is the Template of any following Portrait Oriented Page """ canv.saveState() #set Page Size frame, pagesize = doc.getFrame(doc.template_id) canv.setPageSize(pagesize) canv.setFont(base_fonts()["normal"], doc.fontSize) addPlugin(canv, doc, frame="Later") canv.restoreState()
def drawLaterSpecialLandscape(canv, doc): """ This is the Template of any later drawn Landscape Oriented Page """ canv.saveState() #set Page Size and #some variables frame, pagesize = doc.getFrame(doc.template_id) canv.setPageSize(pagesize) canv.setFont(base_fonts()["normal"], doc.fontSize) addPlugin(canv, doc, frame="Later") canv.restoreState()
def addTableHeader(self, line, fonttype="bold", color="blue"): """ Prepends a table line to data and insert the correct styles, like double underline and bold. :param line: the data for the header line :type line: tuple, list :param fonttype: one of normal, bold, italic :type fonttype: str :param color: the color of the horizontal line :type color: str """ self.headerRow = 1 line = copy.copy(line) if isinstance(line, tuple): line = list(line) if self.leftTablePadding > 0: line.insert(0, "") self.tableData.insert(0, line) cmd = ('FONT', (self.offsetCol, 0), (-1, 0), base_fonts()[fonttype]) self.tableStyleCommands.append(cmd) self.addDoubleLine(color=color_dict().get(color))
def drawFirstLandscape(canv, doc): """ This is the Template of any later drawn Landscape Oriented Page the frame object is only used as a reference to be able to draw to the canvas After creation a Frame is not usually manipulated directly by the applications program -- it is used internally by the platypus modules. Here is a diagramatic abstraction for the definitional part of a Frame:: width x2,y2 +---------------------------------+ | l top padding r | h | e +-------------------------+ i | e | f | | g | i | t | | h | g | | | t | h | p | | | t | a | | p | | d | | a | | | | d | | +-------------------------+ | | bottom padding | +---------------------------------+ (x1,y1) <-- lower left corner NOTE!! Frames are stateful objects. No single frame should be used in two documents at the same time (especially in the presence of multithreading. """ canv.saveState() #set Page Size frame, pagesize = doc.getFrame(doc.template_id) canv.setPageSize(pagesize) canv.setFont(base_fonts()["normal"], doc.fontSize) addPlugin(canv, doc, frame="First") canv.restoreState()
def setFonts(typ): """ Sets fonts for standard font-types :param typ: one of sans-serif-afm, serif (sans-serif is default on init) :type typ: str """ if typ == 'sans-serif-afm': baseNameDict = { 'Helvetica': "_a______", 'Helvetica-Bold': "_ab_____", 'Helvetica-Oblique': "_ai_____", 'Helvetica-BoldOblique': "_abi____" } for afm, pfb in baseNameDict.items(): faceName = afm registerFont(faceName, afm, pfb) base_fonts().update( {"normal": pdfmetrics.getFont('Helvetica').fontName}) base_fonts().update( {"bold": pdfmetrics.getFont('Helvetica-Bold').fontName}) base_fonts().update( {"italic": pdfmetrics.getFont('Helvetica-Oblique').fontName}) base_fonts().update({ "bold_italic": pdfmetrics.getFont('Helvetica-BoldOblique').fontName }) elif typ == 'serif': setTtfFonts('Calibri', __font_dir__, normal=('Calibri', 'CALIBRI.TTF'), italic=('CalibriBd', 'CALIBRIB.TTF'), bold=('CalibriIt', 'CALIBRII.TTF'), bold_italic=('CalibriBI', 'CALIBRIZ.TTF'))
def drawFirstPortrait(canv, doc): """ This is the Title Page Template (Portrait Oriented) """ canv.saveState() #set Page Size frame, pagesize = doc.getFrame(temp_name=doc.template_id) print(doc.template_id, frame._width, frame._height) canv.setPageSize(pagesize) canv.setFont(base_fonts()["normal"], doc.fontSize) # doc.centerM = (frame._width-(frame._leftPadding + frame._rightPadding))/2 # doc.leftM = frame._leftPadding # doc.rightM = frame._width-frame._rightPadding # doc.headM = (frame._height - frame._topPadding) + doc.topM # doc.bottomM = frame._bottomPadding - doc.topM #addPlugin(canv, doc, frame="First") canv.restoreState()
def __init__(self): self.stylesheet = StyleSheet() self.addStyle( ParagraphStyle( name='Normal', fontName=base_fonts()["normal"], fontSize=10, bulletFontName=base_fonts()["normal"], leading=12)) self.addStyle( ParagraphStyle( name='BodyText', parent=self.stylesheet['Normal'], spaceBefore=6), alias='normal') self.addStyle( ParagraphStyle( name='Table', parent=self.stylesheet['Normal'], spaceBefore=0), alias='table') self.addStyle( ParagraphStyle( name='Italic', parent=self.stylesheet['BodyText'], fontName=base_fonts()["italic"]), alias='italic') self.addStyle( ParagraphStyle( name='Bold', parent=self.stylesheet['BodyText'], fontName=base_fonts()["bold"]), alias='bold') self.addStyle( ParagraphStyle( name='Centered', parent=self.stylesheet['BodyText'], alignment=TA_CENTER), alias='centered') self.addStyle( ParagraphStyle( name='Right', parent=self.stylesheet['BodyText'], alignment=TA_RIGHT, wordWrap=False), alias='right') self.addStyle( ParagraphStyle( name='Title', parent=self.stylesheet['Normal'], fontName=base_fonts()["bold"], fontSize=18, leading=22, alignment=TA_CENTER, spaceAfter=6), alias='title') self.addStyle( ParagraphStyle( name='Heading1', parent=self.stylesheet['Normal'], fontName=base_fonts()["bold"], fontSize=18, leading=22, spaceAfter=6), alias='h1') self.addStyle( ParagraphStyle( name='Heading2', parent=self.stylesheet['Normal'], fontName=base_fonts()["bold"], fontSize=14, leading=18, spaceBefore=12, spaceAfter=6), alias='h2') self.addStyle( ParagraphStyle( name='Heading3', parent=self.stylesheet['Normal'], fontName=base_fonts()["bold"], fontSize=12, leading=14, spaceBefore=12, spaceAfter=6), alias='h3') self.addStyle( ParagraphStyle( name='Heading4', parent=self.stylesheet['Normal'], fontName=base_fonts()["bold"], fontSize=10, leading=12, spaceBefore=10, spaceAfter=4), alias='h4') self.addStyle( ParagraphStyle( name='Heading5', parent=self.stylesheet['Normal'], fontName=base_fonts()["bold"], fontSize=9, leading=10.8, spaceBefore=8, spaceAfter=4), alias='h5') self.addStyle( ParagraphStyle( name='Heading6', parent=self.stylesheet['Normal'], fontName=base_fonts()["bold"], fontSize=7, leading=8.4, spaceBefore=6, spaceAfter=2), alias='h6') self.addStyle( ParagraphStyle( name='Heading6', parent=self.stylesheet['Normal'], fontName=base_fonts()["bold"], alignment=TA_CENTER, fontSize=12, leading=8.4, spaceBefore=14, spaceAfter=2), alias='caption') self.addStyle( ParagraphStyle( name='Bullet', parent=self.stylesheet['Normal'], firstLineIndent=0, spaceBefore=3), alias='bu') self.addStyle( ParagraphStyle( name='Definition', parent=self.stylesheet['Normal'], firstLineIndent=0, leftIndent=36, bulletIndent=0, spaceBefore=6, bulletFontName=base_fonts()["bold_italic"]), alias='df') self.addStyle( ParagraphStyle( name='Code', parent=self.stylesheet['Normal'], fontName='Courier', alignment=TA_LEFT, fontSize=8, leading=8.8, firstLineIndent=0, leftIndent=36), alias='code') self.addStyle( ParagraphStyle( name='ConsoleText', parent=self.stylesheet['Normal'], fontName='Courier', alignment=TA_LEFT, fontSize=11, leading=14, #16 firstLineIndent=0, leftIndent=0, spaceBefore=2, spaceAfter=0), alias='console') self.addStyle( ParagraphStyle( name='Warning', parent=self.stylesheet['Normal'], fontName='Courier', alignment=TA_LEFT, fontSize=11, textColor='red', leading=14, #16 firstLineIndent=0, leftIndent=10, spaceBefore=0, spaceAfter=0), alias='warning') # Fixed Notation self.p2 = ParagraphStyle( name='Heading2', # must be according to TOC depth parent=self.stylesheet['Normal'], fontSize=10, leading=12) self.p3 = ParagraphStyle( name='Heading3', # must be according to TOC depth parent=self.stylesheet['Normal'], fontSize=10, leading=12)
def getBaseFont(fonttype): if fonttype in base_fonts(): return base_fonts()[fonttype] else: return None
from reportlab.platypus.frames import Frame from reportlab.platypus.flowables import SlowPageBreak, DDIndenter, PageBreakIfNotEmpty from reportlab.pdfgen import canvas from reportlab.lib.styles import ParagraphStyle # Configure Fonts! from reportlab.pdfbase.pdfdoc import PDFInfo from autobasedoc import base_fonts, color_dict, colors from autobasedoc.styledtable import StyledTable from autobasedoc.styles import StyleSheet, Styles from autobasedoc.pageinfo import addPlugin, PageInfo from autobasedoc.fonts import registerFont, setFonts, setTtfFonts, getFont from autobasedoc.tableofcontents import AutoTableOfContents _baseFontNames = base_fonts() _color_dict = color_dict() _basePath = os.path.realpath(os.path.dirname(__file__)) sys.path.append(_basePath) # #TODO: cx Freeze, this needs adoption to your version of cx_freeze # if _basePath.endswith("library.zip"): # _basePath = os.path.realpath(os.path.join(_basePath, '../')) __font_dir__ = os.path.realpath(os.path.join(_basePath, "fonts")) #__assets_dir__ = os.path.realpath(os.path.join(_basePath,"assets")) def reprFrame(frame): _dict = vars(frame)