def jpg_imagedata(self): #directly process JPEG files #open file, needs some error handling!! self.source = 'JPEG' imageFile = open(self.image, 'rb') info = pdfutils.readJPEGInfo(imageFile) imgwidth, imgheight = info[0], info[1] if info[2] == 1: colorSpace = 'DeviceGray' elif info[2] == 3: colorSpace = 'DeviceRGB' else: #maybe should generate an error, is this right for CMYK? colorSpace = 'DeviceCMYK' imageFile.seek(0) #reset file pointer imagedata = [] #imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /DCTDecode] ID' % (info[0], info[1], colorSpace)) imagedata.append('BI /W %d /H %d /BPC 8 /CS /%s /F [/A85 /DCT] ID' % (imgwidth, imgheight, colorSpace)) #write in blocks of (??) 60 characters per line to a list compressed = imageFile.read() encoded = pdfutils._AsciiBase85Encode(compressed) outstream = getStringIO(encoded) dataline = outstream.read(60) while dataline <> "": imagedata.append(dataline) self.binaryData.append(dataline) dataline = outstream.read(60) imagedata.append('EI') return (imagedata, imgwidth, imgheight)
def pygments2xpre(s, language="python"): "Return markup suitable for XPreformatted" try: from pygments import highlight from pygments.formatters import HtmlFormatter except ImportError: return s from pygments.lexers import get_lexer_by_name rconv = lambda x: x if isPy3: out = getStringIO() else: if isUnicode(s): s = asBytes(s) rconv = asUnicode out = getBytesIO() l = get_lexer_by_name(language) h = HtmlFormatter() highlight(s,l,h,out) styles = [(cls, style.split(';')[0].split(':')[1].strip()) for cls, (style, ttype, level) in h.class2style.items() if cls and style and style.startswith('color:')] return rconv(_2xpre(out.getvalue(),styles))
def saveAsHandout(self): """Write the PDF document, multiple slides per page.""" styleSheet = getSampleStyleSheet() h1 = styleSheet['Heading1'] bt = styleSheet['BodyText'] if self.sourceFilename: filename = os.path.splitext(self.sourceFilename)[0] + '.pdf' outfile = getStringIO() doc = SimpleDocTemplate(outfile, pagesize=rl_config.defaultPageSize, showBoundary=0) doc.leftMargin = 1 * cm doc.rightMargin = 1 * cm doc.topMargin = 2 * cm doc.bottomMargin = 2 * cm multiPageWidth = rl_config.defaultPageSize[ 0] - doc.leftMargin - doc.rightMargin - 50 story = [] orgFullPageSize = (self.pageWidth, self.pageHeight) t = makeSlideTable(self.slides, orgFullPageSize, multiPageWidth, self.cols) story.append(t) ## #ensure outline visible by default ## if self.showOutline: ## doc.canv.showOutline() doc.build(story) return self.savetofile(outfile, filename)
def _showWidgetProperties(self, widget): """Dump all properties of a widget.""" props = widget.getProperties() keys = props.keys() keys.sort() lines = [] for key in keys: value = props[key] f = getStringIO() pprint.pprint(value, f) value = f.getvalue()[:-1] valueLines = string.split(value, '\n') for i in range(1, len(valueLines)): valueLines[i] = ' ' * (len(key) + 3) + valueLines[i] value = string.join(valueLines, '\n') lines.append('%s = %s' % (key, value)) text = join(lines, '\n') self.story.append( Paragraph("<i>Properties of Example Widget</i>", self.bt)) self.story.append(Paragraph("", self.bt)) self.story.append(Preformatted(text, self.code))
def _drawImageLevel1(self, image, x1, y1, width=None, height=None): # Postscript Level1 version available for fallback mode when Level2 doesn't work # For now let's start with 24 bit RGB images (following piddlePDF again) component_depth = 8 myimage = image.convert('RGB') imgwidth, imgheight = myimage.size if not width: width = imgwidth if not height: height = imgheight #print 'Image size (%d, %d); Draw size (%d, %d)' % (imgwidth, imgheight, width, height) # now I need to tell postscript how big image is # "image operators assume that they receive sample data from # their data source in x-axis major index order. The coordinate # of the lower-left corner of the first sample is (0,0), of the # second (1,0) and so on" -PS2 ref manual p. 215 # # The ImageMatrix maps unit squre of user space to boundary of the source image # # The CurrentTransformationMatrix (CTM) maps the unit square of # user space to the rect...on the page that is to receive the # image. A common ImageMatrix is [width 0 0 -height 0 height] # (for a left to right, top to bottom image ) # first let's map the user coordinates start at offset x1,y1 on page self.code.extend([ 'gsave', '%s %s translate' % (x1,y1), # need to start are lower left of image '%s %s scale' % (width,height), '/scanline %d 3 mul string def' % imgwidth # scanline by multiples of image width ]) # now push the dimensions and depth info onto the stack # and push the ImageMatrix to map the source to the target rectangle (see above) # finally specify source (PS2 pp. 225 ) and by exmample self.code.extend([ '%s %s %s' % (imgwidth, imgheight, component_depth), '[%s %s %s %s %s %s]' % (imgwidth, 0, 0, -imgheight, 0, imgheight), '{ currentfile scanline readhexstring pop } false 3', 'colorimage ' ]) # data source output--now we just need to deliver a hex encode # series of lines of the right overall size can follow # piddlePDF again rawimage = (myimage.tobytes if hasattr(myimage,'tobytes') else myimage.tostring)() hex_encoded = self._AsciiHexEncode(rawimage) # write in blocks of 78 chars per line outstream = getStringIO(hex_encoded) dataline = outstream.read(78) while dataline != "": self.code_append(dataline) dataline= outstream.read(78) self.code_append('% end of image data') # for clarity self.code_append('grestore') # return coordinates to normal
def saveAsHandout(self): """Write the PDF document, multiple slides per page.""" styleSheet = getSampleStyleSheet() h1 = styleSheet["Heading1"] bt = styleSheet["BodyText"] if self.sourceFilename: filename = os.path.splitext(self.sourceFilename)[0] + ".pdf" outfile = getStringIO() doc = SimpleDocTemplate(outfile, pagesize=rl_config.defaultPageSize, showBoundary=0) doc.leftMargin = 1 * cm doc.rightMargin = 1 * cm doc.topMargin = 2 * cm doc.bottomMargin = 2 * cm multiPageWidth = rl_config.defaultPageSize[0] - doc.leftMargin - doc.rightMargin - 50 story = [] orgFullPageSize = (self.pageWidth, self.pageHeight) t = makeSlideTable(self.slides, orgFullPageSize, multiPageWidth, self.cols) story.append(t) ## #ensure outline visible by default ## if self.showOutline: ## doc.canv.showOutline() doc.build(story) return self.savetofile(outfile, filename)
def handleError(name, fmt): msg = 'Problem drawing %s fmt=%s file' % (name, fmt) if shout or verbose > 2: print(msg) errs.append('<br/><h2 style="color:red">%s</h2>' % msg) buf = getStringIO() traceback.print_exc(file=buf) errs.append('<pre>%s</pre>' % escape(buf.getvalue()))
def get_templated_HTML( src_template=DUMB_FMT_HTML, title=None, heading=None, body=None): from reportlab.lib.utils import getStringIO p = DocStyle0HTML() D = p.processfile( getStringIO(body), None ) if title is not None: D['title'] = title if heading is not None: D['heading'] = '<center><h1>%s</h1></center>' % heading return get_templated_pagedata( find_template(TEMPLATE_FN), src_template % D)
def handleError(name,fmt): msg = 'Problem drawing %s fmt=%s file'%(name,fmt) if shout or verbose>2: print(msg) errs.append('<br/><h2 style="color:red">%s</h2>' % msg) buf = getStringIO() traceback.print_exc(file=buf) errs.append('<pre>%s</pre>' % escape(buf.getvalue()))
def PIL_imagedata(self): self.source = 'PIL' zlib = import_zlib() if not zlib: return image = self.image myimage = image.convert('RGB') imgwidth, imgheight = myimage.size # this describes what is in the image itself # *NB* according to the spec you can only use the short form in inline images #imagedata=['BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /FlateDecode] ID]' % (imgwidth, imgheight,'RGB')] imagedata=['BI /W %d /H %d /BPC 8 /CS /RGB /F [/A85 /Fl] ID' % (imgwidth, imgheight)] #use a flate filter and Ascii Base 85 to compress raw = myimage.tostring() assert(len(raw) == imgwidth * imgheight, "Wrong amount of data for image") compressed = zlib.compress(raw) #this bit is very fast... encoded = pdfutils._AsciiBase85Encode(compressed) #...sadly this isn't #write in blocks of (??) 60 characters per line to a list outstream = getStringIO(encoded) dataline = outstream.read(60) while dataline <> "": imagedata.append(dataline) self.binaryData.append(dataline) dataline = outstream.read(60) imagedata.append('EI') return (imagedata, imgwidth, imgheight)
def _drawImageLevel2(self, image, x1,y1, width=None,height=None): # Postscript Level2 version '''At present we're handling only PIL''' ### what sort of image are we to draw if image.mode=='L' : imBitsPerComponent = 8 imNumComponents = 1 myimage = image elif image.mode == '1': myimage = image.convert('L') imNumComponents = 1 myimage = image else : myimage = image.convert('RGB') imNumComponents = 3 imBitsPerComponent = 8 imwidth, imheight = myimage.size if not width: width = imwidth if not height: height = imheight self.code.extend([ 'gsave', '%s %s translate' % (x1,y1), # need to start are lower left of image '%s %s scale' % (width,height)]) if imNumComponents == 3 : self.code_append('/DeviceRGB setcolorspace') elif imNumComponents == 1 : self.code_append('/DeviceGray setcolorspace') # create the image dictionary self.code_append(""" << /ImageType 1 /Width %d /Height %d %% dimensions of source image /BitsPerComponent %d""" % (imwidth, imheight, imBitsPerComponent) ) if imNumComponents == 1: self.code_append('/Decode [0 1]') if imNumComponents == 3: self.code_append('/Decode [0 1 0 1 0 1] %% decode color values normally') self.code.extend([ '/ImageMatrix [%s 0 0 %s 0 %s]' % (imwidth, -imheight, imheight), '/DataSource currentfile /ASCIIHexDecode filter', '>> % End image dictionary', 'image']) # after image operator just need to dump image dat to file as hexstring rawimage = (myimage.tobytes if hasattr(myimage,'tobytes') else myimage.tostring)() hex_encoded = self._AsciiHexEncode(rawimage) # write in blocks of 78 chars per line outstream = getStringIO(hex_encoded) dataline = outstream.read(78) while dataline != "": self.code_append(dataline) dataline= outstream.read(78) self.code_append('> % end of image data') # > is EOD for hex encoded filterfor clarity self.code_append('grestore') # return coordinates to normal
def drawToString(d, fmt='GIF', dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_): s = getStringIO() drawToFile(d, s, fmt=fmt, dpi=dpi, bg=bg, configPIL=configPIL) return s.getvalue()
def _AsciiHexEncode(input): """Encodes input using ASCII-Hex coding. This is a verbose encoding used for binary data within a PDF file. One byte binary becomes two bytes of ASCII. Helper function used by images.""" output = getStringIO() for char in input: output.write('%02x' % ord(char)) output.write('>') return output.getvalue()
def _svn(self,args,fail=1): ''' do a svn command and return the results ''' svn = find_exe('svn') if type(args) is type([]): args = ' '.join(args) self.goWorkingDir() fout=getStringIO() do_exec(svn + ' ' + args,fout=fout,sys_exit=0,verbose=self.verbose>1,fail=fail) self.resetDir() T = fout.getvalue() T.replace('\r\n','\n') T.replace('\r','\n') return '\n'.join([x for x in T.split('\n') if not x or x[0]!='?'])
def saveAsPresentation(self): """Write the PDF document, one slide per page.""" if self.verbose: print 'saving presentation...' pageSize = (self.pageWidth, self.pageHeight) if self.sourceFilename: filename = os.path.splitext(self.sourceFilename)[0] + '.pdf' if self.outDir: filename = os.path.join(self.outDir, os.path.basename(filename)) if self.verbose: print filename #canv = canvas.Canvas(filename, pagesize = pageSize) outfile = getStringIO() if self.notes: #translate the page from landscape to portrait pageSize = pageSize[1], pageSize[0] canv = canvas.Canvas(outfile, pagesize=pageSize) canv.setPageCompression(self.compression) canv.setPageDuration(self.pageDuration) if self.title: canv.setTitle(self.title) if self.author: canv.setAuthor(self.author) if self.subject: canv.setSubject(self.subject) slideNo = 0 for slide in self.slides: #need diagnostic output if something wrong with XML slideNo = slideNo + 1 if self.verbose: print 'doing slide %d, id = %s' % (slideNo, slide.id) if self.notes: #frame and shift the slide #canv.scale(0.67, 0.67) scale_amt = (min(pageSize) / float(max(pageSize))) * .95 #canv.translate(self.pageWidth / 6.0, self.pageHeight / 3.0) #canv.translate(self.pageWidth / 2.0, .025*self.pageHeight) canv.translate(.025 * self.pageHeight, (self.pageWidth / 2.0) + 5) #canv.rotate(90) canv.scale(scale_amt, scale_amt) canv.rect(0, 0, self.pageWidth, self.pageHeight) slide.drawOn(canv) canv.showPage() #ensure outline visible by default if self.showOutline: canv.showOutline() canv.save() return self.savetofile(outfile, filename)
def saveAsPresentation(self): """Write the PDF document, one slide per page.""" if self.verbose: print "saving presentation..." pageSize = (self.pageWidth, self.pageHeight) if self.sourceFilename: filename = os.path.splitext(self.sourceFilename)[0] + ".pdf" if self.outDir: filename = os.path.join(self.outDir, os.path.basename(filename)) if self.verbose: print filename # canv = canvas.Canvas(filename, pagesize = pageSize) outfile = getStringIO() if self.notes: # translate the page from landscape to portrait pageSize = pageSize[1], pageSize[0] canv = canvas.Canvas(outfile, pagesize=pageSize) canv.setPageCompression(self.compression) canv.setPageDuration(self.pageDuration) if self.title: canv.setTitle(self.title) if self.author: canv.setAuthor(self.author) if self.subject: canv.setSubject(self.subject) slideNo = 0 for slide in self.slides: # need diagnostic output if something wrong with XML slideNo = slideNo + 1 if self.verbose: print "doing slide %d, id = %s" % (slideNo, slide.id) if self.notes: # frame and shift the slide # canv.scale(0.67, 0.67) scale_amt = (min(pageSize) / float(max(pageSize))) * 0.95 # canv.translate(self.pageWidth / 6.0, self.pageHeight / 3.0) # canv.translate(self.pageWidth / 2.0, .025*self.pageHeight) canv.translate(0.025 * self.pageHeight, (self.pageWidth / 2.0) + 5) # canv.rotate(90) canv.scale(scale_amt, scale_amt) canv.rect(0, 0, self.pageWidth, self.pageHeight) slide.drawOn(canv) canv.showPage() # ensure outline visible by default if self.showOutline: canv.showOutline() canv.save() return self.savetofile(outfile, filename)
def encryptPdfInMemory(inputPDF, userPassword, ownerPassword=None, canPrint=1, canModify=1, canCopy=1, canAnnotate=1, strength=40): """accepts a PDF file 'as a byte array in memory'; return encrypted one. This is a high level convenience and does not touch the hard disk in any way. If you are encrypting the same file over and over again, it's better to use pageCatcher and cache the results.""" try: from rlextra.pageCatcher.pageCatcher import storeFormsInMemory, restoreFormsInMemory except ImportError: raise ImportError( '''reportlab.lib.pdfencrypt.encryptPdfInMemory failed because rlextra cannot be imported. See http://developer.reportlab.com''') (bboxInfo, pickledForms) = storeFormsInMemory(inputPDF, all=1, BBoxes=1) names = bboxInfo.keys() firstPageSize = bboxInfo['PageForms0'][2:] #now make a new PDF document buf = getStringIO() canv = Canvas(buf, pagesize=firstPageSize) # set a standard ID while debugging if CLOBBERID: canv._doc._ID = "[(xxxxxxxxxxxxxxxx)(xxxxxxxxxxxxxxxx)]" encryptCanvas(canv, userPassword, ownerPassword, canPrint, canModify, canCopy, canAnnotate, strength=strength) formNames = restoreFormsInMemory(pickledForms, canv) for formName in formNames: #need to extract page size in future canv.doForm(formName) canv.showPage() canv.save() return buf.getvalue()
def _AsciiHexDecode(input): """Decodes input using ASCII-Hex coding. Not used except to provide a test of the inverse function.""" #strip out all whitespace stripped = join(split(input),'') assert stripped[-1] == '>', 'Invalid terminator for Ascii Hex Stream' stripped = stripped[:-1] #chop off terminator assert len(stripped) % 2 == 0, 'Ascii Hex stream has odd number of bytes' i = 0 output = getStringIO() while i < len(stripped): twobytes = stripped[i:i+2] output.write(chr(eval('0x'+twobytes))) i = i + 2 return output.getvalue()
def _AsciiHexDecode(input): """Decodes input using ASCII-Hex coding. Not used except to provide a test of the inverse function.""" #strip out all whitespace stripped = ''.join(input.split()) assert stripped[-1] == '>', 'Invalid terminator for Ascii Hex Stream' stripped = stripped[:-1] #chop off terminator assert len(stripped) % 2 == 0, 'Ascii Hex stream has odd number of bytes' i = 0 output = getStringIO() while i < len(stripped): twobytes = stripped[i:i+2] output.write(chr(eval('0x'+twobytes))) i = i + 2 return output.getvalue()
def cacheImageFile(filename, returnInMemory=0, IMG=None): "Processes image as if for encoding, saves to a file with .a85 extension." from reportlab.lib.utils import PIL_Image, open_for_read import zlib cachedname = os.path.splitext(filename)[0] + '.a85' if filename==cachedname: if cachedImageExists(filename): if returnInMemory: return split(open_for_read(cachedname).read(),LINEEND)[:-1] else: raise IOError, 'No such cached image %s' % filename else: img1 = PIL_Image.open(open_for_read(filename)) img = img1.convert('RGB') if IMG is not None: IMG.append(img) imgwidth, imgheight = img.size code = [] code.append('BI') # begin image # this describes what is in the image itself code.append('/W %s /H %s /BPC 8 /CS /RGB /F [/A85 /Fl]' % (imgwidth, imgheight)) code.append('ID') #use a flate filter and Ascii Base 85 raw = img.tostring() assert(len(raw) == imgwidth * imgheight, "Wrong amount of data for image") compressed = zlib.compress(raw) #this bit is very fast... encoded = _AsciiBase85Encode(compressed) #...sadly this isn't #write in blocks of 60 characters per line outstream = getStringIO(encoded) dataline = outstream.read(60) while dataline <> "": code.append(dataline) dataline = outstream.read(60) code.append('EI') if returnInMemory: return code #save it to a file f = open(cachedname,'wb') f.write(join(code, LINEEND)+LINEEND) f.close() if rl_config.verbose: print 'cached image as %s' % cachedname
def makeStream(self): "Finishes the generation and returns the TTF file as a string" stm = getStringIO() write = stm.write numTables = len(self.tables) searchRange = 1 entrySelector = 0 while searchRange * 2 <= numTables: searchRange = searchRange * 2 entrySelector = entrySelector + 1 searchRange = searchRange * 16 rangeShift = numTables * 16 - searchRange # Header write( pack(">lHHHH", 0x00010000, numTables, searchRange, entrySelector, rangeShift)) # Table directory tables = self.tables.items() tables.sort() # XXX is this the correct order? offset = 12 + numTables * 16 for tag, data in tables: if tag == 'head': head_start = offset checksum = calcChecksum(data) write(tag) write(pack(">LLL", checksum, offset, len(data))) paddedLength = (len(data) + 3) & ~3 offset = offset + paddedLength # Table data for tag, data in tables: data += "\0\0\0" write(data[:len(data) & ~3]) checksum = calcChecksum(stm.getvalue()) checksum = add32(0xB1B0AFBAL, -checksum) stm.seek(head_start + 8) write(pack('>L', checksum)) return stm.getvalue()
def makeStream(self): "Finishes the generation and returns the TTF file as a string" stm = getStringIO() write = stm.write numTables = len(self.tables) searchRange = 1 entrySelector = 0 while searchRange * 2 <= numTables: searchRange = searchRange * 2 entrySelector = entrySelector + 1 searchRange = searchRange * 16 rangeShift = numTables * 16 - searchRange # Header write(pack(">lHHHH", 0x00010000, numTables, searchRange, entrySelector, rangeShift)) # Table directory tables = self.tables.items() tables.sort() # XXX is this the correct order? offset = 12 + numTables * 16 for tag, data in tables: if tag == 'head': head_start = offset checksum = calcChecksum(data) write(tag) write(pack(">LLL", checksum, offset, len(data))) paddedLength = (len(data)+3)&~3 offset = offset + paddedLength # Table data for tag, data in tables: data += "\0\0\0" write(data[:len(data)&~3]) checksum = calcChecksum(stm.getvalue()) checksum = add32(0xB1B0AFBAL, -checksum) stm.seek(head_start + 8) write(pack('>L', checksum)) return stm.getvalue()
def encryptPdfInMemory(inputPDF, userPassword, ownerPassword=None, canPrint=1, canModify=1, canCopy=1, canAnnotate=1, strength=40): """accepts a PDF file 'as a byte array in memory'; return encrypted one. This is a high level convenience and does not touch the hard disk in any way. If you are encrypting the same file over and over again, it's better to use pageCatcher and cache the results.""" try: from rlextra.pageCatcher.pageCatcher import storeFormsInMemory, restoreFormsInMemory except ImportError: raise ImportError('''reportlab.lib.pdfencrypt.encryptPdfInMemory failed because rlextra cannot be imported. See http://developer.reportlab.com''') (bboxInfo, pickledForms) = storeFormsInMemory(inputPDF, all=1, BBoxes=1) names = bboxInfo.keys() firstPageSize = bboxInfo['PageForms0'][2:] #now make a new PDF document buf = getStringIO() canv = Canvas(buf, pagesize=firstPageSize) # set a standard ID while debugging if CLOBBERID: canv._doc._ID = "[(xxxxxxxxxxxxxxxx)(xxxxxxxxxxxxxxxx)]" encryptCanvas(canv, userPassword, ownerPassword, canPrint, canModify, canCopy, canAnnotate, strength=strength) formNames = restoreFormsInMemory(pickledForms, canv) for formName in formNames: #need to extract page size in future canv.doForm(formName) canv.showPage() canv.save() return buf.getvalue()
def _showWidgetProperties(self, widget): """Dump all properties of a widget.""" props = widget.getProperties() lines = [] for key in sorted(props.keys()): value = props[key] # Method 3 f = getStringIO() pprint.pprint(value, f) value = f.getvalue()[:-1] valueLines = value.splitlines() for i in range(1, len(valueLines)): valueLines[i] = ' ' * (len(key) + 3) + valueLines[i] value = '\n'.join(valueLines) lines.append('%s = %s' % (key, value)) text = '\n'.join(lines) self.outLines.append('<H3>Properties of Example Widget</H3>') self.outLines.append('<PRE>%s</PRE>' % text) self.outLines.append('')
def _showWidgetProperties(self, widget): """Dump all properties of a widget.""" props = widget.getProperties() keys = props.keys() keys.sort() lines = [] for key in keys: value = props[key] # Method 3 f = getStringIO() pprint.pprint(value, f) value = f.getvalue()[:-1] valueLines = string.split(value, "\n") for i in range(1, len(valueLines)): valueLines[i] = " " * (len(key) + 3) + valueLines[i] value = string.join(valueLines, "\n") lines.append("%s = %s" % (key, value)) text = join(lines, "\n") self.outLines.append("<H3>Properties of Example Widget</H3>") self.outLines.append("<PRE>%s</PRE>" % text) self.outLines.append("")
def _showWidgetProperties(self, widget): """Dump all properties of a widget.""" props = widget.getProperties() keys = list(props.keys()) keys.sort() lines = [] for key in keys: value = props[key] f = getStringIO() pprint.pprint(value, f) value = f.getvalue()[:-1] valueLines = value.split('\n') for i in range(1, len(valueLines)): valueLines[i] = ' '*(len(key)+3) + valueLines[i] value = '\n'.join(valueLines) lines.append('%s = %s' % (key, value)) text = '\n'.join(lines) self.story.append(Paragraph("<i>Properties of Example Widget</i>", self.bt)) self.story.append(Paragraph("", self.bt)) self.story.append(Preformatted(text, self.code))
def _showWidgetProperties(self, widget): """Dump all properties of a widget.""" props = widget.getProperties() keys = list(props.keys()) keys.sort() lines = [] for key in keys: value = props[key] # Method 3 f = getStringIO() pprint.pprint(value, f) value = f.getvalue()[:-1] valueLines = value.split('\n') for i in range(1, len(valueLines)): valueLines[i] = ' '*(len(key)+3) + valueLines[i] value = '\n'.join(valueLines) lines.append('%s = %s' % (key, value)) text = '\n'.join(lines) self.outLines.append('<H3>Properties of Example Widget</H3>') self.outLines.append('<PRE>%s</PRE>' % text) self.outLines.append('')
def __init__(self, fileName): if isinstance(fileName, PmlImageReader): self.__dict__ = fileName.__dict__ # borgize return #start wih lots of null private fields, to be populated by #the relevant engine. self.fileName = fileName self._image = None self._width = None self._height = None self._transparent = None self._data = None imageReaderFlags = 0 if PILImage and isinstance(fileName, PILImage.Image): self._image = fileName self.fp = getattr(fileName, 'fp', None) try: self.fileName = self._image.fileName except AttributeError: self.fileName = 'PILIMAGE_%d' % id(self) else: try: self.fp = open_for_read(fileName, 'b') if isinstance(self.fp, StringIO.StringIO().__class__): imageReaderFlags = 0 # avoid messing with already internal files if imageReaderFlags > 0: # interning data = self.fp.read() if imageReaderFlags & 2: # autoclose try: self.fp.close() except: pass if imageReaderFlags & 4: # cache the data if not self._cache: from rl_config import register_reset register_reset(self._cache.clear) data = self._cache.setdefault(md5(data).digest(), data) self.fp = getStringIO(data) elif imageReaderFlags == - 1 and isinstance(fileName, (str, unicode)): #try Ralf Schmitt's re-opening technique of avoiding too many open files self.fp.close() del self.fp # will become a property in the next statement self.__class__ = LazyImageReader if haveImages: #detect which library we are using and open the image if not self._image: self._image = self._read_image(self.fp) if getattr(self._image, 'format', None) == 'JPEG': self.jpeg_fh = self._jpeg_fh else: from reportlab.pdfbase.pdfutils import readJPEGInfo try: self._width, self._height, c = readJPEGInfo(self.fp) except: raise RuntimeError('Imaging Library not available, unable to import bitmaps only jpegs') self.jpeg_fh = self._jpeg_fh self._data = self.fp.read() self._dataA = None self.fp.seek(0) except: # TODO: Kill the catch-all et, ev, tb = sys.exc_info() if hasattr(ev, 'args'): a = str(ev.args[- 1]) + (' fileName=%r' % fileName) ev.args = ev.args[: - 1] + (a,) raise et, ev, tb else: raise
def _AsciiHexEncode(self, input): # also based on piddlePDF "Helper function used by images" output = getStringIO() for char in input: output.write('%02x' % ord(char)) return output.getvalue()
def drawToString(d,fmt='GIF', dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_): s = getStringIO() drawToFile(d,s,fmt=fmt, dpi=dpi, bg=bg, configPIL=configPIL) return s.getvalue()
def _AsciiBase85DecodePYTHON(input): """Decodes input using ASCII-Base85 coding. This is not used - Acrobat Reader decodes for you - but a round trip is essential for testing.""" outstream = getStringIO() #strip all whitespace stripped = join(split(input),'') #check end assert stripped[-2:] == '~>', 'Invalid terminator for Ascii Base 85 Stream' stripped = stripped[:-2] #chop off terminator #may have 'z' in it which complicates matters - expand them stripped = replace(stripped,'z','!!!!!') # special rules apply if not a multiple of five bytes. whole_word_count, remainder_size = divmod(len(stripped), 5) #print '%d words, %d leftover' % (whole_word_count, remainder_size) #assert remainder_size <> 1, 'invalid Ascii 85 stream!' cut = 5 * whole_word_count body, lastbit = stripped[0:cut], stripped[cut:] for i in range(whole_word_count): offset = i*5 c1 = ord(body[offset]) - 33 c2 = ord(body[offset+1]) - 33 c3 = ord(body[offset+2]) - 33 c4 = ord(body[offset+3]) - 33 c5 = ord(body[offset+4]) - 33 num = ((85L**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85*c4) + c5 temp, b4 = divmod(num,256) temp, b3 = divmod(temp,256) b1, b2 = divmod(temp, 256) assert num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!' outstream.write(chr(b1)) outstream.write(chr(b2)) outstream.write(chr(b3)) outstream.write(chr(b4)) #decode however many bytes we have as usual if remainder_size > 0: while len(lastbit) < 5: lastbit = lastbit + '!' c1 = ord(lastbit[0]) - 33 c2 = ord(lastbit[1]) - 33 c3 = ord(lastbit[2]) - 33 c4 = ord(lastbit[3]) - 33 c5 = ord(lastbit[4]) - 33 num = (((85*c1+c2)*85+c3)*85+c4)*85L + (c5 +(0,0,0xFFFFFF,0xFFFF,0xFF)[remainder_size]) temp, b4 = divmod(num,256) temp, b3 = divmod(temp,256) b1, b2 = divmod(temp, 256) assert num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!' #print 'decoding: %d %d %d %d %d -> %d -> %d %d %d %d' % ( # c1,c2,c3,c4,c5,num,b1,b2,b3,b4) #the last character needs 1 adding; the encoding loses #data by rounding the number to x bytes, and when #divided repeatedly we get one less if remainder_size == 2: lastword = chr(b1) elif remainder_size == 3: lastword = chr(b1) + chr(b2) elif remainder_size == 4: lastword = chr(b1) + chr(b2) + chr(b3) else: lastword = '' outstream.write(lastword) #terminator code for ascii 85 return outstream.getvalue()
self.lineEndings = lineEndings def readlines(self): return self.readCSVLines(self.filename) if __name__ == '__main__': from reportlab.lib.utils import getStringIO f = getStringIO('''chartId\tFundId\tReturn\tVolatility 119\t1\t0.303739275\t0.045788029 119\t2\t0.340259329\t0.056684527 119\t3\t0.244538056\t0.044776268 119\t4\t0.379326509\t0.05526936 119\t5\t0.269164078\t0.048254073 120\t1\t0.212006856\t0.045515668 120\t2\t0.404000212\t0.049965404 120\t3\t0.416953391\t0.050843349 120\t4\t0.451333469\t0.040626584 666\t1\t0.417259534\t0.051285696 666\t2\t0.21576762\t0.047812899 666\t3\t0.420633734\t0.040486482 666\t4\t0.22950049\t0.059180818 666\t5\t0.485586939\t0.047515184 ''') dm = CSVDataMaker( filename=f, sep='\t', integerColumns=['chartId'], floatColumns=['Return', 'Volatility'], sql='SELECT chartId, 100*Return, 100*Volatility FROM scatterplot_data', groupingColumn=0, )
def drawToString(d, showBoundary=rl_config.showBoundary): "Returns a SVG as a string in memory, without touching the disk" s = getStringIO() drawToFile(d, s, showBoundary=showBoundary) return s.getvalue()
def drawToString(d, msg="", showBoundary=rl_config._unset_,autoSize=1): "Returns a PDF as a string in memory, without touching the disk" s = getStringIO() drawToFile(d, s, msg=msg, showBoundary=showBoundary,autoSize=autoSize) return s.getvalue()
def tokens(s): return list(tokenize.generate_tokens(getStringIO(s).readline))
filename = self.filename data = (hasattr(filename,'read') and filename or open(filename,'rb')).read() for le in self.lineEndings: data = data.replace(le,'\n') return data.split('\n') if __name__=='__main__': from reportlab.lib.utils import getStringIO f = getStringIO('''chartId\tFundId\tReturn\tVolatility 119\t1\t0.303739275\t0.045788029 119\t2\t0.340259329\t0.056684527 119\t3\t0.244538056\t0.044776268 119\t4\t0.379326509\t0.05526936 119\t5\t0.269164078\t0.048254073 120\t1\t0.212006856\t0.045515668 120\t2\t0.404000212\t0.049965404 120\t3\t0.416953391\t0.050843349 120\t4\t0.451333469\t0.040626584 666\t1\t0.417259534\t0.051285696 666\t2\t0.21576762\t0.047812899 666\t3\t0.420633734\t0.040486482 666\t4\t0.22950049\t0.059180818 666\t5\t0.485586939\t0.047515184 ''') dm = CSVDataMaker( filename=f, sep='\t', integerColumns=['chartId'], floatColumns=['Return','Volatility'], sql='SELECT chartId, 100*Return, 100*Volatility FROM scatterplot_data', groupingColumn=0, )
def tokens(s): try: return list(tokenize.generate_tokens(getStringIO(s).readline)) except: annotateException(': line=%r' % s)
def test10(self): "test open and read of a simple relative file" from reportlab.lib.utils import open_and_read, getStringIO b = getStringIO(_rel_open_and_read('../docs/images/Edit_Prefs.gif')) b = open_and_read(b)
def _AsciiBase85EncodePYTHON(input): """Encodes input using ASCII-Base85 coding. This is a compact encoding used for binary data within a PDF file. Four bytes of binary data become five bytes of ASCII. This is the default method used for encoding images.""" outstream = getStringIO() # special rules apply if not a multiple of four bytes. whole_word_count, remainder_size = divmod(len(input), 4) cut = 4 * whole_word_count body, lastbit = input[0:cut], input[cut:] for i in range(whole_word_count): offset = i*4 b1 = ord(body[offset]) b2 = ord(body[offset+1]) b3 = ord(body[offset+2]) b4 = ord(body[offset+3]) if b1<128: num = (((((b1<<8)|b2)<<8)|b3)<<8)|b4 else: num = 16777216L * b1 + 65536 * b2 + 256 * b3 + b4 if num == 0: #special case outstream.write('z') else: #solve for five base-85 numbers temp, c5 = divmod(num, 85) temp, c4 = divmod(temp, 85) temp, c3 = divmod(temp, 85) c1, c2 = divmod(temp, 85) assert ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85*c4) + c5 == num, 'dodgy code!' outstream.write(chr(c1+33)) outstream.write(chr(c2+33)) outstream.write(chr(c3+33)) outstream.write(chr(c4+33)) outstream.write(chr(c5+33)) # now we do the final bit at the end. I repeated this separately as # the loop above is the time-critical part of a script, whereas this # happens only once at the end. #encode however many bytes we have as usual if remainder_size > 0: while len(lastbit) < 4: lastbit = lastbit + '\000' b1 = ord(lastbit[0]) b2 = ord(lastbit[1]) b3 = ord(lastbit[2]) b4 = ord(lastbit[3]) num = 16777216L * b1 + 65536 * b2 + 256 * b3 + b4 #solve for c1..c5 temp, c5 = divmod(num, 85) temp, c4 = divmod(temp, 85) temp, c3 = divmod(temp, 85) c1, c2 = divmod(temp, 85) #print 'encoding: %d %d %d %d -> %d -> %d %d %d %d %d' % ( # b1,b2,b3,b4,num,c1,c2,c3,c4,c5) lastword = chr(c1+33) + chr(c2+33) + chr(c3+33) + chr(c4+33) + chr(c5+33) #write out most of the bytes. outstream.write(lastword[0:remainder_size + 1]) #terminator code for ascii 85 outstream.write('~>') return outstream.getvalue()
def test10(self): "test open and read of a simple relative file" from reportlab.lib.utils import open_and_read, getStringIO b = getStringIO(open_and_read('../docs/images/Edit_Prefs.gif')) b = open_and_read(b)
def saveToString(self,fmt='GIF'): s = getStringIO() self.saveToFile(s,fmt=fmt) return s.getvalue()
return list(map(lambda x,f=f:list(map(f,x)),L[:])) def modifyCSVRows(f,L,R=[]): L=L[:] for r in R: L[r] = list(map(f,L[r])) return L def modifyCSVCols(f,L,C): L=L[:] if C: for r in range(len(L)): for c in C: L[r][c] = f(L[r][c]) return L if __name__ == '__main__': from reportlab.lib.utils import getStringIO L=read(getStringIO('"abc""d,ef""ghi",23,34\n1,2,3\na,4,5\n6,c,d\n')) print('originally',L) def f(x): try: x = int(x) except: pass return x print('modifyCSV',modifyCSV(f,L)) print('modifyCSVRows([1,3])',modifyCSVRows(f,L,[1,3])) print('modifyCSVCols([0,2])',modifyCSVCols(f,L,[0,2]))
def _drawImageLevel1(self, image, x1, y1, x2=None,y2=None): # Postscript Level1 version available for fallback mode when Level2 doesn't work """drawImage(self,image,x1,y1,x2=None,y2=None) : If x2 and y2 are ommitted, they are calculated from image size. (x1,y1) is upper left of image, (x2,y2) is lower right of image in piddle coordinates.""" # For now let's start with 24 bit RGB images (following piddlePDF again) component_depth = 8 myimage = image.convert('RGB') imgwidth, imgheight = myimage.size if not x2: x2 = imgwidth + x1 if not y2: y2 = y1 + imgheight drawwidth = x2 - x1 drawheight = y2 - y1 #print 'Image size (%d, %d); Draw size (%d, %d)' % (imgwidth, imgheight, drawwidth, drawheight) # now I need to tell postscript how big image is # "image operators assume that they receive sample data from # their data source in x-axis major index order. The coordinate # of the lower-left corner of the first sample is (0,0), of the # second (1,0) and so on" -PS2 ref manual p. 215 # # The ImageMatrix maps unit squre of user space to boundary of the source image # # The CurrentTransformationMatrix (CTM) maps the unit square of # user space to the rect...on the page that is to receive the # image. A common ImageMatrix is [width 0 0 -height 0 height] # (for a left to right, top to bottom image ) # first let's map the user coordinates start at offset x1,y1 on page self.code.extend([ 'gsave', '%s %s translate' % (x1,-y1 - drawheight), # need to start are lower left of image '%s %s scale' % (drawwidth,drawheight), '/scanline %d 3 mul string def' % imgwidth # scanline by multiples of image width ]) # now push the dimensions and depth info onto the stack # and push the ImageMatrix to map the source to the target rectangle (see above) # finally specify source (PS2 pp. 225 ) and by exmample self.code.extend([ '%s %s %s' % (imgwidth, imgheight, component_depth), '[%s %s %s %s %s %s]' % (imgwidth, 0, 0, -imgheight, 0, imgheight), '{ currentfile scanline readhexstring pop } false 3', 'colorimage ' ]) # data source output--now we just need to deliver a hex encode # series of lines of the right overall size can follow # piddlePDF again rawimage = myimage.tostring() hex_encoded = self._AsciiHexEncode(rawimage) # write in blocks of 78 chars per line outstream = getStringIO(hex_encoded) dataline = outstream.read(78) while dataline != "": self.code_append(dataline) dataline= outstream.read(78) self.code_append('% end of image data') # for clarity self.code_append('grestore') # return coordinates to normal
L = L[:] for r in R: L[r] = list(map(f, L[r])) return L def modifyCSVCols(f, L, C): L = L[:] if C: for r in range(len(L)): for c in C: L[r][c] = f(L[r][c]) return L if __name__ == '__main__': from reportlab.lib.utils import getStringIO L = read(getStringIO('"abc""d,ef""ghi",23,34\n1,2,3\na,4,5\n6,c,d\n')) print('originally', L) def f(x): try: x = int(x) except: pass return x print('modifyCSV', modifyCSV(f, L)) print('modifyCSVRows([1,3])', modifyCSVRows(f, L, [1, 3])) print('modifyCSVCols([0,2])', modifyCSVCols(f, L, [0, 2]))
def _drawImageLevel2(self, image, x1,y1, x2=None,y2=None): # Postscript Level2 version '''At present we're handling only PIL''' ### what sort of image are we to draw if image.mode=='L' : imBitsPerComponent = 8 imNumComponents = 1 myimage = image elif image.mode == '1': myimage = image.convert('L') imNumComponents = 1 myimage = image else : myimage = image.convert('RGB') imNumComponents = 3 imBitsPerComponent = 8 imwidth, imheight = myimage.size if not x2: x2 = imwidth + x1 if not y2: y2 = y1 + imheight drawwidth = x2 - x1 drawheight = y2 - y1 self.code.extend([ 'gsave', '%s %s translate' % (x1,-y1 - drawheight), # need to start are lower left of image '%s %s scale' % (drawwidth,drawheight)]) if imNumComponents == 3 : self.code_append('/DeviceRGB setcolorspace') elif imNumComponents == 1 : self.code_append('/DeviceGray setcolorspace') # create the image dictionary self.code_append(""" << /ImageType 1 /Width %d /Height %d %% dimensions of source image /BitsPerComponent %d""" % (imwidth, imheight, imBitsPerComponent) ) if imNumComponents == 1: self.code_append('/Decode [0 1]') if imNumComponents == 3: self.code_append('/Decode [0 1 0 1 0 1] %% decode color values normally') self.code.extend([ '/ImageMatrix [%s 0 0 %s 0 %s]' % (imwidth, -imheight, imheight), '/DataSource currentfile /ASCIIHexDecode filter', '>> % End image dictionary', 'image']) # after image operator just need to dump image dat to file as hexstring rawimage = myimage.tostring() hex_encoded = self._AsciiHexEncode(rawimage) # write in blocks of 78 chars per line outstream = getStringIO(hex_encoded) dataline = outstream.read(78) while dataline != "": self.code_append(dataline) dataline= outstream.read(78) self.code_append('> % end of image data') # > is EOD for hex encoded filterfor clarity self.code_append('grestore') # return coordinates to normal
def _AsciiHexEncode(self, input): # also based on piddlePDF "Helper function used by images" output = getStringIO() for char in asBytes(input): output.write('%02x' % char2int(char)) return output.getvalue()
def _AsciiBase85DecodePYTHON(input): """Decodes input using ASCII-Base85 coding. This is not used - Acrobat Reader decodes for you - but a round trip is essential for testing.""" outstream = getStringIO() #strip all whitespace stripped = ''.join(input.split()) #check end assert stripped[-2:] == '~>', 'Invalid terminator for Ascii Base 85 Stream' stripped = stripped[:-2] #chop off terminator #may have 'z' in it which complicates matters - expand them stripped = stripped.replace('z','!!!!!') # special rules apply if not a multiple of five bytes. whole_word_count, remainder_size = divmod(len(stripped), 5) #print '%d words, %d leftover' % (whole_word_count, remainder_size) #assert remainder_size <> 1, 'invalid Ascii 85 stream!' cut = 5 * whole_word_count body, lastbit = stripped[0:cut], stripped[cut:] for i in range(whole_word_count): offset = i*5 c1 = ord(body[offset]) - 33 c2 = ord(body[offset+1]) - 33 c3 = ord(body[offset+2]) - 33 c4 = ord(body[offset+3]) - 33 c5 = ord(body[offset+4]) - 33 num = ((85L**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85*c4) + c5 temp, b4 = divmod(num,256) temp, b3 = divmod(temp,256) b1, b2 = divmod(temp, 256) assert num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!' outstream.write(chr(b1)) outstream.write(chr(b2)) outstream.write(chr(b3)) outstream.write(chr(b4)) #decode however many bytes we have as usual if remainder_size > 0: while len(lastbit) < 5: lastbit = lastbit + '!' c1 = ord(lastbit[0]) - 33 c2 = ord(lastbit[1]) - 33 c3 = ord(lastbit[2]) - 33 c4 = ord(lastbit[3]) - 33 c5 = ord(lastbit[4]) - 33 num = (((85*c1+c2)*85+c3)*85+c4)*85L + (c5 +(0,0,0xFFFFFF,0xFFFF,0xFF)[remainder_size]) temp, b4 = divmod(num,256) temp, b3 = divmod(temp,256) b1, b2 = divmod(temp, 256) assert num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!' #print 'decoding: %d %d %d %d %d -> %d -> %d %d %d %d' % ( # c1,c2,c3,c4,c5,num,b1,b2,b3,b4) #the last character needs 1 adding; the encoding loses #data by rounding the number to x bytes, and when #divided repeatedly we get one less if remainder_size == 2: lastword = chr(b1) elif remainder_size == 3: lastword = chr(b1) + chr(b2) elif remainder_size == 4: lastword = chr(b1) + chr(b2) + chr(b3) else: lastword = '' outstream.write(lastword) #terminator code for ascii 85 return outstream.getvalue()
def _drawImageLevel2(self, image, x1, y1, x2=None, y2=None): # Postscript Level2 version '''At present we're handling only PIL''' ### what sort of image are we to draw if image.mode == 'L': imBitsPerComponent = 8 imNumComponents = 1 myimage = image elif image.mode == '1': myimage = image.convert('L') imNumComponents = 1 myimage = image else: myimage = image.convert('RGB') imNumComponents = 3 imBitsPerComponent = 8 imwidth, imheight = myimage.size if not x2: x2 = imwidth + x1 if not y2: y2 = y1 + imheight drawwidth = x2 - x1 drawheight = y2 - y1 self.code.extend([ 'gsave', '%s %s translate' % (x1, -y1 - drawheight), # need to start are lower left of image '%s %s scale' % (drawwidth, drawheight) ]) if imNumComponents == 3: self.code_append('/DeviceRGB setcolorspace') elif imNumComponents == 1: self.code_append('/DeviceGray setcolorspace') # create the image dictionary self.code_append(""" << /ImageType 1 /Width %d /Height %d %% dimensions of source image /BitsPerComponent %d""" % (imwidth, imheight, imBitsPerComponent)) if imNumComponents == 1: self.code_append('/Decode [0 1]') if imNumComponents == 3: self.code_append( '/Decode [0 1 0 1 0 1] %% decode color values normally') self.code.extend([ '/ImageMatrix [%s 0 0 %s 0 %s]' % (imwidth, -imheight, imheight), '/DataSource currentfile /ASCIIHexDecode filter', '>> % End image dictionary', 'image' ]) # after image operator just need to dump image dat to file as hexstring rawimage = myimage.tostring() assert (len(rawimage) == imwidth * imheight, 'Wrong amount of data for image') #compressed = zlib.compress(rawimage) # no zlib at moment hex_encoded = self._AsciiHexEncode(rawimage) # write in blocks of 78 chars per line outstream = getStringIO(hex_encoded) dataline = outstream.read(78) while dataline <> "": self.code_append(dataline) dataline = outstream.read(78) self.code_append('> % end of image data' ) # > is EOD for hex encoded filterfor clarity self.code_append('grestore') # return coordinates to normal