def __init__(self, context, filename) : # save some datas self.context = context self.built = 0 self.objects = [] # we will build an in-memory document # instead of creating an on-disk file. self.report = getBytesIO() # initialise a PDF document using ReportLab's platypus self.document = BaseDocTemplate(self.report) # add our page template # (we could add more than one, but I prefer to keep it simple) self.document.addPageTemplates(self.MyPageTemplate(self)) # get the default style sheets self.StyleSheet = styles.getSampleStyleSheet() # then build a simple doc with ReportLab's platypus sometext = "A sample script to show how to use ReportLab from within Zope" url = self.escapexml(context.absolute_url()) urlfilename = self.escapexml(context.absolute_url() + '/%s' % filename) self.append(Paragraph("Using ReportLab from within Zope", self.StyleSheet["Heading3"])) self.append(Spacer(0, 10)) self.append(Paragraph("You launched it from : %s" % url, self.StyleSheet['Normal'])) self.append(Spacer(0, 40)) self.append(Paragraph("If possible, this report will be automatically saved as : %s" % urlfilename, self.StyleSheet['Normal'])) # generation du document PDF self.document.build(self.objects) self.built = 1
def rlzope(self): """A sample external method to show people how to use ReportLab from within Zope.""" try: # # which file/object name to use ? # append ?name=xxxxx to rlzope's url to # choose another name filename = self.REQUEST.get("name", "dummy.pdf") if filename[-4:] != '.pdf': filename = filename + '.pdf' # tell the browser we send some PDF document # with the requested filename # get the document's content itself as a string of text content = str(MyPDFDoc(self, filename)) # we will return it to the browser, but before that we also want to # save it into the ZODB into the current folder try: self.manage_addFile( id=filename, file=content, title="A sample PDF document produced with ReportLab", precondition='', content_type="application/pdf") except: # it seems an object with this name already exists in the ZODB: # it's more secure to not replace it, since we could possibly # destroy an important PDF document of this name. pass self.REQUEST.RESPONSE.setHeader('Content-Type', 'application/pdf') self.REQUEST.RESPONSE.setHeader('Content-Disposition', 'attachment; filename=%s' % filename) except: import traceback, sys, cgi content = sys.stdout = sys.stderr = getBytesIO() self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html') traceback.print_exc() sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ content = '<html><head></head><body><pre>%s</pre></body></html>' % cgi.escape( content.getvalue()) # then we also return the PDF content to the browser return content
def getImageFromZODB(self, name): """Retrieves an Image from the ZODB, converts it to PIL, and makes it 0.75 inch high. """ try: # try to get it from ZODB logo = getattr(self.parent.context, name) except AttributeError: # not found ! return None # Convert it to PIL image = ImageReader(getBytesIO(str(logo.data))) (width, height) = image.getSize() # scale it to be 0.75 inch high multi = ((height + 0.0) / (0.75 * inch)) width = int(width / multi) height = int(height / multi) return ((width, height), image)
def getImageFromZODB(self, name) : """Retrieves an Image from the ZODB, converts it to PIL, and makes it 0.75 inch high. """ try : # try to get it from ZODB logo = getattr(self.parent.context, name) except AttributeError : # not found ! return None # Convert it to PIL image = ImageReader(getBytesIO(str(logo.data))) (width, height) = image.getSize() # scale it to be 0.75 inch high multi = ((height + 0.0) / (0.75 * inch)) width = int(width / multi) height = int(height / multi) return ((width, height), image)
def __init__(self, context, filename): # save some datas self.context = context self.built = 0 self.objects = [] # we will build an in-memory document # instead of creating an on-disk file. self.report = getBytesIO() # initialise a PDF document using ReportLab's platypus self.document = BaseDocTemplate(self.report) # add our page template # (we could add more than one, but I prefer to keep it simple) self.document.addPageTemplates(self.MyPageTemplate(self)) # get the default style sheets self.StyleSheet = styles.getSampleStyleSheet() # then build a simple doc with ReportLab's platypus sometext = "A sample script to show how to use ReportLab from within Zope" url = self.escapexml(context.absolute_url()) urlfilename = self.escapexml(context.absolute_url() + '/%s' % filename) self.append( Paragraph("Using ReportLab from within Zope", self.StyleSheet["Heading3"])) self.append(Spacer(0, 10)) self.append( Paragraph("You launched it from : %s" % url, self.StyleSheet['Normal'])) self.append(Spacer(0, 40)) self.append( Paragraph( "If possible, this report will be automatically saved as : %s" % urlfilename, self.StyleSheet['Normal'])) # generation du document PDF self.document.build(self.objects) self.built = 1
def rlzope(self) : """A sample external method to show people how to use ReportLab from within Zope.""" try: # # which file/object name to use ? # append ?name=xxxxx to rlzope's url to # choose another name filename = self.REQUEST.get("name", "dummy.pdf") if filename[-4:] != '.pdf' : filename = filename + '.pdf' # tell the browser we send some PDF document # with the requested filename # get the document's content itself as a string of text content = str(MyPDFDoc(self, filename)) # we will return it to the browser, but before that we also want to # save it into the ZODB into the current folder try : self.manage_addFile(id = filename, file = content, title = "A sample PDF document produced with ReportLab", precondition = '', content_type = "application/pdf") except : # it seems an object with this name already exists in the ZODB: # it's more secure to not replace it, since we could possibly # destroy an important PDF document of this name. pass self.REQUEST.RESPONSE.setHeader('Content-Type', 'application/pdf') self.REQUEST.RESPONSE.setHeader('Content-Disposition', 'attachment; filename=%s' % filename) except: import traceback, sys, cgi content = sys.stdout = sys.stderr = getBytesIO() self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html') traceback.print_exc() sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ content = '<html><head></head><body><pre>%s</pre></body></html>' % cgi.escape(content.getvalue()) # then we also return the PDF content to the browser return content