def drawInlineImage(self, canvas, preserveAspectRatio=False, anchor='sw', anchorAtXY=False, showBoundary=False): """Draw an Image into the specified rectangle. If width and height are omitted, they are calculated from the image size. Also allow file names as well as images. This allows a caching mechanism""" width = self.width height = self.height if width < 1e-6 or height < 1e-6: return False x, y, self.width, self.height, scaled = aspectRatioFix( preserveAspectRatio, anchor, self.x, self.y, width, height, self.imgwidth, self.imgheight, anchorAtXY) # this says where and how big to draw it if not canvas.bottomup: y = y + height canvas._code.append('q %s 0 0 %s cm' % (fp_str(self.width), fp_str(self.height, x, y))) # self._code.extend(imagedata) if >=python-1.5.2 for line in self.imageData: canvas._code.append(line) canvas._code.append('Q') if showBoundary: canvas.rect(x, y, width, height, stroke=1, fill=0) return True
def drawInlineImage(self, canvas, preserveAspectRatio=False,anchor='sw'): """Draw an Image into the specified rectangle. If width and height are omitted, they are calculated from the image size. Also allow file names as well as images. This allows a caching mechanism""" width = self.width height = self.height if width<1e-6 or height<1e-6: return False x,y,self.width,self.height, scaled = aspectRatioFix(preserveAspectRatio,anchor,self.x,self.y,width,height,self.imgwidth,self.imgheight) # this says where and how big to draw it if not canvas.bottomup: y = y+height canvas._code.append('q %s 0 0 %s cm' % (fp_str(self.width), fp_str(self.height, x, y))) # self._code.extend(imagedata) if >=python-1.5.2 for line in self.imageData: canvas._code.append(line) canvas._code.append('Q') return True
def main(): import glob, traceback, sys from reportlab.lib.boxstuff import aspectRatioFix argv = sys.argv[1:] outDir = os.getcwd() formats = ['pdf'] autoSize = False verbose = logging.CRITICAL handling = False for arg in argv: if arg.startswith('--outdir='): outDir = arg[9:] if not os.path.isabs(outDir): outDir = os.path.abspath(outDir) continue elif arg.startswith('--formats='): formats = [_.strip() for _ in arg[10:].split(',')] continue elif arg.startswith('--verbose='): verbose = max(int(arg[10:]), logging.CRITICAL) continue elif arg == '--verbose': verbose = logging.DEBUG continue elif arg == '--autoSize': autoSize = True continue elif arg.startswith('--'): raise ValueError('unknown option %r' % arg) if verbose != None: if not handling: logger.addHandler(logging.StreamHandler(sys.stdout)) handling = True logger.setLevel(verbose) for fn in glob.glob(arg): try: d = svg2rlg(fn) if d: if autoSize: preserveAspectRatio = False anchor = 'sw' width = float(d.width) height = float(d.height) x0, y0, x1, y1 = d.getBounds() wo = x1 - x0 ho = y1 - y0 xn, yn, wn, hn, scaled = aspectRatioFix( preserveAspectRatio, anchor, 0, 0, width, height, wo, ho) sx = wn / d.width sy = hn / d.height d.transform = [sx, 0, 0, sy, -sx * x0, -sy * y0] d.width = wn d.height = hn fnRoot = os.path.splitext(os.path.basename(fn))[0] d.save(formats=formats, verbose=verbose, fnRoot=fnRoot, outDir=outDir) except: traceback.print_exc()
def drawImage(self, image, filename, x, y, width=None, height=None, mask=None, preserveAspectRatio=False, anchor='c'): self._currentPageHasImages = 1 #imagename, use it s = '%s%s' % (filename, mask) if isUnicode(s): s = s.encode('utf-8') name = _digester(s) # in the pdf document, this will be prefixed with something to # say it is an XObject. Does it exist yet? regName = self._doc.getXObjectName(name) imgObj = self._doc.idToObject.get(regName, None) if not imgObj: #first time seen, create and register the PDFImageXobject imgObj = pdfdoc.PDFImageXObject(name, mask=mask) ext = os.path.splitext(filename)[1].lower() if not (ext in ('.jpg', '.jpeg') and imgObj.loadImageFromJPEG(image)): if rl_config.useA85: imgObj.loadImageFromA85(image) else: imgObj.loadImageFromRaw(image) imgObj.name = name self._setXObjects(imgObj) self._doc.Reference(imgObj, regName) self._doc.addForm(name, imgObj) smask = getattr(imgObj, '_smask', None) if smask: #set up the softmask obtained above mRegName = self._doc.getXObjectName(smask.name) mImgObj = self._doc.idToObject.get(mRegName, None) if not mImgObj: self._setXObjects(smask) imgObj.smask = self._doc.Reference(smask, mRegName) else: imgObj.smask = pdfdoc.PDFObjectReference(mRegName) del imgObj._smask # ensure we have a size, as PDF will make it 1x1 pixel otherwise! x, y, width, height, scaled = aspectRatioFix(preserveAspectRatio, anchor, x, y, width, height, imgObj.width, imgObj.height) # scale and draw self.saveState() self.translate(x, y) self.scale(width, height) self._code.append("/%s Do" % regName) self.restoreState() # track what's been used on this page self._formsinuse.append(name) return (imgObj.width, imgObj.height)