def __init__(self, boards): self.boards = boards self.header_style = ParagraphStyle( fontName=DEFAULT_FONT, fontSize=12, name='header', spaceAfter=10, borderColor=Color(0, 0, 0, 1), borderPadding=3, borderWidth=1, ) self.body_style = ParagraphStyle( fontName=DEFAULT_FONT, fontSize=8, name='body', spaceAfter=4, justifyBreaks=1, backColor=Color(0, 0, 0, 0.1), )
def _draw_textarea(self, canvas: Canvas, op: OrderPosition, order: Order, o: dict): font = o['fontfamily'] if o['bold']: font += ' B' if o['italic']: font += ' I' align_map = { 'left': TA_LEFT, 'center': TA_CENTER, 'right': TA_RIGHT } style = ParagraphStyle( name=uuid.uuid4().hex, fontName=font, fontSize=float(o['fontsize']), leading=float(o['fontsize']), autoLeading="max", textColor=Color(o['color'][0] / 255, o['color'][1] / 255, o['color'][2] / 255), alignment=align_map[o['align']] ) text = conditional_escape( self._get_text_content(op, order, o) or "", ).replace("\n", "<br/>\n") # reportlab does not support RTL, ligature-heavy scripts like Arabic. Therefore, we use ArabicReshaper # to resolve all ligatures and python-bidi to switch RTL texts. configuration = { 'delete_harakat': True, 'support_ligatures': False, } reshaper = ArabicReshaper(configuration=configuration) try: text = "<br/>".join(get_display(reshaper.reshape(l)) for l in text.split("<br/>")) except: logger.exception('Reshaping/Bidi fixes failed on string {}'.format(repr(text))) p = Paragraph(text, style=style) w, h = p.wrapOn(canvas, float(o['width']) * mm, 1000 * mm) # p_size = p.wrap(float(o['width']) * mm, 1000 * mm) ad = getAscentDescent(font, float(o['fontsize'])) canvas.saveState() # The ascent/descent offsets here are not really proven to be correct, they're just empirical values to get # reportlab render similarly to browser canvas. if o.get('downward', False): canvas.translate(float(o['left']) * mm, float(o['bottom']) * mm) canvas.rotate(o.get('rotation', 0) * -1) p.drawOn(canvas, 0, -h - ad[1] / 2) else: canvas.translate(float(o['left']) * mm, float(o['bottom']) * mm + h) canvas.rotate(o.get('rotation', 0) * -1) p.drawOn(canvas, 0, -h - ad[1]) canvas.restoreState()
def main(): # To do: parse some useful arguments as rendering options here # e.g. outline color, page size, etc. # # For now, put these options into variables here: bg_color = Color(1.0, 1.0, 1.0, alpha=1.0) # For now just assume a list of files # kk: changed how to import pickle files so that they can be merged path = 'C:/Users/sawkk/Desktop/CellModeller2/data/Tutorial_1p10-17-06-01-11-24' a = os.listdir(path) infns = [_ for i in xrange(len(a))] for i in xrange(len(a)): infns[i] = path + '/' + a[i] #sys.argv[1:] for infn in infns: # File names if infn[-7:] != '.pickle': print 'Ignoring file %s, because its not a pickle...' % (infn) continue outfn = string.replace(infn, '.pickle', '.pdf') #print 'Processing %s to generate %s'%(infn,outfn) #kk: commented the print # Import data data = importPickle(infn) if not data: print "Problem importing data!" return # Create a pdf canvas thing pdf = MyPDFGenerator(outfn, data, bg_color) # Get the bounding square of the colony to size the image # This will resize the image to fit the page... # ** alternatively you can specify a fixed world size here '''(w,h) = pdf.computeBox() sqrt2 = math.sqrt(2) world = (w/sqrt2,h/sqrt2)''' world = (250, 250) # Page setup page = (20, 20) center = (0, 0) # Render pdf #print 'Rendering PDF output to %s'%outfn #commented out print pdf.draw_frame(outfn, world, page, center) #kk: merging pdfs append_pdf(PdfFileReader(open(outfn, 'rb')), output) output.write(open("merged.pdf", "wb"))
def draw(self): fillColor = self.fillColor strokeColor = self.strokeColor g = Group() bg = self.background bd = self.border bdw = self.borderWidth shadow = self.shadow x, y = self.x, self.y if bg: if shadow is not None and 0 <= shadow < 1: shadow = Color(bg.red * shadow, bg.green * shadow, bg.blue * shadow) self._paintLogo(g, dy=-2.5, dx=2, fillColor=shadow) self._paintLogo(g, fillColor=fillColor, strokeColor=strokeColor, _ocolors=self.oColors or None, _pagecolors=self.pageColors or None) g.skew(kx=self.skewX, ky=self.skewY) g.shift(self._dx, self._dy) G = Group() G.add(g) _w, _h = 130, 86 w, h = self.width, self.height if bg or (bd and bdw): G.insert( 0, Rect(0, 0, _w, _h, fillColor=bg, strokeColor=bd, strokeWidth=bdw)) if w != _w or h != _h: G.scale(w / float(_w), h / float(_h)) angle = self.angle if self.angle: w, h = w / 2., h / 2. G.shift(-w, -h) G.rotate(angle) G.shift(w, h) xFlip = getattr(self, 'xFlip', 0) and -1 or 0 yFlip = getattr(self, 'yFlip', 0) and -1 or 0 if xFlip or yFlip: sx = xFlip or 1 sy = yFlip or 1 G.shift(sx * x + w * xFlip, sy * y + yFlip * h) G = Group(G, transform=(sx, 0, 0, sy, 0, 0)) else: G.shift(x, y) return G
def __init__(self,width=400,height=400,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) self.transform = (1,0,0,1,0,0) self.add(Polygon(points=[108.3032,252.9412,200,288.2353,291.6968,252.9412,306.9796,138.2353,200,58.82353,93.02039,138.2353,108.3032,252.9412],fillColor=Color(1,.972549,.862745,1),fillOpacity=None,strokeColor=None,strokeWidth=0,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(Polygon(points=[85.37899,266.1765,200,252.9412,261.1312,235.2941,276.414,155.8824,200,94.11765,131.2274,160.2941,85.37899,266.1765],fillColor=Color(0,1,1,1),fillOpacity=None,strokeColor=None,strokeWidth=0,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(Polygon(points=[138.8688,235.2941,200,261.7647,261.1312,235.2941,329.9038,125,200,164.7059,108.3032,147.0588,138.8688,235.2941],fillColor=Color(.596078,.984314,.596078,1),fillOpacity=None,strokeColor=None,strokeWidth=0,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(PolyLine(points=[108.3032,252.9412,200,288.2353,291.6968,252.9412,306.9796,138.2353,200,58.82353,93.02039,138.2353,108.3032,252.9412],strokeColor=Color(1,.972549,.862745,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(PolyLine(points=[85.37899,266.1765,200,252.9412,261.1312,235.2941,276.414,155.8824,200,94.11765,131.2274,160.2941,85.37899,266.1765],strokeColor=Color(0,1,1,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(PolyLine(points=[138.8688,235.2941,200,261.7647,261.1312,235.2941,329.9038,125,200,164.7059,108.3032,147.0588,138.8688,235.2941],strokeColor=Color(.596078,.984314,.596078,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(Line(200,200,200,350,strokeColor=Color(0,0,0,1),strokeWidth=.5,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=(2,2),strokeOpacity=None)) self.add(Line(200,200,329.9038,275,strokeColor=Color(0,0,0,1),strokeWidth=.5,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=(2,2),strokeOpacity=None)) self.add(Line(200,200,329.9038,125,strokeColor=Color(0,0,0,1),strokeWidth=.5,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=(2,2),strokeOpacity=None)) self.add(Line(200,200,200,50,strokeColor=Color(0,0,0,1),strokeWidth=.5,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=(2,2),strokeOpacity=None)) self.add(Line(200,200,70.09619,125,strokeColor=Color(0,0,0,1),strokeWidth=.5,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=(2,2),strokeOpacity=None)) self.add(Line(200,200,70.09619,275,strokeColor=Color(0,0,0,1),strokeWidth=.5,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=(2,2),strokeOpacity=None)) v0=self._nn(Group()) v0.transform = (1,0,0,1,200,357.5) v0.add(String(-2.22,-4,'a',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,336.399,278.75) v0.add(String(-2.5,-4,'b',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,336.399,121.25) v0.add(String(-2.22,-4,'c',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,200,42.5) v0.add(String(-2.5,-4,'d',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,63.601,121.25) v0.add(String(-2.22,-4,'e',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,63.601,278.75) v0.add(String(-1.665,-4,'f',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1)))
def __init__(self, path): self.path = path self.styleSheet = getSampleStyleSheet() self.elements = [] # colors - Azul turkeza 367AB3 self.colorOhkaGreen0 = Color((45.0/255), (166.0/255), (153.0/255), 1) self.colorOhkaGreen1 = Color((182.0/255), (227.0/255), (166.0/255), 1) self.colorOhkaGreen2 = Color((140.0/255), (222.0/255), (192.0/255), 1) #self.colorOhkaGreen2 = Color((140.0/255), (222.0/255), (192.0/255), 1) self.colorOhkaBlue0 = Color((54.0/255), (122.0/255), (179.0/255), 1) self.colorOhkaBlue1 = Color((122.0/255), (180.0/255), (225.0/255), 1) self.colorOhkaGreenLineas = Color((50.0/255), (140.0/255), (140.0/255), 1) self.firstPage() self.nextPagesHeader(True) self.remoteSessionTableMaker() self.nextPagesHeader(False) self.inSiteSessionTableMaker() self.nextPagesHeader(False) self.extraActivitiesTableMaker() self.nextPagesHeader(False) self.summaryTableMaker() # Build self.doc = SimpleDocTemplate(path, pagesize=LETTER) self.doc.multiBuild(self.elements, canvasmaker=FooterCanvas)
def visualize_line(self, line_number, records, can, page, height_offset): """ Visualize line <line_number> by drawing records on canvas """ random.seed(line_number*page) can.setLineWidth(1) r = random.randrange(20, 80, 1)/100.0 g = random.randrange(20, 80, 1)/100.0 b = random.randrange(20, 80, 1)/100.0 nontransparent = Color(r, g, b) transparent = Color(r, g, b, 0.3) can.setStrokeColor(nontransparent) can.setFillColor(transparent) text = str(line_number) for rect in records: can.rect(rect.x, height_offset-rect.y, rect.width-2, rect.height, fill=1) can.setFillColor(nontransparent) can.drawString(20 + (line_number % 2) * 500, height_offset-records[0].y, str(line_number))
def _drawAfter(self, canvas, x, y, w, h): # Strike if self.get("strike", None): ff = 0.125 * self["fontSize"] yStrike = y + 2.0 * ff textColor = self.get("color", Color(0, 0, 0)) canvas.saveState() canvas.setLineWidth(ff * 0.75) canvas.setStrokeColor(textColor) canvas.line(x, yStrike, x + w, yStrike) canvas.restoreState()
def makeBorder(width, style="solid", color=Color(1, 0, 0)): return dict(borderLeftColor=color, borderLeftWidth=width, borderLeftStyle=style, borderRightColor=color, borderRightWidth=width, borderRightStyle=style, borderTopColor=color, borderTopWidth=width, borderTopStyle=style, borderBottomColor=color, borderBottomWidth=width, borderBottomStyle=style)
def _drawBefore(self, canvas, x, y, w, h): canvas.saveState() textColor = self.get("color", Color(0, 0, 0)) # Background bg = self.get("backgroundColor", None) if bg is not None: # draw a filled rectangle (with no stroke) using bg color canvas.setFillColor(bg) canvas.rect(x, y, w, h, fill=1, stroke=0) # Borders def _drawBorderLine(bstyle, width, color, x1, y1, x2, y2): # We need width and border style to be able to draw a border if width and bstyle: # If no color for border is given, the text color is used (like defined by W3C) if color is None: color = textColor # print "Border", bstyle, width, color if color is not None: canvas.setStrokeColor(color) canvas.setLineWidth(width) canvas.line(x1, y1, x2, y2) _drawBorderLine(self.get("borderLeftStyle", None), self.get("borderLeftWidth", None), self.get("borderLeftColor", None), x, y, x, y + h) _drawBorderLine(self.get("borderRightStyle", None), self.get("borderRightWidth", None), self.get("borderRightColor", None), x + w, y, x + w, y + h) _drawBorderLine(self.get("borderTopStyle", None), self.get("borderTopWidth", None), self.get("borderTopColor", None), x, y + h, x + w, y + h) _drawBorderLine(self.get("borderBottomStyle", None), self.get("borderBottomWidth", None), self.get("borderBottomColor", None), x, y, x + w, y) # Underline if self.get("underline", None): ff = 0.125 * self["fontSize"] yUnderline = y - 1.0 * ff canvas.setLineWidth(ff * 0.75) canvas.setStrokeColor(textColor) canvas.line(x, yUnderline, x+w, yUnderline) canvas.restoreState()
def __init__(self,width=400,height=200,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) self.transform = (1,0,0,1,0,0) self.add(Rect(0,0,100,20,rx=0,ry=0,fillColor=Color(1,1,0,1),fillOpacity=None,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(String(5,5,'Text in the box',textAnchor='start',fontName='Times-Roman',fontSize=12,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,25,25) v0.add(Rect(0,0,100,20,rx=0,ry=0,fillColor=Color(1,1,0,1),fillOpacity=None,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) v0.add(String(5,5,'Text in the box',textAnchor='start',fontName='Times-Roman',fontSize=12,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,25,25) v1=v0._nn(Group()) v1.transform = (1,0,0,1,25,25) v1.add(Rect(0,0,100,20,rx=0,ry=0,fillColor=Color(1,1,0,1),fillOpacity=None,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) v1.add(String(5,5,'Text in the box',textAnchor='start',fontName='Times-Roman',fontSize=12,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,25,25) v1=v0._nn(Group()) v1.transform = (1,0,0,1,25,25) v2=v1._nn(Group()) v2.transform = (1,0,0,1,25,25) v2.add(Rect(0,0,100,20,rx=0,ry=0,fillColor=Color(1,1,0,1),fillOpacity=None,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) v2.add(String(5,5,'Text in the box',textAnchor='start',fontName='Times-Roman',fontSize=12,fillColor=Color(0,0,0,1)))
def _make_table(self, positions): data = [ Row(items=[ RowItem('L#'), RowItem('Irradiation'), RowItem('Sample'), RowItem('Positions') ]), ] ts = self._new_style(header_line_idx=0) ts.add('FONTSIZE', (0, 1), (-1, -1), 8) # ts.add('VALIGN', (-3, 1), (-1, -1), 'MIDDLE') for idx, pi in enumerate(positions): # row = (pi.labnumber, pi.irradiation_str, pi.sample, # pi.position_str) items = [ RowItem(i) for i in (pi.labnumber, pi.irradiation_str, pi.sample, pi.position_str) ] row = Row(items=items) data.append(row) c = colors.white if self.options.show_colors: cc = pi.color if sum(cc) < 1.5: ts.add('TEXTCOLOR', (0, idx + 1), (-1, idx + 1), colors.white) c = Color(*cc) elif self.options.use_alternating_background: if idx % 2 == 0: c = self.options.get_alternating_background() if sum(c) < 1.5: ts.add('TEXTCOLOR', (0, idx + 1), (-1, idx + 1), colors.white) ts.add('BACKGROUND', (0, idx + 1), (-1, idx + 1), c) cw = map(lambda x: mm * x, [13, 21, 22, 38]) rh = [mm * 5 for _ in xrange(len(data))] t = self._new_table(ts, data, colWidths=cw, rowHeights=rh) return t
def border(): draw = Drawing(1, 1) rect = Polygon(points=[ -12, cm / 6, (PAGE_WIDTH - (RIGHT_MARGIN + LEFT_MARGIN)), cm / 6, PAGE_WIDTH - (RIGHT_MARGIN + LEFT_MARGIN), -1 * (PAGE_HEIGHT - (TOP_MARGIN + BOTTOM_MARGIN + cm / 2)), -12, -1 * (PAGE_HEIGHT - (TOP_MARGIN + BOTTOM_MARGIN + cm / 2)) ], strokeColor=Color(*charts.BG_COLOR)) rect.fillColor = Color(*charts.BG_COLOR, 0.1) draw.add(rect) draw.add(Circle(100, 90, 5, fillColor=colors.green)) lab = Label() lab.setOrigin(350, -50) lab.boxAnchor = 'ne' lab.fillColor = Color(*charts.BG_COLOR, 0.15) lab.fontSize = 72 lab.angle = 60 lab.dx = 0 lab.dy = 0 lab.setText('Wisdom Tests') draw.add(lab) return draw
def draw_atom(self, c): fillc, strc = self.colorx, self.linex if self.islight: fillc = self.colorx * self.contrast + ProjectedShape.write * ( 1 - self.contrast) strc = self.linex * self.contrast + ProjectedShape.write * ( 1 - self.contrast) c.setStrokeColor(Color(*list(strc / 255.0))) c.setFillColor(Color(*list(fillc / 255.0))) glow = ProjectedShape.atom_glow c.circle(self.coord[0], self.coord[1], ProjectedShape.atom_radius, stroke=1, fill=1) c.setFillColor( Color(*list(fillc / 255.0 * 0.5 + ProjectedShape.write / 255.0 * 0.5))) c.circle(self.coord[0] - glow[0], self.coord[1] - glow[1], ProjectedShape.atom_radius * 0.2, stroke=0, fill=1)
def __init__(self,width=400,height=200,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) self.transform = (1,0,0,1,0,0) self.add(Line(50,50,350,50,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(Line(50,50,50,45,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=10,strokeDashArray=None,strokeOpacity=None)) self.add(Line(143.75,50,143.75,45,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=10,strokeDashArray=None,strokeOpacity=None)) self.add(Line(237.5,50,237.5,45,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=10,strokeDashArray=None,strokeOpacity=None)) self.add(Line(331.25,50,331.25,45,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=10,strokeDashArray=None,strokeOpacity=None)) v0=self._nn(Group()) v0.transform = (1,0,0,1,50,45) v0.add(String(-5,-10,'10',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,143.75,45) v0.add(String(-5,-10,'20',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,237.5,45) v0.add(String(-5,-10,'30',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,331.25,45) v0.add(String(-5,-10,'40',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) self.add(Line(284.375,50,284.375,175,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=0,strokeDashArray=None,strokeOpacity=None)) self.add(Line(284.375,50,279.375,50,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=10,strokeDashArray=None,strokeOpacity=None)) self.add(Line(284.375,89.0625,279.375,89.0625,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=10,strokeDashArray=None,strokeOpacity=None)) self.add(Line(284.375,128.125,279.375,128.125,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=10,strokeDashArray=None,strokeOpacity=None)) self.add(Line(284.375,167.1875,279.375,167.1875,strokeColor=Color(0,0,0,1),strokeWidth=1,strokeLineCap=0,strokeLineJoin=0,strokeMiterLimit=10,strokeDashArray=None,strokeOpacity=None)) v0=self._nn(Group()) v0.transform = (1,0,0,1,279.375,50) v0.add(String(-10,-4,'10',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,279.375,89.0625) v0.add(String(-10,-4,'20',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,279.375,128.125) v0.add(String(-10,-4,'30',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1))) v0=self._nn(Group()) v0.transform = (1,0,0,1,279.375,167.1875) v0.add(String(-10,-4,'40',textAnchor='start',fontName='Times-Roman',fontSize=10,fillColor=Color(0,0,0,1)))
def create_row_colors(self, rows): """ create 2d list containing colors for each row in table. """ colors = [] for index, _ in enumerate(rows): if index < 2: continue elif index % 2: colors.append(('BACKGROUND', (0, index), (4, index), Color(red=220 / 255, green=230 / 255, blue=241))) return colors
def _draw_chart_rect(d, x, y, width, height, format_json): r = Rect(x, y, width, height, fillColor=Color(0.95, 0.95, 0.95, 1)) d.add(r) width += 40 height += 40 text_format = { "rect": [int(width / 2), int(height / 2), width, height], "content": "暂无数据", "position": "middle", "font_name": DefaultFontName, "font_size": 30, "font_color": Color(0.5, 0.5, 0.5, 1) } t = PDFTemplate._draw_text(text_format) d.add(t) if "main_title" in format_json: text_format = { "rect": [0, height, width, height - 15], "content": format_json['main_title'], "position": "start", "font_name": DefaultFontName, "font_size": 15, "font_color": Color(0.5, 0.5, 0.5, 1) } if "main_title_font_name" in format_json: text_format['font_name'] = format_json['main_title_font_name'] if "main_title_font_size" in format_json: text_format['font_size'] = format_json['main_title_font_size'] if "main_title_font_color" in format_json: text_format['font_color'] = format_json[ 'main_title_font_color'] main_title = PDFTemplate._draw_text(text_format) d.add(main_title)
def __init__(self, width=400, height=200, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) self.transform = (1, 0, 0, 1, 0, 0) self.add( Rect(50, 50, 300, 100, rx=0, ry=0, fillColor=Color(1, 1, 0, 1), fillOpacity=None, strokeColor=Color(0, 0, 0, 1), strokeWidth=1, strokeLineCap=0, strokeLineJoin=0, strokeMiterLimit=0, strokeDashArray=None, strokeOpacity=None)) self.add( String(180, 100, 'Hello World', textAnchor='start', fontName='Times-Roman', fontSize=10, fillColor=Color(1, 0, 0, 1))) self.add( String( 180, 86, b'Special characters \xc2\xa2\xc2\xa9\xc2\xae\xc2\xa3\xce\xb1\xce\xb2', textAnchor='start', fontName='Times-Roman', fontSize=10, fillColor=Color(1, 0, 0, 1)))
def gen_watermark_pdf(email): """Generates a watermark by tiling email""" # name of the file to save c = canvas.Canvas('watermark.pdf') c.setFontSize(10) red50transparent = Color(100, 0, 0, alpha=0.09) c.setFillColor(red50transparent) c.setFont('Helvetica-Bold', 20) text = (((email + " ") * 20)) textobject = c.beginText(0, 29.7 * cm) for i in range(100): textobject.textLine(text) c.drawText(textobject) return c.getpdfdata()
def make_items(self, content): if DEBUG: style = ParagraphStyle('debug', borderColor=Color(1, 0, 0, 1), borderWidth=1, parent=self.text_style) key_style = ParagraphStyle('debug', borderColor=Color(1, 0, 0, 1), borderWidth=1, parent=self.key_style) else: style = self.text_style key_style = self.key_style txt = str(content).strip() match = Part.pair_matcher.match(txt) if match: key = self.remove_quote(match.group(1).strip()) value = self.remove_quote(match.group(2).strip()) return self.make_paragraphs(key, key_style) + self.make_paragraphs( value, style) else: txt = self.remove_quote(txt) return self.make_paragraphs(txt, style)
def __init__(self, paper=landscape(A3), magLim=3.5, projection="merc", output='output/proj_output_example.pdf', costellation_list='', altaz=0, city='Madrid', date=ephem.now()): self.paper = paper self.altaz = altaz self.observer = ephem.city(city) self.observer.date = date self.s = solarsystem.SolarSystem(self.observer) self.magLim = magLim self.projection = projection self.paperwidth, self.paperheight = self.paper self.c = canvas.Canvas(output, pagesize=self.paper) from reportlab.lib.colors import Color self.c.setFillColor(Color(0., 0., 0., alpha=1)) # self.c.rect(0,0,self.paperwidth,self.paperheight,fill=1,stroke=0) if altaz != 0: ra, dec = self.observer.radec_of(0, '90') ra = ra * 180 / pi dec = dec * 180 / pi zenit = " +lon_0=" + str(ra) + " +lat_0=" + str(dec) print zenit self.prj = pyproj.Proj("+proj=" + self.projection + zenit) else: self.prj = pyproj.Proj("+proj=" + self.projection) self.prj_ra_dec = pyproj.Proj("+proj=lonlat +ellps=sphere") self.xfactor = 5.8 self.yfactor = self.xfactor * self.paperheight / self.paperwidth self.scale = 1 (self.lonmin, self.latmin), ( self.lonmax, self.latmax) = self.getCostellationsLimits(costellation_list) print(self.lonmin, self.latmin), (self.lonmax, self.latmax) x0, y0 = self.p(self.lonmin, self.latmin) x1, y1 = self.p(self.lonmax, self.latmax) self.scale = max((x1 - x0) / self.paperwidth, (y1 - y0) / self.paperheight) print x0, y0, self.scale
def write_texture_to_page(c, texture, scalef=cm): # compensate for different coordinate systems texture = texture.transpose(PIL.Image.FLIP_TOP_BOTTOM) for x in xrange(texture.size[0]): for y in xrange(texture.size[1]): (r, g, b, a) = texture.getpixel((x, y)) pixelcolor = Color(r / 255., g / 255., b / 255., alpha=a / 255.) c.setFillColor(pixelcolor) c.setStrokeColor(pixelcolor) #print("Created rect at %d,%d with fill color %d,%d,%d" % (x, y, r, g, b)) c.rect(scalef * x, scalef * y, scalef, scalef, stroke=True, fill=True)
def alpha(canvas): red50transparent = Color(100, 0, 0, alpha=0.5) c = canvas c.setFillColor(black) c.setFont("Helvetica", 10) c.drawString(25, 180, "solid") c.setFillColor(blue) c.rect(25, 25, 100, 100, fill=True, stroke=False) c.setFillColor(red) c.rect(100, 75, 100, 100, fill=True, stroke=False) c.setFillColor(black) c.drawString(225, 180, "transparent") c.setFillColor(blue) c.rect(225, 25, 100, 100, fill=True, stroke=False) c.setFillColor(red50transparent) c.rect(300, 75, 100, 100, fill=True, stroke=False)
def detalle_lineas(p, header, sizes, lineas): tw = width - 2*margin table = Table(header + lineas, colWidths=[size * tw for size in sizes]) table.setStyle([ ('FONT', (0, 0), (-1, -1), font_std), ('FONT', (0, 0), (-1, 0), font_bld), ('LEADING', (0, 1), (-1, -1), 5), ('GRID', (0, 0), (-1, 0), 0.5, black), ('BACKGROUND', (0, 0), (-1, 0), Color(0.8,0.8,0.8)), ('ALIGN', (1, 0), (-1, -1), 'CENTRE'), ('ALIGN', (1, 1), (-3, -1), 'RIGHT'), ('ALIGN', (3, 1), (-1, -1), 'RIGHT'), ('FONTSIZE',(0,0),(-1,-1),9), ]) mw, mh = table.wrapOn(p, width, height) table.drawOn(p, margin, height - 99*mm - mh)
def __init__(self, width=400, height=200, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) self.transform = (1, 0, 0, 1, 0, 0) self.add( Wedge(200, 100, 75, -176.4, 90, yradius=75, annular=False, fillColor=Color(.27451, .509804, .705882, 1), fillOpacity=None, strokeColor=Color(0, 0, 0, 1), strokeWidth=1, strokeLineCap=0, strokeLineJoin=1, strokeMiterLimit=0, strokeDashArray=None, strokeOpacity=None)) self.add( Wedge(200, 100, 75, -180, -176.4, yradius=75, annular=False, fillColor=Color(.847059, .74902, .847059, 1), fillOpacity=None, strokeColor=Color(0, 0, 0, 1), strokeWidth=1, strokeLineCap=0, strokeLineJoin=1, strokeMiterLimit=0, strokeDashArray=None, strokeOpacity=None)) self.add( Wedge(200, 100, 75, -270, -180, yradius=75, annular=False, fillColor=Color(.392157, .584314, .929412, 1), fillOpacity=None, strokeColor=Color(0, 0, 0, 1), strokeWidth=1, strokeLineCap=0, strokeLineJoin=1, strokeMiterLimit=0, strokeDashArray=None, strokeOpacity=None))
def drawCostellationsFigures(self): from reportlab.lib.colors import Color self.c.setStrokeColor(Color(0.1, 0.2, 0.7, alpha=0.5)) self.c.setLineWidth(0.2) figures = catalogues.CostellationFigures() costellations = set(map(lambda x: x[0], figures)) for costellation in costellations: data = filter(lambda x: x[0] == costellation, figures)[0] data = list(util.group(data[2:], 2)) for s in data: star1 = self.H.search(s[0]) star2 = self.H.search(s[1]) if star1 != None and star2 != None: costellation_line = ((star1[4] * 180 / pi, star1[5] * 180 / pi), (star2[4] * 180 / pi, star2[5] * 180 / pi)) self.drawLine(costellation_line)
def alpha(canvas): from reportlab.graphics.shapes import Rect from reportlab.lib.colors import Color, black, blue, red red50transparent = Color(100, 0, 0, alpha=0.5) c = canvas c.setFillColor(black) c.setFont('Helvetica', 10) c.drawString(25, 180, 'solid') c.setFillColor(blue) c.rect(25, 25, 100, 100, fill=True, stroke=False) c.setFillColor(red) c.rect(100, 75, 100, 100, fill=True, stroke=False) c.setFillColor(black) c.drawString(225, 180, 'transparent') c.setFillColor(blue) c.rect(225, 25, 100, 100, fill=True, stroke=False) c.setFillColor(red50transparent) c.rect(300, 75, 100, 100, fill=True, stroke=False)
def render_graph(self, request_opts, graph_opts): def label_fmt(x): print "@@@", x return str(x) ld = len(graph_opts["data"]) palette = [Color(*x) for x in get_float_pallete(ld)] w = graph_opts["width"] h = graph_opts["height"] drawing = Drawing(w, h) # Legend legend = LineLegend() legend.colorNamePairs = [ (palette[i], graph_opts["data"][i].name) for i in range(ld) ] legend.boxAnchor = "sw" legend.columnMaximum = 2 legend.alignment = "right" drawing.add(legend) lh = legend._calcHeight() + self.X_PADDING / 2 # Plot lp = LinePlot() lfs = lp.xValueAxis.labels.fontSize lp.x = self.X_PADDING lp.y = self.Y_PADDING + lh + lfs lp.width = w - 2 * self.X_PADDING lp.height = h - self.Y_PADDING - lp.y lp.data = [ [(t, v) for v, t in ts] for ts in graph_opts["data"] ] for i in range(ld): lp.lines[i].strokeColor = palette[i] drawing.add(lp) # Render cdata = drawing.asString(format="png") response = self.render_plain_text( cdata, mimetype="image/png" ) if not request_opts["noCache"]: cache.set(request_opts["requestKey"], response, request_opts["cacheTimeout"]) return response
def extractData(table): spanArr = [] dataArr = [] backArr = [] for row in table['rows']: rowData = [] spanData = [] backData = [] for col in row['cols']: # rowData.append(Paragraph(col['data'])) # rowData.append(col['data']) spanData.append(col['styles']['colspan']) style = {'name': 'datax'} if 'color' in col['styles']: fc = col['styles']['color'] style['textColor'] = Color(fc[0], fc[1], fc[2]) style['alignment'] = TA_CENTER style['fontSize'] = 10 style['leading'] = 10 pStyle = ParagraphStyle(**style) p = Paragraph(col['data'], pStyle) rowData.append(p) # rowData.append(col['data']) # if we don't go for styles, etc if 'background-color' in col['styles']: c = col['styles']['background-color'] # print(bc) backData.append(c) else: backData.append(0) # spanData.append(col['styles']['colspan']) # should have appended only once, but repeating # to take advantage of reportlab table SPAN spans = col['styles']['colspan'] for s in range(1, spans): rowData.append("NULL") spanData.append(0) backData.append(0) dataArr.append(rowData) spanArr.append(spanData) backArr.append(backData) return dataArr, spanArr, backArr
def main(): # To do: parse some useful arguments as rendering options here # e.g. outline color, page size, etc. # # For now, put these options into variables here: bg_color = Color(1.0, 1.0, 1.0, alpha=1.0) # For now just assume a list of files infns = os.listdir(sys.argv[1]) infns.sort() for infn in infns: # File names if infn[-7:] != '.pickle': print 'Ignoring file %s, because its not a pickle...' % (infn) continue outfn = string.replace(infn, '.pickle', '.pdf') outfn = os.path.basename(outfn) # Put output in this dir print 'Processing %s to generate %s' % (infn, outfn) # Import data data = importPickle(infn) if not data: print "Problem importing data!" return # Create a pdf canvas thing pdf = MyPDFGenerator(outfn, data, bg_color) # Get the bounding square of the colony to size the image # This will resize the image to fit the page... # ** alternatively you can specify a fixed world size here '''(w,h) = pdf.computeBox() sqrt2 = math.sqrt(2) world = (w/sqrt2,h/sqrt2)''' #world = (250,250) world = (1000, 1000) # Page setup page = (20, 20) center = (0, 0) # Render pdf print 'Rendering PDF output to %s' % outfn pdf.draw_frame(outfn, world, page, center)