def test(): from reportlab.graphics.charts.piecharts import WedgeProperties wedges = TypedPropertyCollection(WedgeProperties) wedges.fillColor = colors.red wedges.setVector(fillColor=(colors.blue,colors.green,colors.white)) print len(_ItemWrapper) d = shapes.Drawing(400, 200) tc = TwoCircles() d.add(tc) import renderPDF renderPDF.drawToFile(d, 'sample_widget.pdf', 'A Sample Widget') print 'saved sample_widget.pdf' d = shapes.Drawing(400, 200) f = Face() f.skinColor = colors.yellow f.mood = "sad" d.add(f, name='theFace') print 'drawing 1 properties:' d.dumpProperties() renderPDF.drawToFile(d, 'face.pdf', 'A Sample Widget') print 'saved face.pdf' d2 = d.expandUserNodes() renderPDF.drawToFile(d2, 'face_copy.pdf', 'An expanded drawing') print 'saved face_copy.pdf' print 'drawing 2 properties:' d2.dumpProperties()
def demo(self): D = shapes.Drawing(200, 100) yn = YesNo() yn.x = 15 yn.y = 25 yn.size = 70 yn.testValue = 0 yn.draw() D.add(yn) yn2 = YesNo() yn2.x = 120 yn2.y = 25 yn2.size = 70 yn2.testValue = 1 yn2.draw() D.add(yn2) labelFontSize = 8 D.add(shapes.String(yn.x+(yn.size/2),(yn.y-(1.2*labelFontSize)), 'testValue=0', fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) D.add(shapes.String(yn2.x+(yn2.size/2),(yn2.y-(1.2*labelFontSize)), 'testValue=1', fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) labelFontSize = 10 D.add(shapes.String(yn.x+85,(yn.y-20), self.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) return D
def _draw(self, canvas, x, y): drawing = shapes.Drawing(self.width, self.height) drawing.rotate(self.rotation) rect = shapes.Rect(0, 0, self.width, self.height) rect.setProperties({'strokeWidth':self.strokeWidth, 'fillColor':self.fillColor, 'strokeColor':self.strokeColor, 'strokeDashArray':self.strokeDashArray, }) drawing.add(rect) drawing.drawOn(canvas, x, y)
def demo(self): D = shapes.Drawing(200, 100) s = float(self.size) ob = self.__class__() ob.x=50 ob.y=0 ob.draw() D.add(ob) D.add(shapes.String(ob.x+(s/2),(ob.y-12), ob.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=10)) return D
def symbols(canvas, params): args = eval('dict(%s)' % params) name = args.pop('name') n = args.pop('n') for key, value in args.items(): field = IMarker[key].bind(canvas.manager) args[key] = field.fromUnicode(value) m = reportlab.graphics.widgets.markers.makeMarker(name, **args) drawing = shapes.Drawing() drawing.add(m) for idx in range(n): drawing.drawOn(canvas, idx * args['dx'], idx * args['dy'])
def process(self): attrs = dict(self.getAttributeValues(attrMapping=self.attrMapping)) angle = attrs.pop('angle', 0) x, y = attrs.pop('dx'), attrs.pop('dy') self.drawing = shapes.Drawing(attrs.pop('dwidth'), attrs.pop('dheight')) self.context = chart = self.createChart(attrs) self.processSubDirectives() group = shapes.Group(chart) group.translate(0, 0) group.rotate(angle) self.drawing.add(group) manager = attr.getManager(self, interfaces.ICanvasManager) self.drawing.drawOn(manager.canvas, x, y)
def generate(img_path, pdf_width_mm, pdf_height_mm, pdf_path): """ Convert an input image (*) into a PDF output document, having the specified size in mm. (*) given as an input file path; the caller may have to store it in a temporary file. """ mm2point = lambda x: x * 0.0393701 * 72 w, h = mm2point(pdf_width_mm), mm2point(pdf_height_mm) #print w, h img = shapes.Image(0, 0, w, h, img_path) page = shapes.Drawing(w, h) page.add(img) renderPDF.drawToFile(page, pdf_path) return
def simple_label(): drawing = shapes.Drawing(width=400, height=200) drawing.add(shapes.Rect(200, 100, 10, 10, fillColor=colors.red)) x = 50 angle = 0 for item in range(3): label = Label() label.setOrigin(200, 100) label.boxAnchor = 'se' label.angle = angle #label.boxStrokeColor = colors.black label.setText('ReportLab label') drawing.add(label) x += 25 angle += 45 renderPDF.drawToFile(drawing, 'simple_label.pdf')
def axes_demo(): drawing = shapes.Drawing(width=500, height=300) data = [(5, 10, 15, 20), (10, 17, 25, 31)] x_axis = XCategoryAxis() x_axis.setPosition(100, 100, 350) x_axis.configure(data, barWidth=None) x_axis.categoryNames = ['Python', 'Ruby', 'C++', 'Haskell', 'Java'] x_axis.labels.boxAnchor = 'n' x_axis.labels[0].angle = 45 x_axis.labels[0].fontName = 'Times-Bold' x_axis.labels[1].fontName = 'Courier' x_axis.labels[1].fontSize = 16 drawing.add(x_axis) y_axis = YValueAxis() y_axis.setPosition(75, 75, 150) y_axis.configure(data) drawing.add(y_axis) renderPDF.drawToFile(drawing, 'axes_demo.pdf')
#tutorial 37 #create png #reportlab graphics sub package save to png #save to png #rectangle from reportlab.graphics import shapes, renderPM from reportlab.lib import colors drawing_obj = shapes.Drawing(700, 800) drawing_obj.add(shapes.Rect(20, 20, 300, 300, fillColor=colors.green)) renderPM.drawToFile(drawing_obj, "tutorial37.png", "PNG") #circle from reportlab.graphics import shapes, renderPM from reportlab.lib import colors drawing_obj = shapes.Drawing(700, 800) drawing_obj.add(shapes.Circle(150, 150, 75, fillColor=colors.green)) renderPM.drawToFile(drawing_obj, "tutorial37.png", "PNG")
def test(): """This function produces a pdf with examples of all the signs and symbols from this file. """ labelFontSize = 10 D = shapes.Drawing(450, 650) cb = Crossbox() cb.x = 20 cb.y = 530 D.add(cb) D.add( shapes.String(cb.x + (cb.size / 2), (cb.y - (1.2 * labelFontSize)), cb.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) tb = Tickbox() tb.x = 170 tb.y = 530 D.add(tb) D.add( shapes.String(tb.x + (tb.size / 2), (tb.y - (1.2 * labelFontSize)), tb.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) yn = YesNo() yn.x = 320 yn.y = 530 D.add(yn) tempstring = yn.__class__.__name__ + '*' D.add( shapes.String(yn.x + (tb.size / 2), (yn.y - (1.2 * labelFontSize)), tempstring, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) D.add( shapes.String( 130, 6, "(The 'YesNo' widget returns a tickbox if testvalue=1, and a crossbox if testvalue=0)", fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize * 0.75)) ss = StopSign() ss.x = 20 ss.y = 400 D.add(ss) D.add( shapes.String(ss.x + (ss.size / 2), ss.y - (1.2 * labelFontSize), ss.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) ne = NoEntry() ne.x = 170 ne.y = 400 D.add(ne) D.add( shapes.String(ne.x + (ne.size / 2), (ne.y - (1.2 * labelFontSize)), ne.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) sf = SmileyFace() sf.x = 320 sf.y = 400 D.add(sf) D.add( shapes.String(sf.x + (sf.size / 2), (sf.y - (1.2 * labelFontSize)), sf.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) ds = DangerSign() ds.x = 20 ds.y = 270 D.add(ds) D.add( shapes.String(ds.x + (ds.size / 2), (ds.y - (1.2 * labelFontSize)), ds.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) na = NotAllowed() na.x = 170 na.y = 270 D.add(na) D.add( shapes.String(na.x + (na.size / 2), (na.y - (1.2 * labelFontSize)), na.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) ns = NoSmoking() ns.x = 320 ns.y = 270 D.add(ns) D.add( shapes.String(ns.x + (ns.size / 2), (ns.y - (1.2 * labelFontSize)), ns.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) a1 = ArrowOne() a1.x = 20 a1.y = 140 D.add(a1) D.add( shapes.String(a1.x + (a1.size / 2), (a1.y - (1.2 * labelFontSize)), a1.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) a2 = ArrowTwo() a2.x = 170 a2.y = 140 D.add(a2) D.add( shapes.String(a2.x + (a2.size / 2), (a2.y - (1.2 * labelFontSize)), a2.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) fd = FloppyDisk() fd.x = 320 fd.y = 140 D.add(fd) D.add( shapes.String(fd.x + (fd.size / 2), (fd.y - (1.2 * labelFontSize)), fd.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=labelFontSize)) renderPDF.drawToFile(D, 'signsandsymbols.pdf', 'signsandsymbols.py') print 'wrote file: signsandsymbols.pdf'
data = [ # Year Month Predicted High Low (2007, 8, 113.2, 114.2, 112.2), (2007, 9, 112.8, 115.8, 109.8), (2007, 10, 111.0, 116.0, 106.0), (2007, 11, 109.8, 116.8, 102.8), (2007, 12, 107.3, 115.3, 99.3), (2008, 1, 105.2, 114.2, 96.2), (2008, 2, 104.1, 114.1, 94.1), (2008, 3, 99.9, 110.9, 88.9), (2008, 4, 94.8, 106.8, 82.8), (2008, 5, 91.2, 104.2, 78.2), ] drawing = rgs.Drawing(200, 150) predict = [row[2] - 40 for row in data] # 使用列表推导获得一列的值 high = [row[3] - 40 for row in data] low = [row[4] - 40 for row in data] times = [200 * ((row[0] + row[1] / 12.0) - 2007) - 110 for row in data] drawing.add(rgs.PolyLine(list(zip(times, predict)), strokeColor=colors.blue)) # 添加折线 drawing.add(rgs.PolyLine(list(zip(times, high)), strokeColor=colors.red)) drawing.add(rgs.PolyLine(list(zip(times, low)), strokeColor=colors.green)) drawing.add(rgs.String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red)) renderPDF.drawToFile(drawing, 'report.pdf', 'Sunspots') # ### reportlab.graphics.shapes.PolyLine
def simple_drawing(): drawing = shapes.Drawing(400, 400) drawing.add(shapes.Circle(200, 200, 100, fillColor=colors.red)) renderPDF.drawToFile(drawing, 'simple_drawing.pdf', msg='My Drawing')
SCREEN_TO_METALY1 = 6 SCREEN_TO_METALY2 = 108 marginxTop = 20 marginxBottom = 62 marginx = 70 marginy = 10 ##### preparation (box and field) box = Boxmaker.Box(BOX_LENGTH, BOX_INNER_HEIGHT + MATERIAL_THICKNESS, BOX_WIDTH, MATERIAL_THICKNESS, CUT_WIDTH, 2.5*MATERIAL_THICKNESS) box._compute_dimensions() box._initialize_document("6box_wood.pdf", PLATE_LENGTH, PLATE_WIDTH, 0.001) # ################## render the bottom part boxBottom = shapes.Drawing(PLATE_LENGTH, PLATE_WIDTH) boxBottom.add(box._draw_width_by_depth_side(0, 0)) # the field boxBottom.add(box.drawField(BOX_LENGTH - MATERIAL_THICKNESS, 0, BOX_WIDTH/2, 5)) boxBottom.add(box.drawField(MATERIAL_THICKNESS, 0, BOX_WIDTH/2, 5, -1)) def draw_side(x, y, attach_height, hole_dist_x, fieldnum, fliph = 1, colorout = green, colorin = green): f = box.drawField(x, y, BOX_WIDTH/2, fieldnum, fliph, colorout) attach_width = 12 insert_length = fliph * 10 insert_width = 15 margin_hole = 5 l1 = box._draw_line(x, y, x, y + MATERIAL_THICKNESS, colorout) l2 = box._draw_line(x, y + MATERIAL_THICKNESS, x - insert_length, y + MATERIAL_THICKNESS, colorout)
def draw(tree, opts): for attr in opts.visible: tree.set_render_attr(attr, "visible", 1, 1) leaves = tree.leaves() maxdepth = max([leaf.depth for leaf in leaves]) width = opts.unitwidth * maxdepth set_terminal_ypos(tree) calc_internal_ypos(tree) maxleaf = leaves[-1] maxy = maxleaf.y minleaf = leaves[0] miny = minleaf.y height = sum([x.get_height() for x in leaves]) max_labelwidth = max( [ pdfmetrics.stringWidth(str(l.tiplabel), l.render_info["tiplabel"]["font"], l.calc_tiplabel_font_size()) \ + (l.render_info["tiplabel"]["x_offset"] * l.baseheight) for l in leaves ] ) #unitwidth = (height*((avail_w-max_labelwidth)/avail_h)) / maxdepth calc_xpos(tree, maxdepth, opts.unitwidth) for i in range(10): smooth_xpos(tree) ## tree.translate(tree.calc_strokewidth()/2.0, ## tree.calc_tiplabel_font_size()/2.0) ## width = (maxdepth * unitwidth) + \ ## max_labelwidth + (tree.calc_strokewidth() * 0.5) #width *= 1.2 # if fill: width = avail_w if opts.lengths_are_support: def func(n): if n.length is not None: n.support = n.length n.length = None if not n.children: n.support = None tree.foreach(func) if opts.thicken is not None: def func(n): try: if n.support >= opts.thicken: n.thicken = 1 except AttributeError: pass tree.foreach(func) if opts.draw_phylogram: if opts.scale: brlen_transform = opts.scale else: brlen_transform = (maxdepth * opts.unitwidth) * \ 1.0/max([l.calc_length_to_root() for l in leaves]) scale_branches(tree, brlen_transform) if opts.verbose: print "%s: brlen_transform=%s" % (opts.outfile, brlen_transform) # draw scalebar max_length_to_root = max([n.calc_length_to_root() for n in leaves]) max_x = max([n.x for n in leaves]) scalebar = shapes.Group() font_size = tree.calc_tiplabel_font_size() * 0.9 ## scalebar.add(shapes.String(max_x, tree.baseheight-font_size*(1.0/3.0), ## " %g" % max_length_to_root, ## textAnchor="start", fontSize=font_size)) scalebar.add( shapes.String(tree.x, 1, "0.0", textAnchor="middle", fontSize=font_size)) scalebar.add( shapes.Line(tree.x, tree.baseheight, max_x, tree.baseheight, strokeColor=colors.black, strokeWidth=1.0)) scalebar.add( shapes.Line(tree.x, tree.baseheight * 1.2, tree.x, tree.baseheight * 0.8, strokeColor=colors.black, strokeWidth=1.0)) scalebar.add( shapes.Line(max_x, tree.baseheight * 1.2, max_x, tree.baseheight * 0.8, strokeColor=colors.black, strokeWidth=1.0)) interval = 10**(math.floor(math.log10(float(max_length_to_root)))) nintervals = int(math.modf(max_length_to_root / interval)[1]) if nintervals == 1: interval = interval / 4.0 x = interval while x < max_length_to_root: scalebar.add( shapes.Line(x * brlen_transform, tree.baseheight * 1.2, x * brlen_transform, tree.baseheight * 0.8, strokeColor=colors.black, strokeWidth=0.5)) scalebar.add( shapes.String(x * brlen_transform, 1, str(x), textAnchor="middle", fontSize=font_size)) x += interval height += tree.baseheight * 3 tree.translate(0, tree.baseheight * 3) tree.drawables.append(scalebar) font_size = tree.calc_tiplabel_font_size() if opts.title: title = shapes.Group() title.add( shapes.String(0, tree.x + height + font_size * 2, opts.title, fontSize=font_size)) now = datetime.datetime.now().ctime() title.add( shapes.String(width + max_labelwidth, tree.x + height + font_size * 2, now, fontSize=font_size, textAnchor="end")) tree.drawables.append(title) height += font_size * 2 #print list(tree.iternodes())[0].draw().getBounds() ## boxes = [ n.draw().getBounds() for n in tree.iternodes() ] ## minx = min([ b[0] for b in boxes ]) ## maxx = max([ b[2] for b in boxes ]) ## miny = min([ b[1] for b in boxes ]) ## maxy = max([ b[3] for b in boxes ]) ## tree.translate(dx=-minx,dy=-miny) ## boxes = [ n.draw().getBounds() for n in tree.iternodes() ] ## minx = min([ b[0] for b in boxes ]) ## maxx = max([ b[2] for b in boxes ]) ## miny = min([ b[1] for b in boxes ]) ## maxy = max([ b[3] for b in boxes ]) ## width = maxx; height = maxy drawing = shapes.Drawing(width, height) for node in tree.iternodes(): ## x1, y1, x2, y2 = node.getBounds() ## if x2 > width: ## print node.tiplabel, x2 drawing.add(node.draw()) x1, y1, x2, y2 = drawing.getBounds() drawing.width = x2 - x1 drawing.height = y2 - y1 return drawing
#TUTORIAL 41 #how to generate qr code from reportlab.graphics.barcode import qr from reportlab.pdfgen import canvas from reportlab.graphics import shapes,renderPDF,renderPM pdf=canvas.Canvas("tutorial41.pdf") text="www.totaltechnologyabcd.com" qrcode=qr.QrCodeWidget(text) drawing_obj=shapes.Drawing() drawing_obj.add(qrcode) renderPDF.draw(drawing_obj,pdf,200,200) pdf.save() #how to generate qr code in png from reportlab.graphics.barcode import qr from reportlab.graphics import shapes,renderPM drawing_obj=shapes.Drawing(500,500) code="www.totaltechnoogy.com" qrcode=qr.QrCodeWidget(code) qrcode.barWidth=200 drawing_obj.add(qrcode) renderPM.drawToFile(drawing_obj, fn="roni.png" ) #generate qrcode using qrcode package import qrcode from reportlab.platypus import SimpleDocTemplate,Image from reportlab.lib.styles import getSampleStyleSheet from reportlab.pdfgen import canvas from reportlab.lib.units import cm pdf=canvas.Canvas("tutorial41_1.pdf")
def draw_number(w, h, nr): w, h = w*mm, h*mm d = shapes.Drawing(w, h) d.add(shapes.String(w/2, h/2, f"{nr}", textAnchor="middle", fontSize=15, fillColor=black)) return renderPDF.drawToString(d)
def __init__(self, size=(300, 300), name='RLCanvas'): self.size = size self._initOutput() Canvas.__init__(self, size, name) self.drawing = shapes.Drawing(size[0], size[1])
#tutorial 36 #create index #reportlab graphics sub package #RECTANGLE DRAWING from reportlab.graphics import shapes, renderPDF from reportlab.lib import colors drawing_obj = shapes.Drawing(500, 200) drawing_obj.add(shapes.Rect(10, 10, 250, 100, fillColor=colors.blue)) renderPDF.drawToFile(drawing_obj, "tutorial36.pdf", msg="tutorial36") #CIRCLE DRAWING from reportlab.graphics import shapes, renderPDF from reportlab.lib import colors drawing_obj = shapes.Drawing(500, 200) drawing_obj.add(shapes.Circle(50, 50, 50, fillColor=colors.blue)) renderPDF.drawToFile(drawing_obj, "tutorial36.pdf", msg="tutorial36")
# Option two: background from a ReportLab image. # Note we just load it from file, but you could do something fancier... # The size parameters don't matter as pylabels will scale it to fit the page. specs = labels.Specification(210, 297, 2, 8, 90, 25, corner_radius=2, background_image=shapes.Image( 0, 0, 750, 1055, file2)) process_sheet(specs, "page_background_image.pdf") # Option three: use a ReportLab drawing. # Again, this will be automatically scaled so choose the size to suit you. # Using the size of the page sounds like a sensible option. bg = shapes.Drawing(width=210.5, height=297) bg.add(shapes.String(105, 50, "My cool background", textAnchor="middle")) bg.add(shapes.Wedge(10, 155, 95, 30, 90, fillColor=colors.green)) specs = labels.Specification(210, 297, 2, 8, 90, 25, corner_radius=2, background_image=bg) process_sheet(specs, "page_background_drawing.pdf")