def testAutoPageTemplate(self): from reportlab.platypus import BaseDocTemplate, PageTemplate, Frame, PageBegin from reportlab.lib.units import inch class onPage: def __init__(self,label): self.label = label def __call__(self,canv,doc): canv.drawString(72,72,'This is pageTemplate(%s)' % (self.label,)) class MyDocTemplate(BaseDocTemplate): _invalidInitArgs = ('pageTemplates',) def __init__(self, filename, **kw): self.allowSplitting = 0 BaseDocTemplate.__init__(self, filename, **kw) self.addPageTemplates( [ PageTemplate('normal', [Frame(inch, inch, 6.27*inch, 9.69*inch, id='first',topPadding=0,rightPadding=0,leftPadding=0,bottomPadding=0,showBoundary=ShowBoundaryValue(color="red"))], onPage = onPage('normal'), ), PageTemplate('auto', [Frame(inch, inch, 6.27*inch, 9.69*inch, id='first',topPadding=0,rightPadding=0,leftPadding=0,bottomPadding=0,showBoundary=ShowBoundaryValue(color="red"))], onPage = onPage('auto'), autoNextPageTemplate = 'autoFollow', ), PageTemplate('autoFollow', [Frame(inch, inch, 6.27*inch, 9.69*inch, id='first',topPadding=0,rightPadding=0,leftPadding=0,bottomPadding=0,showBoundary=ShowBoundaryValue(color="red"))], onPage = onPage('autoFollow'), ), ]) styleSheet = getSampleStyleSheet() normal = ParagraphStyle(name='normal',fontName='Times-Roman',fontSize=12,leading=1.2*12,parent=styleSheet['Normal']) story =[] a = story.append a(Paragraph('should be on page template normal', normal)) a(NextPageTemplate('auto')) a(PageBreak()) a(Paragraph('should be on page template auto', normal)) a(PageBreak()) a(DocAssert('doc.pageTemplate.id=="autoFollow"','expected doc.pageTemplate.id=="autoFollow"')) a(Paragraph('should be on page template autoFollow 1', normal)) a(PageBreak()) a(Paragraph('should be on page template autoFollow 2', normal)) doc = MyDocTemplate(outputfile('test_platypus_paragraphs_AutoNextPageTemplate.pdf')) doc.build(story)
def set_toc(self): # Create an instance of TableOfContents. Override the level styles (optional) # and add the object to the story toc = TableOfContents() toc.levelStyles = [ PS(fontSize=12, name='TOCHeading1', leftIndent=0, firstLineIndent=-20, spaceBefore=0, leading=10), PS(fontSize=11, name='TOCHeading2', leftIndent=10, firstLineIndent=-20, spaceBefore=0, leading=10), PS(fontSize=10, name='TOCHeading3', leftIndent=20, firstLineIndent=-20, spaceBefore=0, leading=10), PS(fontSize=9, name='TOCHeading4', leftIndent=30, firstLineIndent=-20, spaceBefore=0, leading=10), PS(fontSize=8, name='TOCHeading5', leftIndent=40, firstLineIndent=-20, spaceBefore=0, leading=10), ] self.elements.append(Paragraph(self.title, self.style['centered'])) #self.elements.append(Paragraph(_("Organization: <b>%s</b>") % self.org.name, self.style[1])) self.elements.append(toc) self.elements.append(PageBreak())
def makeReport(self): style = self.getStyle() story = [] story.append(self.doExport()) story.append(self.doImport()) story.append(self.doSubscriptions()) story.append(PageBreak()) story.append(self.doOutstanding()) story.append(self.doErrors()) self.pdf.build(story, onFirstPage=self.myFirstPage, onLaterPages=self.myLaterPages)
def test1(self): "This makes one long multi-page paragraph." # Build story. story = [] styleSheet = getSampleStyleSheet() h3 = styleSheet['Heading3'] bt = styleSheet['BodyText'] text = '''If you imagine that the box of X's tothe left is an image, what I want to be able to do is flow a series of paragraphs around the image so that once the bottom of the image is reached, then text will flow back to the left margin. I know that it would be possible to something like this using tables, but I can't see how to have a generic solution. There are two examples of this in the demonstration section of the reportlab site. If you look at the "minimal" euro python conference brochure, at the end of the timetable section (page 8), there are adverts for "AdSu" and "O'Reilly". I can see how the AdSu one might be done generically, but the O'Reilly, unsure... I guess I'm hoping that I've missed something, and that it's actually easy to do using platypus.We can do greek letters <greek>mDngG</greek>. This should be a u with a dieresis on top <unichar code=0xfc/>="<unichar code="0xfc"/>" and this &#xfc;="ü" and this \\xc3\\xbc="\xc3\xbc". On the other hand this should be a pound sign &pound;="£" and this an alpha &alpha;="α". You can have links in the page <link href="http://www.reportlab.com" color="blue">ReportLab</link> & <a href="http://www.reportlab.org" color="green">ReportLab.org</a>. Use scheme "pdf:" to indicate an external PDF link, "http:", "https:" to indicate an external link eg something to open in your browser. If an internal link begins with something that looks like a scheme, precede with "document:". <strike>This text should have a strike through it.</strike> ''' from reportlab.platypus.flowables import ImageAndFlowables, Image from reportlab.lib.testutils import testsFolder gif = os.path.join(testsFolder,'pythonpowered.gif') heading = Paragraph('This is a heading',h3) story.append(ImageAndFlowables(Image(gif),[heading,Paragraph(text,bt)])) phrase = 'This should be a paragraph spanning at least three pages. ' description = ''.join([('%d: '%i)+phrase for i in xrange(250)]) story.append(ImageAndFlowables(Image(gif),[heading,Paragraph(description, bt)],imageSide='left')) story.append(NextPageTemplate('special')) story.append(PageBreak()) VERA = ('Vera','VeraBd','VeraIt','VeraBI') for v in VERA: registerFont(TTFont(v,v+'.ttf')) registerFontFamily(*(VERA[:1]+VERA)) story.append(ImageAndFlowables( Image(gif,width=280,height=120), Paragraph('''<font name="Vera">The <b>concept</b> of an <i>integrated</i> one <b><i>box</i></b> solution for <i><b>advanced</b></i> voice and data applications began with the introduction of the IMACS. The IMACS 200 carries on that tradition with an integrated solution optimized for smaller port size applications that the IMACS could not economically address. An array of the most popular interfaces and features from the IMACS has been bundled into a small 2U chassis providing the ultimate in ease of installation.</font>''', style=ParagraphStyle( name="base", fontName="Helvetica", leading=12, leftIndent=0, firstLineIndent=0, spaceBefore = 9.5, fontSize=9.5, ) ), imageSide='left', ) ) story.append(ImageAndFlowables( Image(gif,width=240,height=120), Paragraph('''The concept of an integrated one box solution for advanced voice and data applications began with the introduction of the IMACS. The IMACS 200 carries on that tradition with an integrated solution optimized for smaller port size applications that the IMACS could not economically address. An array of the most popular interfaces and features from the IMACS has been bundled into a small 2U chassis providing the ultimate in ease of installation.''', style=ParagraphStyle( name="base", fontName="Helvetica", leading=12, leftIndent=0, firstLineIndent=0, spaceBefore = 9.5, fontSize=9.5, ) ), imageSide='left', ) ) story.append(PageBreak()) story.append(Paragraph('Image larger than the frame',h3)) story.append(ImageAndFlowables( Image(gif,width=6*110,height=6*44), Paragraph('''The concept of an integrated one box solution for advanced voice and data applications began with the introduction of the IMACS. The IMACS 200 carries on that tradition with an integrated solution optimized for smaller port size applications that the IMACS could not economically address. An array of the most popular interfaces and features from the IMACS has been bundled into a small 2U chassis providing the ultimate in ease of installation.''', style=ParagraphStyle( name="base", fontName="Helvetica", leading=12, leftIndent=0, firstLineIndent=0, spaceBefore = 9.5, fontSize=9.5, ) ), imageSide='left', ) ) doc = MyDocTemplate(outputfile('test_platypus_imageandflowables.pdf'),showBoundary=1) doc.multiBuild(story)
def set_page_break(self): self.elements.append(PageBreak())
#tutorial 50 #CREATE PORTRAIT & LANDSCAPE ORIENTATION TOGETHER IN SAME PDF from reportlab.platypus.doctemplate import SimpleDocTemplate, PageTemplate, PageBreak from reportlab.pdfgen import canvas from reportlab.platypus import Paragraph, SimpleDocTemplate, PageTemplate, NextPageTemplate, Frame from reportlab.lib.styles import getSampleStyleSheet pdf = SimpleDocTemplate("tutorial50.pdf") frame1 = Frame(10, 20, 600, 1000, showBoundary=1) frame2 = Frame(10, 20, 1000, 700, showBoundary=1) port = PageTemplate(id="p", pagesize=[800, 1200], frames=[frame1]) land = PageTemplate(id="l", pagesize=[1200, 900], frames=[frame2]) pdf.addPageTemplates([port, land]) text = "text" flow_obj = [] styles = getSampleStyleSheet() ptext = Paragraph(text, style=styles["Normal"]) flow_obj.append(ptext) flow_obj.append(NextPageTemplate("l")) flow_obj.append(PageBreak()) flow_obj.append(ptext) flow_obj.append(NextPageTemplate("p")) flow_obj.append(PageBreak()) flow_obj.append(ptext) pdf.build(flow_obj)
def testUl(self): from reportlab.platypus import BaseDocTemplate, PageTemplate, Frame, PageBegin from reportlab.lib.units import inch class MyDocTemplate(BaseDocTemplate): _invalidInitArgs = ('pageTemplates', ) def __init__(self, filename, **kw): self.allowSplitting = 0 BaseDocTemplate.__init__(self, filename, **kw) self.addPageTemplates([ PageTemplate( 'normal', [ Frame(inch, inch, 6.27 * inch, 9.69 * inch, id='first', topPadding=0, rightPadding=0, leftPadding=0, bottomPadding=0, showBoundary=ShowBoundaryValue(color="red")) ], ), ]) styleSheet = getSampleStyleSheet() normal = ParagraphStyle(name='normal', fontName='Times-Roman', fontSize=12, leading=1.2 * 12, parent=styleSheet['Normal']) normal_just = ParagraphStyle(name='normal_just', parent=normal, alignment=TA_JUSTIFY, spaceAfter=12) text0 = '''Furthermore, a subset of English sentences interesting on quite independent grounds is not quite equivalent to a stipulation to place the constructions into these various categories. We will bring evidence in favor of The following thesis: most of the methodological work in modern linguistics can be defined in such a way as to impose problems of phonemic and morphological analysis.''' story = [] a = story.append for mode in (0, 1, 2, 3, 4, 5, 6, 7): text = text0 paraStyle = normal_just if mode == 1: text = text.replace('English sentences', '<b>English sentences</b>').replace( 'quite equivalent', '<i>quite equivalent</i>') text = text.replace('the methodological work', '<b>the methodological work</b>').replace( 'to impose problems', '<i>to impose problems</i>') a( Paragraph('Justified paragraph in normal/bold/italic font', style=normal)) elif mode == 2: text = '<b>%s</b>' % text a(Paragraph('Justified paragraph in bold font', style=normal)) elif mode == 3: text = '<i>%s</i>' % text a(Paragraph('Justified paragraph in italic font', style=normal)) elif mode == 4: text = text.replace('English ', 'English ').replace( 'quite ', 'quite ') text = text.replace(' methodological', ' methodological').replace( ' impose', ' impose') a( Paragraph( 'Justified paragraph in normal font & some hard spaces', style=normal)) elif mode in (5, 6, 7): text = text.replace('as to impose', '<br/>as to impose').replace( ' most of the', '<br/>most of the') text = text.replace(' grounds', '<br/>grounds').replace( ' various', '<br/>various') if mode in (6, 7): msg = [] msg.append('justifyBreaks=1') paraStyle = paraStyle.clone('paraStyle6', paraStyle, justifyBreaks=1) if mode == 7: msg.append('justifyLastLine=3') paraStyle = paraStyle.clone('paraStyle7', paraStyle, justifyLastLine=3) msg = '(%s) ' % (' '.join(msg)) else: a(PageBreak()) msg = ' ' a( Paragraph('Justified%swith some <br/> tags' % msg, style=normal)) else: a(Paragraph('Justified paragraph in normal font', style=normal)) a(Paragraph(text, style=paraStyle)) doc = MyDocTemplate(outputfile('test_platypus_paragraphs_just.pdf')) doc.build(story)