def story_title(survey, info=dict()): story = [ platypus.Paragraph(str(line), stylesheet['Title']) for line in survey.title.split('\n') ] story += [ platypus.FrameBreak(), ] keys = list(survey.info.keys()) if keys: keys.sort() table = [[ platypus.Paragraph(str(key), stylesheet['Normal']), platypus.Paragraph(str(survey.info[key]), stylesheet['Normal']) ] for key in keys] story += [ platypus.Table(table, colWidths=(50 * mm, None)), ] if info: story += [platypus.Spacer(0, 10 * mm)] keys = list(info.keys()) keys.sort() table = [[ platypus.Paragraph(str(key), stylesheet['Normal']), platypus.Paragraph(str(info[key]), stylesheet['Normal']) ] for key in keys] story += [ platypus.Table(table, colWidths=(50 * mm, None)), ] story += [platypus.NextPageTemplate('Normal'), platypus.PageBreak()] return story
def write_foot(self): if not self.multidoc: self.close() # Finish the document if this is all-in-one #self.out.close() else: #self.txt.append(platypus.PageBreak()) # Otherwise, a new page # Append to the txt list we were handed ourselves in a KeepTogether block #self.txt.append(platypus.Spacer(0,inch*0.5)) #if pdf_args.get('mode',('column',1))[0]=='column': # self.master_txt.append(platypus.KeepTogether(self.txt)) #else: if self.master_txt: self.master_txt.append(platypus.FrameBreak()) self.master_txt.extend(self.txt)
def prepareReport(self, session): #this is the report lab set up stuff OutputElements = [] output = platypus.BaseDocTemplate('/tmp/temp.pdf') #A4 page size setup with margins output.pagesize = (8.3 * inch, 11.7 * inch) output.leftMargin = 0.5 * inch output.rightMargin = 0.5 * inch output.topMargin = 0.5 * inch output.bottomMargin = 0.5 * inch output.width = output.pagesize[ 0] - output.leftMargin - output.rightMargin output.height = output.pagesize[ 1] - output.topMargin - output.bottomMargin if self.columns == 1: #set up the columns interFrameMargin = 0.5 * inch frameWidth = output.width / 2 - interFrameMargin / 2 frameHeight = output.height - inch * 0.6 framePadding = 0 * inch # create a frameset called withHeader withHeader = [] # append header settings and 2 columns leftMargin = output.leftMargin titlebar = platypus.Frame(leftMargin, output.height, output.width, 0.75 * inch, leftPadding=0, rightPadding=0, topPadding=0, bottomPadding=0, showBoundary=0) withHeader.append(titlebar) column = platypus.Frame(leftMargin, output.bottomMargin, frameWidth, frameHeight) withHeader.append(column) leftMargin = output.leftMargin + frameWidth + interFrameMargin column = platypus.Frame(leftMargin, output.bottomMargin, frameWidth, frameHeight) withHeader.append(column) #create frameset called withoutHeader withoutHeader = [] #change the frame height because no header here frameHeight = output.height - inch * 0.2 #add two columns leftMargin = output.leftMargin column = platypus.Frame(leftMargin, output.bottomMargin, frameWidth, frameHeight) withoutHeader.append(column) leftMargin = output.leftMargin + frameWidth + interFrameMargin column = platypus.Frame(leftMargin, output.bottomMargin, frameWidth, frameHeight) withoutHeader.append(column) else: #set up the full page stuff frameWidth = output.width frameHeight = output.height - inch * 0.6 framePadding = 0 * inch withHeader = [] #append header and single column (full page) leftMargin = output.leftMargin titlebar = platypus.Frame(leftMargin, output.height, output.width, 0.75 * inch, leftPadding=0, rightPadding=0, topPadding=0, bottomPadding=0, showBoundary=0) withHeader.append(titlebar) column = platypus.Frame(leftMargin, output.bottomMargin, frameWidth, frameHeight) withHeader.append(column) withoutHeader = [] frameHeight = output.height - inch * 0.2 column = platypus.Frame(leftMargin, output.bottomMargin, frameWidth, frameHeight) withoutHeader.append(column) def header(Elements, txt, style=HeaderStyle): s = platypus.Spacer(0.2 * inch, 0.2 * inch) Elements.append(s) style.alignment = 1 style.fontName = self.psfont style.fontSize = 18 style.borderWidth = 0 para = platypus.Paragraph(txt, style) Elements.append(para) headerPage = platypus.PageTemplate(id='Header', frames=withHeader) normalPage = platypus.PageTemplate(id='noHeader', frames=withoutHeader) if self.headerString is not None: output.addPageTemplates([headerPage, normalPage]) header(OutputElements, self.headerString) OutputElements.append(platypus.FrameBreak()) else: output.addPageTemplates([normalPage]) OutputElements.extend(self.getData(session)) #numbered canvas does footer (including page numbers and date is requested) NumberedCanvas.setFooterText(self.footerString) output.build(OutputElements, canvasmaker=NumberedCanvas) output.showBoundary = True fileInput = open('/tmp/temp.pdf', 'r') doc = StringDocument(fileInput.read()) os.remove('/tmp/temp.pdf') yield doc
def _flowable(self, node): if node.localName == 'para': style = self.styles.para_style_get(node) return platypus.Paragraph( self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'}))) elif node.localName == 'name': self.styles.names[node.getAttribute('id')] = node.getAttribute( 'value') return None elif node.localName == 'xpre': style = self.styles.para_style_get(node) return platypus.XPreformatted( self._textual(node), style, **(utils.attr_get(node, [], { 'bulletText': 'str', 'dedent': 'int', 'frags': 'int' }))) elif node.localName == 'pre': style = self.styles.para_style_get(node) return platypus.Preformatted( self._textual(node), style, **(utils.attr_get(node, [], { 'bulletText': 'str', 'dedent': 'int' }))) elif node.localName == 'illustration': return self._illustration(node) elif node.localName == 'blockTable': return KeepTogether(self._table(node)) elif node.localName == 'title': styles = reportlab.lib.styles.getSampleStyleSheet() style = styles['Title'] return platypus.Paragraph( self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'}))) elif node.localName == 'h1': styles = reportlab.lib.styles.getSampleStyleSheet() style = styles['Heading1'] return platypus.Paragraph( self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'}))) elif node.localName == 'h2': styles = reportlab.lib.styles.getSampleStyleSheet() style = styles['Heading2'] return platypus.Paragraph( self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'}))) elif node.localName == 'h3': styles = reportlab.lib.styles.getSampleStyleSheet() style = styles['Heading3'] return platypus.Paragraph( self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'}))) elif node.localName == 'image': return platypus.Image( node.getAttribute('file'), mask=(250, 255, 250, 255, 250, 255), **(utils.attr_get( node, ['width', 'height', 'preserveAspectRatio', 'anchor']))) elif node.localName == 'spacer': if node.hasAttribute('width'): width = utils.unit_get(node.getAttribute('width')) else: width = utils.unit_get('1cm') length = utils.unit_get(node.getAttribute('length')) return platypus.Spacer(width=width, height=length) elif node.localName == 'barCode': return code39.Extended39(self._textual(node)) elif node.localName == 'pageBreak': return platypus.PageBreak() elif node.localName == 'condPageBreak': return platypus.CondPageBreak(**(utils.attr_get(node, ['height']))) elif node.localName == 'setNextTemplate': return platypus.NextPageTemplate(str(node.getAttribute('name'))) elif node.localName == 'nextFrame': return platypus.FrameBreak() # return platypus.CondPageBreak(1000) # TODO: change the 1000 ! elif node.localName == 'ul': return self._list(node) elif node.localName == 'keepInFrame': substory = self.render(node) kwargs = { "maxWidth": 0, "maxHeight": 0, "content": substory, } mode = node.getAttribute("onOverflow") if mode: kwargs["mode"] = mode name = node.getAttribute("id") if name: kwargs["name"] = name kwargs.update( utils.attr_get(node, ['maxWidth', 'maxHeight', 'mergeSpace'], { 'maxWidth': 'int', 'maxHeight': 'int' })) return platypus.KeepInFrame(**kwargs) else: sys.stderr.write('Warning: flowable not yet implemented: %s !\n' % (node.localName, )) return None