from reportlab.platypus.paragraph import Paragraph from reportlab.platypus.tables import Table from reportlab.platypus.doctemplate import * from reportlab.platypus import Flowable, PageBreak, Image from reportlab.lib.units import inch, cm from reportlab.lib.pagesizes import A4 from reportlab.lib import styles, colors # This very basic template is used in all my PDF pages... class MyPDFTemplate (PageTemplate): def __init__ (self, parent): self.parent = parent self.page = 0 content = Frame (1*cm, 1*cm, parent.document.pagesize[0] - 2*cm, parent.document.pagesize[1] - 2*cm) PageTemplate.__init__ (self, "Body", [content]) # # Class MyPDFFile # This class generates the PDF output # class MyPDFFile: def __init__ (self, context): self.context = context self.built = 0 self.objects = [] self.report = cStringIO.StringIO () self.document = BaseDocTemplate (self.report, pagesize=A4) self.document.addPageTemplates (MyPDFTemplate (self)) self.StyleSheet = styles.getSampleStyleSheet () # this environment variable si used to access images stored on disk, but images could be loaded directly from # the ZODB via the "context" parameter... image = Image ('%s/if_logo-pdf.png' % (os.getenv ('IF_IMAGES_ROOT_DIR') or '/var/local/zope'), 3.8*cm, 1.4*cm) first = 1 # getItems is a method of my Zope class which may retrieve anything interesting... for item in context.getItems(): if not first: self.append (PageBreak ()) first = 0 style = [ ('FONTNAME', (0,0), (-1,-1), "Helvetica-Bold"), ('FONTSIZE', (0,0), (-1,-1), 10), ('BOX', (1,0), (-1,-1), 0.25, colors.black), ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), ('ALIGN', (0,0), (-1,-1), "CENTER"), ('VALIGN', (0,0), (-1,-1), "MIDDLE") ] data = [] data.append ( [ image, 'Réseau Achats\nFiche fournisseur', 'N° %d' % item.no ] ) self.append (Table (data, colWidths=[5*cm, 11*cm, 3*cm], style=style)) self.append (Spacer (0, 1*cm)) style = [ ('FONTNAME', (0,0), (-1,-1), "Helvetica"), ('FONTNAME', (0,0), (0,-1), "Helvetica-Bold"), ('FONTSIZE', (0,0), (-1,-1), 9), ('ALIGN', (0,0), (0,-1), "RIGHT"), ('ALIGN', (1,0), (1,-1), "LEFT"), ('VALIGN', (0,0), (-1,-1), "TOP") ] data = [] if item.date_saisie: data.append ( [ "Date de création :", item.date_saisie.strftime('%d/%m/%Y') ] ) if item.date_modif: data.append ( [ "Date de modification :", item.date_modif.strftime('%d/%m/%Y') ] ) data.append ( [ "Nom :", "%s (%s)" % (item.name, item.code) ] ) data.append ( [ "SIRET :", item.siret ] ) data.append ( [ "Code APE :", item.ape ] ) data.append ( [ "Téléphone :", item.telephone ] ) data.append ( [ "Fax :", item.fax ] ) data.append ( [ "Adresse mél :", item.email ] ) self.append (Table (data, style=style)) self.document.build (self.objects) self.built = 1 def __str__ (self): if self.built: return self.report.getvalue () else: return None def append (self, object): self.objects.append (object) # Here starts my Zope product... class MyZopeClass (...): def __init__ (...): ... ... _security.declareProtected ("Load PDF file", 'get_pdf') def get_pdf (self, REQUEST=None): """ Generate a PDF file """ result = str (MyPDFFile (self)) if REQUEST is not None: REQUEST.RESPONSE.setHeader ('Content-Type', 'application/pdf') return result