def __init__(self, id, pageSize=defaultPageSize): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] colWidth = 0.5 * (self.pageWidth - 2.25 * inch) frame1 = Frame(inch, inch, colWidth, self.pageHeight - 2 * inch, id="leftCol") frame2 = Frame(0.5 * self.pageWidth + 0.125, inch, colWidth, self.pageHeight - 2 * inch, id="rightCol") PageTemplate.__init__(self, id, [frame1, frame2]) # note lack of onPage
def __init__(self, title, subtitle, background=None): self.title = title self.subtitle = subtitle self.background = background dummy = Frame(1, 1, 200, 200, id='dummy') #ReportLab uses old style classes, so super() doesn't work. PageTemplate.__init__(self, id='cover', frames=[dummy])
def __init__(self, id, pageSize=defaultPageSize): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] frame1 = Frame(inch, 3*inch, self.pageWidth - 2*inch, self.pageHeight - 518, id='cover') PageTemplate.__init__(self, id, [frame1]) # note lack of onPage
def build(self, style=1): template = SimpleDocTemplate(config.get("paths.tempdir", "") + "print.pdf", showBoundary=0) tFirst = PageTemplate(id='First', frames=self.getStyle(1, style), onPage=self.myPages, pagesize=defaultPageSize) tNext = PageTemplate(id='Later', frames=self.getStyle(2, style), onPage=self.myPages, pagesize=defaultPageSize) template.addPageTemplates([tFirst, tNext]) template.allowSplitting = 1 BaseDocTemplate.build(template, self.data) return template.canv._doc.GetPDFData(template.canv)
def __init__(self, pageSize=pagesizes.A4): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] frame1 = Frame(0, 0, self.pageWidth, self.pageHeight, id='normal') PageTemplate.__init__(self, frames=[frame1])
def build_pdf(self): """Using different internal page methods and given the data provided build the report """ # doc options to abstract and set # doc.creator # doc.encrypt # doc.author # doc.subject # doc.title style = self.styles["Normal"] doc = BaseDocTemplate(self._filename) # Flowables to be added to document Story = [] # could create different sized frames and set addPageTemplate frames to array frames=[frame1, frame2] standard_frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='sframe') Story.append(NextPageTemplate('host')) # space for title page Story.append(PageBreak()) # Host data Story.append(Paragraph('Scanned Hosts ---------------------', self.styles['Heading2'])) for h in self._report['hosts']: if h.get_display_val(): hname = f'Hostname: {h.get_display_name()}' ip = f'IP Address: {h.get_ip()}' mac = f'MAC Address: {h.get_mac_address()}' Story.append(Paragraph(hname, style)) Story.append(Paragraph(ip, style)) Story.append(Paragraph(mac, style)) Story.append(Spacer(1, 0.2 * inch)) # cpe data Story.append(Paragraph('CPEs ---------------------', self.styles['Heading2'])) if self._report['cpes']: for c in self._report['cpes']: if self._report['cpes'][c][0]: cpe = self._report['cpes'][c][0] Story.append(Paragraph(cpe, style)) Story.append(Spacer(1, 0.2 * inch)) else: Story.append(Paragraph('No CPEs found', style)) # Vuln Data Story.append(Paragraph('Vulnerabilities ---------------------', self.styles['Heading2'])) if self._report['vulns']: for v in self._report['vulns']: Story.append(Paragraph(v, style)) else: Story.append(Paragraph('No vulnerabilities found.', style)) doc.addPageTemplates([PageTemplate(id='title', frames=standard_frame, onPage=self.title_page), PageTemplate(id='host', frames=standard_frame, onPage=self.host_page)]) doc.build(Story)
def __init__(self, id, pageSize=A4): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] frame1 = Frame(2.54 * cm, 1.25 * cm, self.pageWidth - 2 * 2.54 * cm, self.pageHeight - 2.54 * cm - 1.25 * cm, id='Cover') PageTemplate.__init__(self, id, [frame1]) # note lack of onPage
def __init__(self, id, pageSize=defaultPageSize): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] frame1 = Frame(inch, inch, self.pageWidth - 2 * inch, self.pageHeight - 2 * inch, id='normal') PageTemplate.__init__(self, id, [frame1]) # note lack of onPage
def main(sFilename,invoice): pdf_file = sFilename t = get_table(invoice) Elements = [] def pgHdr(c, doc): width,height = A4 #c.saveState() #c.translate(.3 * inch, 0 * inch) # STUFF RELATED TO 2 INCH STTIC HEADER FOR FIRST PAGE #c.restoreState() def othPg(c,doc): width,height = A4 styles = getSampleStyleSheet() Elements.append(Spacer(1,2*inch)) style = styles["Normal"] for i in range(3): bogustext = ("This is Paragraph number %s. " % i) * 2 p = Paragraph(bogustext, style) Elements.append(p) Elements.append(Spacer(1,0.2*inch)) doc = BaseDocTemplate(pdf_file, pagesize=A4, leftMargin=.3*inch, rightMargin= .1 * inch, topMargin= .1 * inch, bottomMargin=.3 * inch, showBoundary=0) #normal frame as for SimpleFlowDocument #frameT = Frame(doc.leftMargin + 2*inch, doc.bottomMargin, doc.width - 2.01*inch, doc.height - 4.1*inch, id='normal', showBoundary=0) #frameB = Frame(doc.leftMargin+2, doc.bottomMargin, 7.5*inch, 10*inch, id='small', showBoundary=0) frameT = Frame(doc.leftMargin , doc.bottomMargin + 1*inch, doc.width, doc.height, id='normal', showBoundary=0) frameB = Frame(doc.leftMargin , doc.bottomMargin + 1*inch, doc.width, doc.height, id='normal', showBoundary=0) doc.addPageTemplates([PageTemplate(id='First',frames=frameT,onPage=pgHdr), PageTemplate(id='Later',frames=frameB,onPage=othPg) ]) Elements.append(NextPageTemplate('First')) Elements.append(NextPageTemplate('Later')) Elements.append(t) #doc.addPageTemplates([PageTemplate(id='First',onPage=pgHdr)]) #Elements.append(BaseDocTemplate) #Elements.append(t) doc.build(Elements)
def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing, canvasmaker=canvas.Canvas): self._calc() #in case we changed margins sizes etc frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal') self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage,pagesize=self.pagesize), PageTemplate(id='Later',frames=frameT, onPage=onLaterPages,pagesize=self.pagesize)]) if onFirstPage is _doNothing and hasattr(self,'onFirstPage'): self.pageTemplates[0].beforeDrawPage = self.onFirstPage if onLaterPages is _doNothing and hasattr(self,'onLaterPages'): self.pageTemplates[1].beforeDrawPage = self.onLaterPages BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
def __init__(self, params, pageSize=pagesizes.landscape(pagesizes.A4)): self.params = params self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] frame1 = Frame(cm, cm, self.pageWidth - 2*cm, self.pageHeight - 2.5*cm, id='normal') PageTemplate.__init__(self, frames=[frame1])
def _make_pdf(self) -> bytes: """ Crea el pdf a partir de story. Ademas inicializa el doc con los estilo de las hojas y el tamaño. return pdf """ # La clase io.BytesIO permite tratar un array de bytes como un fichero binario, # se utiliza como almacenamiento temporal dentro de python, para luego ser descargado todo el dato como pdf pdf_buffer = BytesIO() #c = canvas.Canvas(buffer) doc = BaseDocTemplate( pdf_buffer, pagesize=A4) # Se pasa el pdf_buffer al BaseDocTemplate frame0 = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, showBoundary=0, id='normalBorde') # Frames o marcos de la pagina # Plantillas de las hojas, cabecera, pie de pagina, marco de la pagina. Se # puede tener varias plantillas. Siempre partira de la primera plantilla doc.addPageTemplates([ PageTemplate(id='primera_hoja', frames=frame0, onPage=self._cabecera_1, onPageEnd=self._pie_pagina), PageTemplate(id='contenido', frames=frame0, onPage=self._cabecera_contenido, onPageEnd=self._pie_pagina) ]) # Creamos las hojas de Estilos estilo = getSampleStyleSheet() estilo.add( ParagraphStyle(name="titulo_tablas_graficas", alignment=TA_CENTER, fontSize=15, fontName="Helvetica-Bold", textColor=colors.Color(0.0390625, 0.4921875, 0.69140625))) estilo.add( ParagraphStyle(name="texto", alignment=TA_LEFT, fontSize=12, fontName="Helvetica", textColor=colors.Color(0, 0, 0))) kargs = self._make_table_graphics( estilo) # Dicionario con las tablas y graficas para el story story = self._make_story(**kargs) doc.build(story) # Se construye el pdf con el array story #Descargando todo el buffer pdf = pdf_buffer.getvalue() pdf_buffer.close() return pdf
def __init__(self, pdf_template_filename, name=None): frames = [ Frame(0.85 * inch, 0.5 * inch, PAGE_WIDTH - 1.15 * inch, PAGE_HEIGHT - (1.5 * inch)) ] PageTemplate.__init__(self, name, frames) # use first page as template page = PdfReader(pdf_template_filename).pages[0] self.page_template = pagexobj(page) # Scale it to fill the complete page self.page_xscale = PAGE_WIDTH / self.page_template.BBox[2] self.page_yscale = PAGE_HEIGHT / self.page_template.BBox[3]
def __init__(self, id, pageSize=A4): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] frame3 = Frame(2.54 * cm, 1.25 * cm, self.pageWidth - 2 * 2.54 * cm, self.pageHeight - 2.54 * cm - 1.25 * cm - 1.75 * cm - (title_1_style.fontSize * 2) - logo_height, id='Credits') PageTemplate.__init__(self, id, [frame3]) # note lack of onPage
def render(self, articles, outfile): """ render list of articles as PDF into a byte file-like output. """ story = [] self.render_header(story) self.render_articles(articles, story) # build the pdf document # to show frame boundaries set showBoundary=1 doc = BaseDocTemplate(outfile, title='', author='') # first page, has space for drawing header firstPageTemplate = PageTemplate( id='First', autoNextPageTemplate='Next', frames=[ Frame(doc.leftMargin, doc.bottomMargin + doc.height - self.header_height, doc.width, self.header_height, id='header'), Frame(doc.leftMargin, doc.bottomMargin, doc.width / 2 - 6, doc.height - self.header_height, id='col1'), Frame(doc.leftMargin + doc.width / 2 + 6, doc.bottomMargin, doc.width / 2 - 6, doc.height - self.header_height, id='col2') ]) laterPagesTemplate = PageTemplate(id='Next', frames=[ Frame(doc.leftMargin, doc.bottomMargin, doc.width / 2 - 6, doc.height, id='col1'), Frame(doc.leftMargin + doc.width / 2 + 6, doc.bottomMargin, doc.width / 2 - 6, doc.height, id='col2') ]) doc.addPageTemplates([firstPageTemplate, laterPagesTemplate]) doc.build(story)
def __init__(self, id, pageSize=A4): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] frame2 = Frame(2.54 * cm, 1.25 * cm, self.pageWidth - 2 * 2.54 * cm, self.pageHeight - 2.54 * cm - 1.25 * cm - (title_1_style.fontSize * 2), id='Normal') PageTemplate.__init__(self, id, [frame2]) # note lack of onPage
def init_templates(self, page_fn, page_fn_later=None): self.doc.addPageTemplates([ PageTemplate( id='First', frames=[self.frame], onPage=page_fn), PageTemplate( id='Later', frames=[self.frame], onPage=page_fn_later or page_fn), ]) self.story.append(NextPageTemplate('Later'))
def __init__(self, aDocInfo, aStyleSheet): self.docInfo = aDocInfo self.style = aStyleSheet if "pageOrientation" in self.docInfo: self.orientation = self.docInfo["pageOrientation"] else: self.orientation = "portrait" if "pageSize" in self.docInfo: self.pageSize = A3 if self.docInfo["pageSize"] == "A3" else A4 else: self.pageSize = A4 if isinstance(self.pageSize, list): if len(self.pageSize) > 1: self.pageRect = [self.pageSize[0], self.pageSize[1]] else: self.pageRect = \ landscape(self.pageSize) \ if self.orientation == "landscape" \ else portrait(self.pageSize) self.doc = DocTemplateWithToc('', outputfilepagesize=self.pageRect, leftMargin=self.style["marginL"], rightMargin=self.style["marginR"], topMargin=self.style["marginT"], bottomMargin=self.style["marginB"]) portraitTempl = PageTemplate(id="portrait", frames=Frame(0, 0, self.pageRect[0], self.pageRect[1], leftPadding=self.style["marginL"], bottomPadding=self.style["marginB"], rightPadding=self.style["marginR"], topPadding=self.style["marginT"]), onPageEnd=self.drawDecoration, pagesize=self.pageRect) self.doc.addPageTemplates(portraitTempl) landscapeTempl = PageTemplate(id="landscape", frames=Frame(0, 0, self.pageRect[0], self.pageRect[1], leftPadding=self.style["marginL"], bottomPadding=self.style["marginB"], rightPadding=self.style["marginR"], topPadding=self.style["marginT"]), onPageEnd=self.drawDecoration, pagesize=self.pageRect) self.doc.addPageTemplates(landscapeTempl) self.doc.setDefaultTemplate(self.orientation) self.enabledDecorations = [] self.availableDecorations = []
def build(self, printfile, style=1): template = SimpleDocTemplate(printfile, showBoundary=0) tFirst = PageTemplate(id='First', frames=self.getStyle(1, style), onPage=self.myPages, pagesize=defaultPageSize) tNext = PageTemplate(id='Later', frames=self.getStyle(2, style), onPage=self.myPages, pagesize=defaultPageSize) template.addPageTemplates([tFirst, tNext]) template.allowSplitting = 1 BaseDocTemplate.build(template, self.data)
def __init__(self, id, pageSize=defaultPageSize, top=1, bottom=1, left=1, right=1): self.pagesize, (self.pageWidth, self.pageHeight) = pageSize, pageSize self.nTop, self.nBottom, self.nLeft, self.nRight = top * inch, bottom * inch, left * inch, right * inch frameList = self.CreateTemplateFrames( ) # overload this virtual function in all derived classes PageTemplate.__init__(self, id, frameList, pagesize=pageSize) # note lack of onPage
def __init__(self, student): self.student = student filename = slugify('{0}_{1}'.format( self.student.last_name, self.student.first_name)) + '_Expert.pdf' super().__init__(filename) self.addPageTemplates([ PageTemplate(id='FirstPage', frames=[self.page_frame], onPage=self.header), PageTemplate(id='ISOPage', frames=[self.page_frame], onPage=self.header_iso), ])
def __init__(self, pdf_template_filename, name=None): frames = [Frame( PAD, PAD, PAGE_WIDTH - 2 * PAD, PAGE_HEIGHT - 2 * PAD, )] PageTemplate.__init__(self, name, frames) # use first page as template page = PdfReader(pdf_template_filename, decompress=False).pages[0] self.page_template = pagexobj(page) # Scale it to fill the complete page self.page_xscale = PAGE_WIDTH/self.page_template.BBox[2] self.page_yscale = PAGE_HEIGHT/self.page_template.BBox[3]
def __init__(self, pdf_template_filename, name=None): frames = [Frame( 0.85 * inch, 0.5 * inch, PAGE_WIDTH - 1.15 * inch, PAGE_HEIGHT - (1.5 * inch) )] PageTemplate.__init__(self, name, frames) # use first page as template page = PdfReader(pdf_template_filename).pages[0] self.page_template = pagexobj(page) # Scale it to fill the complete page self.page_xscale = PAGE_WIDTH/self.page_template.BBox[2] self.page_yscale = PAGE_HEIGHT/self.page_template.BBox[3]
def multiBuild(self, story, filename=None, canvasMaker=Canvas, maxPasses=10, onFirstPage=_doNothing, onLaterPages=_doNothing): self._calc() #in case we changed margins sizes etc frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal') self.addPageTemplates([ PageTemplate(id='Later', frames=frameT, onPageEnd=onLaterPages, pagesize=self.pagesize) ]) if onLaterPages is _doNothing and hasattr(self, 'onLaterPages'): self.pageTemplates[0].beforeDrawPage = self.onLaterPages SimpleDocTemplate.multiBuild(self, story, maxPasses, canvasmaker=canvasMaker) self._prepareTOC() contentFile = self.filename self.filename = FileDummy() self.pageTemplates = [] self.addPageTemplates([ PageTemplate(id='First', frames=frameT, onPage=onFirstPage, pagesize=self.pagesize) ]) if onFirstPage is _doNothing and hasattr(self, 'onFirstPage'): self.pageTemplates[0].beforeDrawPage = self.onFirstPage self.addPageTemplates([ PageTemplate(id='Later', frames=frameT, onPageEnd=self.laterPages, pagesize=self.pagesize) ]) if onLaterPages is _doNothing and hasattr(self, 'onLaterPages'): self.pageTemplates[1].beforeDrawPage = self.onLaterPages SimpleDocTemplate.multiBuild(self, self._tocStory, maxPasses, canvasmaker=canvasMaker) self.mergePDFs(self.filename, contentFile)
def afterInit(self): """Set up the page templates""" frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal') self.addPageTemplates([ PageTemplate(id='Front', frames=frameT), PageTemplate(id='Body', frames=frameT) ]) # just need a unique key generator for outline entries; # easiest is to count all flowables in afterFlowable # and set up a counter variable here self._uniqueKey = 0
def __init__(self, id, pageSize=defaultPageSize): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] colWidth = 0.5 * (self.pageWidth - 2.25*inch) frame1 = Frame(inch, inch, colWidth, self.pageHeight - 2*inch, id='leftCol') frame2 = Frame(0.5 * self.pageWidth + 0.125, inch, colWidth, self.pageHeight - 2*inch, id='rightCol') PageTemplate.__init__(self, id, [frame1, frame2]) # note lack of onPage
def __init__(self, bg_page, name=None): frames = [Frame( 22.0*mm, 13.0*mm, PAGE_WIDTH-30.0*mm, PAGE_HEIGHT-30.0*mm, )] PageTemplate.__init__(self, name, frames) self.page_template = pagexobj(bg_page) # scale to fill the complete page self.page_xscale = PAGE_WIDTH/self.page_template.BBox[2] self.page_yscale = PAGE_HEIGHT/self.page_template.BBox[3] # store content and absolute position of paragraphs/primitives self.abspars = [] self.absprims = []
def __init__(self, empfaenger: Mitglied, abrechnung: Abrechnung, buffer=BytesIO()): self._abrechnung = abrechnung self._empfaenger = empfaenger self._buffer = buffer first_page_tpl = PageTemplate(id='first_page_tpl', onPage=self._first_page_static, frames=[ Frame( x1=10 * mm, y1=15 * mm, width=PAGE_WIDTH - 20 * mm, height=PAGE_HEIGHT - 105 * mm, showBoundary=DEBUG, id='mainFrame', ), ]) next_page_tpl = PageTemplate(id='next_page_tpl', onPage=self._next_page_static, frames=[ Frame(x1=10 * mm, y1=15 * mm, width=PAGE_WIDTH - 20 * mm, height=PAGE_HEIGHT - 25 * mm, showBoundary=DEBUG, id='mainFrame'), ]) self.doc = BaseDocTemplate( self._buffer, pagesize=A4, pageTemplates=[first_page_tpl, next_page_tpl], showBoundary=DEBUG, title=f'Abrechnung für {self.filename}', author=PDF_AUTHOR, # Die folgenden sind nur für nicht-flowables interessant: # (falls überhaupt) leftMargin=10 * mm, rightMargin=10 * mm, topMargin=10 * mm, bottomMargin=10 * mm, # ) self._elements = [NextPageTemplate('next_page_tpl')]
def __init__(self, filename, **kw): m = 2 * cm cw, ch = (PAGESIZE[0] - 2 * m) / 2., (PAGESIZE[1] - 2 * m) # if we replace the value 4.9 with 5.0, everything works as expected f1 = Frame(m, m + 0.5 * cm, cw + 4.9 * cm, ch - 1 * cm, id='F1', leftPadding=0, topPadding=0, rightPadding=0, bottomPadding=0, showBoundary=True) f2 = Frame(cw + 7 * cm, m + 0.5 * cm, cw - 5 * cm, ch - 1 * cm, id='F2', leftPadding=0, topPadding=0, rightPadding=0, bottomPadding=0, showBoundary=True) BaseDocTemplate.__init__(self, filename, **kw) template = PageTemplate('template', [f1, f2]) self.addPageTemplates(template)
def run(self): """ Run the report """ def make_landscape(canvas, doc): canvas.setPageSize(landscape(A4)) # file initialization in buffer self.doc = BaseDocTemplate( self.buffer, showBoundary=1, # margines pagesize=landscape(A4)) # create the frames. Here you can adjust the margins frame = Frame(self.doc.leftMargin - 65, self.doc.bottomMargin - 50, self.doc.width + 125, self.doc.height + 110, id='first_frame') # add the PageTempaltes to the BaseDocTemplate. # You can also modify those to adjust the margin # if you need more control over the Frames. self.doc.addPageTemplates( PageTemplate(id='first_page', frames=frame, onPage=make_landscape)) self.create_text() self.create_table() self.create_footer() self.doc.build(self.story)
def __init__(self, doc_or_path, coverpage=None, pagesize=None, stylesheet=None, showBoundary=0): self.path = None if isinstance(doc_or_path, str): self.path = doc_or_path doc = self.build_doc(doc_or_path, pagesize=pagesize, showBoundary=showBoundary) self.doc = doc self.pagesize = doc.pagesize self.width, self.height = self.pagesize self.inc_cover = inc_coverpage = coverpage is not None self.template_defs = {} self.story = [] self.active_template_id = None self.stylesheet = stylesheet or getSampleStyleSheet() if inc_coverpage: # Allow user to override the cover page template if not self.get_page_template('cover', err=0): f = Frame(0, 0, self.width, self.height) pt = PageTemplate(id='cover', frames=[f], onPage=coverpage.onPage) self.add_page_template(pt)
def as_page_template(self, builder): rheight = builder.height / self.nrows cwidth = builder.width / self.ncols frames = [] for alias, (gframe, fargs) in list(self.gframes.items()): rs, re, cs, ce = gframe.y0, gframe.y1, gframe.x0, gframe.x1 # Flip since y-axis starting at bottom rs, re = abs(self.nrows - rs), abs(self.nrows - re) x = cs * cwidth y = re * rheight h = (rs - re) * rheight w = (ce - cs) * cwidth frames.append(Frame(x, y, w, h, id=alias, **fargs)) pt = PageTemplate(frames=frames) pt.id = self.template_id return pt
def __init__(self, path): self.path = path styles = getSampleStyleSheet() styles2 = getSampleStyleSheet() self._styleN = styles["Normal"] self._styleH1 = styles["Heading1"] self._styleH2 = styles["Heading2"] self._styleRightAlignment = styles2["Normal"] # to make sure we don't overwrite the normal styles (IS HACK) self._styleRightAlignment.alignment = 2 self._header = "" self._footer = "" self._pagenr = 0 self.doc = BaseDocTemplate(self.path, pagesize=A4) self.doc.leftMargin = 1 * cm self.doc.rightMargin = 1 * cm self.doc.bottomMargin = 1.5 * cm self.doc.topMargin = 1.5 * cm frame = Frame(self.doc.leftMargin, self.doc.bottomMargin, self.doc.width, self.doc.height - 2 * cm, id="normal") template = PageTemplate(id="legal_doc", frames=frame, onPage=self._header_footer) self.doc.addPageTemplates([template]) self.parts = []
def go(): #create the basic page and frames doc = BaseDocTemplate("phello.pdf", leftMargin=10, rightMargin=0, topMargin=0, bottomMargin=0) doc.pagesize = landscape(A4) frameCount = 2 frameWidth = doc.height / frameCount frameHeight = doc.width - .05 * inch frames = [] #construct a frame for each column for frame in range(frameCount): leftMargin = doc.leftMargin + frame * frameWidth column = Frame(leftMargin, doc.bottomMargin, frameWidth, frameHeight) print leftMargin, doc.bottomMargin, frameWidth, frameHeight frames.append(column) template = PageTemplate(frames=frames) doc.addPageTemplates(template) # doc = SimpleDocTemplate("phello.pdf", id='TwoColumns') # doc.pagesize = landscape(A4) ## TODO: make configurable Story = [] style = styles["Normal"] style.fontName = 'Courier' style.fontSize = 6 for monkey in monkeys: p = Preformatted(get_ttview(monkey[0], monkey[1], 15), style) Story.append(KeepTogether(p)) Story.append(Spacer(1, 0.05 * inch)) doc.build(Story)
def _create_document(self, buff): page_t = PageTemplate( "normal", [ Frame( self.page_margin[0], self.page_margin[1], self.page_size[0] - self.page_margin[0] - self.page_margin[2], self.page_size[1] - self.page_margin[1] - self.page_margin[3], leftPadding=self.page_padding[0], bottomPadding=self.page_padding[3], rightPadding=self.page_padding[2], topPadding=self.page_padding[1], ) ], ) doc_t = self.doc_template_type( buff, pagesize=letter, title=self.title, author=self.author, subject=self.subject, creator=self.creator, leftMargin=self.page_margin[0], rightMargin=self.page_margin[2], topMargin=self.page_margin[1], bottomMargin=self.page_margin[3], ) doc_t.addPageTemplates(page_t) return doc_t
def _create_bulk_licence_renewal(licences, site_url, buf=None): bulk_licence_renewal_frame = Frame(LETTER_PAGE_MARGIN, LETTER_PAGE_MARGIN, PAGE_WIDTH - 2 * LETTER_PAGE_MARGIN, PAGE_HEIGHT - 160, id='BulkLicenceRenewalFrame') bulk_licence_renewal_template = PageTemplate( id='BulkLicenceRenewalFrame', frames=bulk_licence_renewal_frame, onPage=_create_letter_header_footer) if buf is None: buf = BytesIO() doc = BaseDocTemplate(buf, pageTemplates=[bulk_licence_renewal_template], pagesize=A4) # this is the only way to get data into the onPage callback function doc.site_url = site_url all_elements = [] for licence in licences: all_elements += _create_letter_address(licence) + [Spacer(1, LETTER_ADDRESS_BUFFER_HEIGHT)] + \ _create_licence_renewal_elements(licence) + _create_letter_signature() all_elements.append(PageBreak()) doc.build(all_elements) return doc
def __init__(self, filename, **kw): frame1 = Frame(2.5 * cm, 2.5 * cm, 16 * cm, 25 * cm, id='Frame1') self.allowSplitting = 0 self.showBoundary = 1 BaseDocTemplate.__init__(self, filename, **kw) template = PageTemplate('normal', [frame1], myMainPageFrame) self.addPageTemplates(template)
def __init__(self, filename, **kw): m = 2 * cm cw, ch = (PAGESIZE[0] - 2 * m) / 2., (PAGESIZE[1] - 2 * m) ch -= 14 * cm f1 = Frame(m, m + 0.5 * cm, cw - 0.75 * cm, ch - 1.9 * cm, id='F1', leftPadding=0, topPadding=0, rightPadding=0, bottomPadding=0, showBoundary=True) f2 = Frame(cw + 2.7 * cm, m + 0.5 * cm, cw - 0.75 * cm, ch - 1 * cm, id='F2', leftPadding=0, topPadding=0, rightPadding=0, bottomPadding=0, showBoundary=True) BaseDocTemplate.__init__(self, filename, **kw) template = PageTemplate('template', [f1, f2]) self.addPageTemplates(template)
def as_page_template(self, builder): rheight = builder.height / self.nrows cwidth = builder.width / self.ncols frames = [] for alias, (gframe, fargs) in self.gframes.iteritems(): rs, re, cs, ce = gframe.y0, gframe.y1, gframe.x0, gframe.x1 # Flip since y-axis starting at bottom rs, re = abs(self.nrows - rs), abs(self.nrows - re) x = cs * cwidth y = re * rheight h = (rs - re) * rheight w = (ce - cs) * cwidth frames.append(Frame(x, y, w, h, id=alias, **fargs)) pt = PageTemplate(frames=frames) pt.id = self.template_id return pt
def __init__(self, id='basic', columns=1, pagesize=A4, leftMargin=(2*cm), bottomMargin=(2.1*cm), colmargin=(0.5*cm), background=None): (right, top) = pagesize right -= leftMargin top -= bottomMargin height = top - bottomMargin width = (right - leftMargin) # Subtract out blank space between columns colwidth = (width - ((columns - 1) * colmargin)) / columns frames = [] for col in range(columns): left = leftMargin + (col * (colwidth + colmargin)) frames.append(Frame(left, bottomMargin, colwidth, height)) self.background = background PageTemplate.__init__(self, id=id, frames=frames, pagesize=pagesize)
def __init__(self, id, pageSize=defaultPageSize): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] border = 0.3*cm miniwidth = (self.pageWidth / 2) - 2*border miniheight = (self.pageHeight / 2) - 2*border #self.f4 = Frame(border, border, miniwidth, miniheight, id='p4') #self.f1 = Frame(self.pageWidth/2 + border, border, miniwidth, miniheight, id='p1') #self.f2 = Frame(self.pageWidth/2 + border, self.pageHeight/2 + border, miniwidth, miniheight, id='p2') #self.f3 = Frame(border, self.pageHeight/2 + border, miniwidth, miniheight, id='p3') self.f4 = Frame(0, 0, miniwidth, miniheight, id='p4') self.f1 = Frame(miniwidth, 0, miniwidth, miniheight, id='p1') self.f2 = Frame(miniwidth, miniheight, miniwidth, miniheight, id='p2') self.f3 = Frame(0, miniheight, miniwidth, miniheight, id='p3') PageTemplate.__init__(self, id, [self.f1, self.f2, self.f3, self.f4])
def __init__(self, id, pageSize=defaultPageSize): self.pageWidth = pageSize[0] self.pageHeight = pageSize[1] border = 1.0*cm miniwidth = (self.pageWidth / 2) - 2*border miniheight = (self.pageHeight / 2) - 2*border # with border, in booklet order (but not rotated) #self.f4 = Frame(border, border, miniwidth, miniheight, id='p4') #self.f1 = Frame(self.pageWidth/2 + border, border, miniwidth, miniheight, id='p1') #self.f2 = Frame(self.pageWidth/2 + border, self.pageHeight/2 + border, miniwidth, miniheight, id='p2') #self.f3 = Frame(border, self.pageHeight/2 + border, miniwidth, miniheight, id='p3') self.bl = Frame(border, border, miniwidth, miniheight, id='BL') self.br = Frame(self.pageWidth/2 + border, border, miniwidth, miniheight, id='BR') self.ur = Frame(self.pageWidth/2 + border, self.pageHeight/2 + border, miniwidth, miniheight, id='UR') self.ul = Frame(border, self.pageHeight/2 + border, miniwidth, miniheight, id='UL') PageTemplate.__init__(self, id, [self.ul, self.ur, self.bl, self.br])
def __init__(self, order=None): frames = [Frame(MARGIN, MARGIN, PAGE_WIDTH - (2 * MARGIN), PAGE_HEIGHT - (2 * MARGIN) - (8.5 * cm))] self.order = order PageTemplate.__init__(self, id='first', frames=frames)