def get_pdfcolor(self, color): pdfcolor = None alpha = color[2] if self.use_spot and color[0] == uc2const.COLOR_SPOT: c, m, y, k = self.cms.get_cmyk_color(color)[1] spotname = color[3] if spotname == uc2const.COLOR_REG: spotname = 'All' pdfcolor = CMYKColorSep(c, m, y, k, spotName=spotname, alpha=alpha) elif self.colorspace == uc2const.COLOR_CMYK: c, m, y, k = self.cms.get_cmyk_color(color)[1] pdfcolor = CMYKColor(c, m, y, k, alpha=alpha) elif self.colorspace == uc2const.COLOR_RGB: r, g, b = self.cms.get_rgb_color(color)[1] return Color(r, g, b, alpha) elif self.colorspace == uc2const.COLOR_GRAY: gray = self.cms.get_grayscale_color(color) k = 1.0 - gray[1][0] c = m = y = 0.0 pdfcolor = CMYKColor(c, m, y, k, alpha=alpha) else: if color[0] == uc2const.COLOR_RGB: r, g, b = color[1] return Color(r, g, b, alpha) elif color[0] == uc2const.COLOR_GRAY: k = 1.0 - color[1][0] c = m = y = 0.0 pdfcolor = CMYKColor(c, m, y, k, alpha=alpha) else: c, m, y, k = self.cms.get_cmyk_color(color)[1] pdfcolor = CMYKColor(c, m, y, k, alpha=alpha) self.set_rgb_values(color, pdfcolor) return pdfcolor
def draw_guide(canvas, x, y, guide): canvas.setDash(1, 2) canvas.setStrokeColor(CMYKColor(0, 0, 0, 0.2)) if guide == Guide.STAR: x1 = x y1 = y x2 = x1 + SQUARE_SIZE y2 = y - SQUARE_SIZE canvas.line(x1, y1, x2, y2) canvas.line(x2, y1, x1, y2) elif guide == Guide.CROSS: x1 = x y1 = y - SQUARE_SIZE / 2 x2 = x1 + SQUARE_SIZE y2 = y1 canvas.line(x1, y1, x2, y2) x1 = x + SQUARE_SIZE / 2 y1 = y x2 = x1 y2 = y1 - SQUARE_SIZE canvas.line(x1, y1, x2, y2) canvas.setDash() canvas.setStrokeColor(CMYKColor(0, 0, 0, 1))
def plot_glyph(font, name, canvas, orig): if canvas is None: return from reportlab.lib.colors import CMYKColor gs = font.getGlyphSet() glyph = gs[name] pen = PDFPen(gs, canvas.beginPath()) glyph.draw(pen) if orig: color = CMYKColor(0, 1, 1, 0, alpha=.5) else: color = CMYKColor(0, 0, 0, 1, alpha=.5) x0 = 100 y0 = 100 scale = 0.25 canvas.saveState() canvas.setFillColor(color) canvas.translate(x0, y0) canvas.scale(scale, scale) canvas.drawPath(pen.path, stroke=False, fill=True) canvas.restoreState() if not orig: canvas.showPage()
def draw_guide(canvas, x, y, guide, working_dir, character_info): if guide == Guide.CHARACTER: prefill_character(working_dir, canvas, x + SQUARE_PADDING, \ y - SQUARE_PADDING, \ character_info.character + '0.png') return canvas.setDash(1, 2) canvas.setStrokeColor(CMYKColor(0, 0, 0, 0.2)) if guide == Guide.STAR or guide == Guide.CROSS_STAR: x1 = x y1 = y x2 = x1 + SQUARE_SIZE y2 = y - SQUARE_SIZE canvas.line(x1, y1, x2, y2) canvas.line(x2, y1, x1, y2) if guide == Guide.CROSS or guide == Guide.CROSS_STAR: x1 = x y1 = y - SQUARE_SIZE / 2 x2 = x1 + SQUARE_SIZE y2 = y1 canvas.line(x1, y1, x2, y2) x1 = x + SQUARE_SIZE / 2 y1 = y x2 = x1 y2 = y1 - SQUARE_SIZE canvas.line(x1, y1, x2, y2) canvas.setDash() canvas.setStrokeColor(CMYKColor(0, 0, 0, 1))
def stdColors(t, b, f): if isinstance(f, CMYKColor) or isinstance(t, CMYKColor) or isinstance( b, CMYKColor): return (t or CMYKColor(0, 0, 0, 0.9), b or CMYKColor(0, 0, 0, 0.9), f or CMYKColor(0.12, 0.157, 0, 0)) else: return (t or Color(0.1, 0.1, 0.1), b or Color(0.1, 0.1, 0.1), f or Color(0.8, 0.843, 1))
def _rgbFind(self, color): "see if it matches any existing color in my list" C = self.cmykColors if isinstance(color, (list, tuple)): if len(color) == 3: color = Color(color[0], color[1], color[2]) elif len(color) == 4: color = CMYKColor(color[0], color[1], color[2], color[3]) else: raise ValueError("bad color %s" % repr(color)) isCMYK = isinstance(color, CMYKColor) if not isCMYK: if isinstance(color, str): color = toColor(color) if colorDistance(color, black) < 1e-8: isCMYK = 1 color = PCMYKColor(0, 0, 0, 100, 100) elif colorDistance(color, white) < 1e-8: isCMYK = 1 color = PCMYKColor(0, 0, 0, 0, 100) rgb = color.red, color.green, color.blue if isCMYK: if color not in C: C.append(color) return self._setCMYKColor(color) else: for c in C: if (c.red, c.green, c.blue) == rgb: return self._setCMYKColor(c) return '%s setrgbcolor' % fp_str(rgb)
def draw_square(c, x, y, style): """ Draw a single character square with specified style. """ w = style['sqwidth'] h = style['sqheight'] # draw guides first so they are under the outline strokes c.setStrokeColor(style['guidecolor']) c.setLineWidth(style['guidewidth']) c.setDash(style['guidedash']) if style['guidestyle'] in ['+', '*']: c.line(x + w / 2, y, x + w / 2, y + h) c.line(x, y + h / 2, x + w, y + h / 2) if style['guidestyle'] in ['X', '*']: c.line(x, y, x + w, y + h) c.line(x, y + h, x + w, y) if style['guidestyle'] == '#': c.line(x, y + h / 3, x + w, y + h / 3) c.line(x, y + 2 * h / 3, x + w, y + 2 * h / 3) c.line(x + w / 3, y, x + w / 3, y + h) c.line(x + 2 * w / 3, y, x + 2 * w / 3, y + h) # outline c.setStrokeColor(style['outlinecolor']) c.setLineWidth(style['outlinewidth']) c.setFillColor(CMYKColor(black=0, alpha=0)) c.setDash() c.rect(x, y, w, h, fill=0)
def _draw_header(self): # load the header logo and get its width for later nav_logo = svg2rlg("helpdful/{}".format(self.theme["images"]["logo"])) scaled_nav_logo = utils.scale( nav_logo, scaling_factor=self.theme["header"]["logo"]["scale"]) # create a paragraph to calculate header height later header = Paragraph(utils.type_to_title(self.data["soknadstype"]), self.style["header"]) header_text_boundary = self.page_width - ( (self.theme["page_margin"] * 2) + scaled_nav_logo.width + self.theme["header"]["logo"]["margin_right"]) w, h = header.wrap(header_text_boundary, self.theme["header"]["base_height"]) # draw the header rectangle self.canvas.setFillColor( CMYKColor(*self.theme["header"]["background"])) self.y_position = self.y_position - self.theme["header"][ "base_height"] - h self.canvas.rect( 0, self.y_position, self.page_width, self.theme["header"]["base_height"] + h, stroke=0, fill=1, ) # draw a logo and the text renderPDF.draw(scaled_nav_logo, self.canvas, self.theme["page_margin"], 806 - (h / 2)) # TODO: Fix magical value 806 :( header.drawOn( self.canvas, self.theme["page_margin"] + scaled_nav_logo.width + self.theme["header"]["logo"]["margin_right"], 820 - h, ) # TODO: Fix magical value 820 :( self.y_position -= self.theme["header"]["margin_bottom"]
def main(): LP = tuple( enumerate((None, 'top', 'top_right', 'right', 'bottom_right', 'bottom', 'bottom_left', 'left', 'top_left'))) HLEGS = tuple(enumerate((None, False, True))) LEGENDMODES = tuple(enumerate((None, 'fullWidth', 'fullHeight'))) ALLOWED_CHART_TYPES.sort() html = [] for chartType in ALLOWED_CHART_TYPES: makeDrawing(html, '', chartType=chartType) makeDrawing(html, "0.1 Line chart should have both x and y gridlines", chartType='linechart', yAxisVisible=1, yAxisGridLines=1, xAxisVisible=1, xAxisGridLines=1) makeDrawing(html, "0.11 Line plot should have both x and y gridlines", chartType='lineplot', yAxisVisible=1, yAxisGridLines=1, xAxisVisible=1, xAxisGridLines=1, data=[[1.5, 3, 4.5], [1, 2, 3], [1.5, 2.5, 3.5]]) makeDrawing( html, "0.12 0.1+yAxisLabelTextFormat='$%(decfmt(value*10000,2,'.',''))s'", chartType='linechart', yAxisVisible=1, yAxisGridLines=1, xAxisVisible=1, xAxisGridLines=1, yAxisLabelTextFormat="$%(decfmt(value*10000,2,'.',''))s") makeDrawing( html, "0.13 0.1+yAxisLabelTextFormat='$%(decfmt(value*10000,-2,'.',','))s'", chartType='linechart', yAxisVisible=1, yAxisGridLines=1, xAxisVisible=1, xAxisGridLines=1, yAxisLabelTextFormat="$%(decfmt(value*10000,-2,'.',','))s") makeDrawing( html, "0.14 0.1+yAxisLabelTextFormat='$%(decfmt(value*10000,(1 if notAllInt else 0),'.',','))s'", chartType='linechart', yAxisVisible=1, yAxisGridLines=1, xAxisVisible=1, xAxisGridLines=1, yAxisLabelTextFormat= "$%(decfmt(value*10000,(1 if notAllInt else 0),'.',','))s") makeDrawing( html, "0.15 0.1+yAxisLabelTextFormat='$%(decfmt(value*10000,2,',','.'))s'", chartType='linechart', yAxisVisible=1, yAxisGridLines=1, xAxisVisible=1, xAxisGridLines=1, yAxisLabelTextFormat="$%(decfmt(value*10000,2,',','.'))s") makeDrawing( html, '1.1 Clustered_bar with the yAxisVisible and yAxisGridlines on', chartType='clustered_bar', yAxisVisible=1, yAxisGridLines=1, xAxisVisible=0) makeDrawing(html, '1.2 Stacked_bar with the yAxisVisible and yAxisGridlines on', chartType='stacked_bar', yAxisVisible=1, yAxisGridLines=1, xAxisVisible=0) makeDrawing( html, '1.3 Clustered_bar with the xAxisVisible and xAxisGridlines on', chartType='clustered_bar', xAxisVisible=1, xAxisGridLines=1, yAxisVisible=0) makeDrawing(html, '1.4 Stacked_bar with the xAxisVisible and xAxisGridlines on', chartType='stacked_bar', xAxisVisible=1, xAxisGridLines=1, yAxisVisible=0) makeDrawing( html, '2.1 Clustered_bar with both the x Axis and y Axis invisible - should resize correctly', chartType='stacked_bar', xAxisVisible=0, yAxisVisible=0, showBoundaries=1) makeDrawing( html, '2.2 Stacked_bar with both the x Axis and y Axis invisible - should resize correctly', chartType='clustered_bar', xAxisVisible=0, yAxisVisible=0, showBoundaries=1) makeDrawing(html, '3.1 Stacked_bar with dataLabelsType set to None', chartType='stacked_bar', dataLabelsType=None) makeDrawing(html, '3.2 Clustered_bar with dataLabelsType set to None', chartType='clustered_bar', dataLabelsType=None) makeDrawing(html, '3.3 Pie with dataLabelsType set to None', chartType='pie', dataLabelsType=None) makeDrawing(html, "3.4 Stacked_bar with dataLabelsType set to 'values'", chartType='stacked_bar', dataLabelsType='values') makeDrawing(html, "3.5 Clustered_bar with dataLabelsType set to 'values'", chartType='clustered_bar', dataLabelsType='values') makeDrawing(html, "3.6 Pie with dataLabelsType set to 'values'", chartType='pie', dataLabelsType='values') makeDrawing(html, "3.7 Stacked_bar with dataLabelsType set to 'percent'", chartType='stacked_bar', dataLabelsType='percent') makeDrawing(html, "3.8 Clustered_bar with dataLabelsType set to 'percent'", chartType='clustered_bar', dataLabelsType='percent') makeDrawing(html, "3.9 Pie with dataLabelsType set to 'percent'", chartType='pie', dataLabelsType='percent') makeDrawing(html, "3.71 Stacked_bar with dataLabelsType set to 'percent,2'", chartType='stacked_bar', dataLabelsType='percent,2') makeDrawing(html, "3.81 Clustered_bar with dataLabelsType set to 'percent,2'", chartType='clustered_bar', dataLabelsType='percent,2') makeDrawing(html, "3.91 Pie with dataLabelsType set to 'percent,2'", chartType='pie', dataLabelsType='percent,2') makeDrawing( html, "3.72 Stacked_bar with dataLabelsType set to '%(percent).1f%%'", chartType='stacked_bar', dataLabelsType='%(percent).1f%%') makeDrawing( html, "3.82 Clustered_bar with dataLabelsType set to '%(percent).2f%%'", chartType='clustered_bar', dataLabelsType='%(percent).2f%%') makeDrawing( html, "3.92 Pie with dataLabelsType set to '%(percent).3f%% per Annum'", chartType='pie', dataLabelsType='%(percent).3f%% per Annum') makeDrawing( html, "3.73 Stacked_bar with dataLabelsType set to '$%(decfmt(value*10000,2,'.',''))s'", chartType='stacked_bar', dataLabelsType='$%(decfmt(value*10000,2,\'.\',\'\'))s') makeDrawing( html, "3.74 Stacked_bar with dataLabelsType set to '$%(decfmt(value*10000,2,'.',','))s'", chartType='stacked_bar', dataLabelsType='$%(decfmt(value*10000,2,\'.\',\',\'))s') makeDrawing( html, "3.83 Clustered_bar with dataLabelsType set to '$%(decfmt(value*14000,2,',','.'))s'", chartType='clustered_bar', dataLabelsType='$%(decfmt(value*14000,1,\',\',\'.\'))s') makeDrawing( html, "3.93 Pie with dataLabelsType set to '$%(decfmt(value*12000,2))s/Year'", chartType='pie', dataLabelsType='$%(decfmt(value*12000,2))s/Year') makeDrawing( html, "4.1 Pie with dataLabelsType unset - no datalabels should be printed", chartType='pie') makeDrawing( html, "4.2 Pie with dataLabelsType set to 'values' - values should be used for dataLabels", chartType='pie', dataLabelsType='values') makeDrawing( html, "4.3 Pie with dataLabelsType set to 'percent' - percentages should be used for dataLabels", chartType='pie', dataLabelsType='percent') makeDrawing( html, "4.4 Pie with with category names - set dataLabelsType '%(category)s'", chartType='pie', dataLabelsType='%(category)s', categoryNames=['A', 'B', 'C', 'D']) makeDrawing(html, "4.5.0 Pie with overlap", chartType='pie', dataLabelsType='%(percent).3f%% per Annum', data=[0.9, 1.1, 2.2, 40, 57], categoryNames=['0.9', '1.1', '2.2', '40', '57']) makeDrawing(html, "4.5.1 Pie with overlap orderMode='alternate'", chartType='pie', orderMode='alternate', dataLabelsType='%(percent).3f%% per Annum', data=[0.9, 1.1, 2.2, 40, 57], categoryNames=['0.9', '1.1', '2.2', '40', '57']) makeDrawing(html, "4.5.2 Pie with overlap checkLabelOverlap=1", chartType='pie', checkLabelOverlap=1, dataLabelsType='%(percent).3f%% per Annum', data=[0.9, 1.1, 2.2, 40, 57], categoryNames=['0.9', '1.1', '2.2', '40', '57']) makeDrawing( html, "4.5.3 Pie with overlap orderMode='alternate' checkLabelOverlap=1", chartType='pie', checkLabelOverlap=1, orderMode='alternate', dataLabelsType='%(percent).3f%% per Annum', data=[0.9, 1.1, 2.2, 40, 57], categoryNames=['0.9', '1.1', '2.2', '40', '57']) makeDrawing(html, "5.1 Title should be (ascent * 1.5) from the font base line ", chartType='pie', titleText="This is the Title") makeDrawing( html, "6.1 Bar with integer data - axis should display without decimals", chartType='bar', data=[[100, 200, 300, 400]], xAxisVisible=1, yAxisVisible=1) makeDrawing( html, "6.2 Column with integer data - axis should display without decimals", chartType='column', data=[[100, 200, 300, 400]], xAxisVisible=1, yAxisVisible=1) makeDrawing( html, "6.3 Bar with floating point data - axis should display with decimals", chartType='bar', data=[[0.01, 0.02, 0.03, 0.04]], xAxisVisible=1, yAxisVisible=1) makeDrawing( html, "6.4 Bar with floating point data - axis should display with decimals", chartType='column', data=[[0.01, 0.02, 0.03, 0.04]], xAxisVisible=1, yAxisVisible=1) makeDrawing(html, "7.1 x Axis and y Axis gridlines should be the same width", chartType='bar', xAxisVisible=1, yAxisVisible=1, xAxisGridLines=1, yAxisGridLines=1) makeDrawing(html, "7.2 x Axis and y Axis gridlines should be the same width", chartType='column', xAxisVisible=1, yAxisVisible=1, xAxisGridLines=1, yAxisGridLines=1) makeDrawing( html, "8.1 When using data = [[120,20]], the value axis should no longer show negative values of (-50,0,50,100,150)", chartType='column', data=[[120, 20]]) makeDrawing( html, "8.1a When using data = [[120,20]], the value axis should no longer show negative values of (-50,0,50,100,150)", chartType='bar', data=[[120, 20]]) makeDrawing( html, "8.2 When using negative data the gridline of the category axis should be correctly sized", chartType='column', data=[[-120, -20, 20]], xAxisGridLines=1) makeDrawing( html, "8.2a When using negative data the gridline of the category axis should be correctly sized", chartType='bar', data=[[-120, -20, 20]], yAxisGridLines=1) for k, legendMode in LEGENDMODES: for j, hleg in HLEGS: for i, p in LP: makeDrawing( html, "9.1.%d.%d.%d Blue border round background, no fill on background legendPos=%s hleg=%s legendMode=%s" % (k, j, i, p, hleg, legendMode), chartType='column', bgColor=None, bgStrokeColor='blue', seriesNames=('A long series name', 'Another very long series name'), legendPos=p, hleg=hleg, legendMaxWFrac=0.5, legendMaxHFrac=0.5, legendMode=legendMode) makeDrawing(html, "9.2 Blue border round background, yellow fill on background", chartType='bar', bgColor='yellow', bgStrokeColor='blue') makeDrawing( html, "10.1 Test piechart with data of '[[230,340]]' and dataLabelsType of 'percent' (as per Nardi's test)", chartType='pie', data=[[230, 340]], categoryNames=['category1', 'category2'], seriesNames=[], bgColor=None, plotColor=CMYKColor(0, 0, 1, 0), legendPos='left', legendFontName='Helvetica', legendFontColor=CMYKColor(0, 0, 0, 2), titleText='This is the main title', titleFontName='Helvetica-Bold', titleFontSize=18, titleFontColor=CMYKColor(0, 1, 1, 1), dataLabelsType='percent', dataLabelsFontName='Helvetica', dataLabelsFontSize=14, dataLabelsFontColor=CMYKColor(0, 1, 1, 0)) for k, legendMode in LEGENDMODES: for j, hleg in HLEGS: for i, p in LP: makeDrawing( html, "10.2.%d.%d.%d piechart with more than 10 slices legendPos=%s hleg=%s legendMode=%s" % (k, j, i, p, hleg, legendMode), chartType='exploded_pie', data=[[ 27.00000, 3.00000, 10.00000, 5.00000, 5.00000, 15.00000, 35.00000, 12, 17, 11, 19, 23, 32 ]], categoryNames=[ 'Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5', 'Category 6', 'Category 7', 'Cat 8', 'Cat 9', 'Cat 10', 'Cat 11', 'Cat 12', 'Cat 13' ], seriesNames=[], chartColors=[ CMYKColor(0.00, 1.00, 0.00, 0.00), CMYKColor(1.00, 1.00, 0.00, 0.00), CMYKColor(0.00, 0.00, 1.00, 0.00), CMYKColor(0.00, 1.00, 1.00, 0.00), CMYKColor(0.00, 0.80, 1.00, 0.00), CMYKColor(0.00, 0.40, 1.00, 0.00), CMYKColor(0.80, 0.00, 1.00, 0.00), toColor('red'), toColor('grey'), toColor('brown'), toColor('magenta'), toColor('darkblue'), toColor('pink') ], bgColor=None, plotColor=None, legendPos=p, legendFontName='Helvetica', legendFontSize=9.00, legendFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), titleText='', titleFontName='Helvetica-Bold', titleFontSize=14.00, titleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), dataLabelsType='%.1f', dataLabelsFontName='Helvetica', dataLabelsFontSize=9.00, dataLabelsFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), width=401.54384, height=150.86375, hleg=hleg, legendMaxWFrac=0.5, legendMaxHFrac=0.5, legendMode=legendMode) makeDrawing(html, "11.1 Test for black lines in 3D exploded piechart", chartType='exploded_pie3d', data=[[ 27.00000, 3.00000, 10.00000, 5.00000, 5.00000, 15.00000, 35.00000 ]], categoryNames=[ 'Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5', 'Category 6', 'Category 7' ], seriesNames=[], chartColors=[ CMYKColor(0.00, 1.00, 0.00, 0.00), CMYKColor(1.00, 1.00, 0.00, 0.00), CMYKColor(0.00, 0.00, 1.00, 0.00), CMYKColor(0.00, 1.00, 1.00, 0.00), CMYKColor(0.00, 0.80, 1.00, 0.00), CMYKColor(0.00, 0.40, 1.00, 0.00), CMYKColor(0.80, 0.00, 1.00, 0.00) ], bgColor=None, plotColor=None, legendPos='right', legendFontName='Helvetica', legendFontSize=9.00, legendFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), titleText='', titleFontName='Helvetica-Bold', titleFontSize=14.00, titleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), dataLabelsType='%.1f', dataLabelsFontName='Helvetica', dataLabelsFontSize=9.00, dataLabelsFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), width=401.54384, height=150.86375) for k, legendMode in LEGENDMODES: for j, hleg in HLEGS: for i, p in LP: makeDrawing( html, "11.2.%d.%d.%d 3D exploded piechart with more than 10 slices legendPos=%s hleg=%s legendMode=%s" % (k, j, i, p, hleg, legendMode), chartType='exploded_pie3d', data=[[ 27.00000, 3.00000, 10.00000, 5.00000, 5.00000, 15.00000, 35.00000, 12, 17, 11, 19, 23, 32 ]], categoryNames=[ 'Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5', 'Category 6', 'Category 7', 'Cat 8', 'Cat 9', 'Cat 10', 'Cat 11', 'Cat 12', 'Cat 13' ], seriesNames=[], chartColors=[ CMYKColor(0.00, 1.00, 0.00, 0.00), CMYKColor(1.00, 1.00, 0.00, 0.00), CMYKColor(0.00, 0.00, 1.00, 0.00), CMYKColor(0.00, 1.00, 1.00, 0.00), CMYKColor(0.00, 0.80, 1.00, 0.00), CMYKColor(0.00, 0.40, 1.00, 0.00), CMYKColor(0.80, 0.00, 1.00, 0.00), toColor('red'), toColor('grey'), toColor('brown'), toColor('magenta'), toColor('darkblue'), toColor('pink') ], bgColor=None, plotColor=None, legendPos=p, legendFontName='Helvetica', legendFontSize=9.00, legendFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), titleText='', titleFontName='Helvetica-Bold', titleFontSize=14.00, titleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), dataLabelsType='%.1f', dataLabelsFontName='Helvetica', dataLabelsFontSize=9.00, dataLabelsFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), width=401.54384, height=150.86375, hleg=hleg, legendMaxWFrac=0.5, legendMaxHFrac=0.5, legendMode=legendMode) makeDrawing( html, "11.211 3D exploded piechart with more than 10 slices legend at top legendMaxHFrac=0.125", chartType='exploded_pie3d', legendMaxHFrac=0.125, data=[[ 27.00000, 3.00000, 10.00000, 5.00000, 5.00000, 15.00000, 35.00000, 12, 17, 11, 19, 23, 32 ]], categoryNames=[ 'Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5', 'Category 6', 'Category 7', 'Cat 8', 'Cat 9', 'Cat 10', 'Cat 11', 'Cat 12', 'Cat 13' ], seriesNames=[], chartColors=[ CMYKColor(0.00, 1.00, 0.00, 0.00), CMYKColor(1.00, 1.00, 0.00, 0.00), CMYKColor(0.00, 0.00, 1.00, 0.00), CMYKColor(0.00, 1.00, 1.00, 0.00), CMYKColor(0.00, 0.80, 1.00, 0.00), CMYKColor(0.00, 0.40, 1.00, 0.00), CMYKColor(0.80, 0.00, 1.00, 0.00), toColor('red'), toColor('grey'), toColor('brown'), toColor('magenta'), toColor('darkblue'), toColor('pink') ], bgColor=None, plotColor=None, legendPos='top', legendFontName='Helvetica', legendFontSize=9.00, legendFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), titleText='', titleFontName='Helvetica-Bold', titleFontSize=14.00, titleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), dataLabelsType='%.1f', dataLabelsFontName='Helvetica', dataLabelsFontSize=9.00, dataLabelsFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), width=401.54384, height=150.86375) makeDrawing( html, "11.212 3D exploded piechart with more than 10 slices legend at top legendMaxHFrac=0.09", chartType='exploded_pie3d', legendMaxHFrac=0.09, data=[[ 27.00000, 3.00000, 10.00000, 5.00000, 5.00000, 15.00000, 35.00000, 12, 17, 11, 19, 23, 32 ]], categoryNames=[ 'Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5', 'Category 6', 'Category 7', 'Cat 8', 'Cat 9', 'Cat 10', 'Cat 11', 'Cat 12', 'Cat 13' ], seriesNames=[], chartColors=[ CMYKColor(0.00, 1.00, 0.00, 0.00), CMYKColor(1.00, 1.00, 0.00, 0.00), CMYKColor(0.00, 0.00, 1.00, 0.00), CMYKColor(0.00, 1.00, 1.00, 0.00), CMYKColor(0.00, 0.80, 1.00, 0.00), CMYKColor(0.00, 0.40, 1.00, 0.00), CMYKColor(0.80, 0.00, 1.00, 0.00), toColor('red'), toColor('grey'), toColor('brown'), toColor('magenta'), toColor('darkblue'), toColor('pink') ], bgColor=None, plotColor=None, legendPos='top', legendFontName='Helvetica', legendFontSize=9.00, legendFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), titleText='', titleFontName='Helvetica-Bold', titleFontSize=14.00, titleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), dataLabelsType='%.1f', dataLabelsFontName='Helvetica', dataLabelsFontSize=9.00, dataLabelsFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), width=401.54384, height=150.86375) makeDrawing( html, "11.213 3D exploded piechart with more than 10 slices legend at top legendMaxWFrac=0.8", chartType='exploded_pie3d', legendMaxWFrac=0.8, data=[[ 27.00000, 3.00000, 10.00000, 5.00000, 5.00000, 15.00000, 35.00000, 12, 17, 11, 19, 23, 32 ]], categoryNames=[ 'Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5', 'Category 6', 'Category 7', 'Cat 8', 'Cat 9', 'Cat 10', 'Cat 11', 'Cat 12', 'Cat 13' ], seriesNames=[], chartColors=[ CMYKColor(0.00, 1.00, 0.00, 0.00), CMYKColor(1.00, 1.00, 0.00, 0.00), CMYKColor(0.00, 0.00, 1.00, 0.00), CMYKColor(0.00, 1.00, 1.00, 0.00), CMYKColor(0.00, 0.80, 1.00, 0.00), CMYKColor(0.00, 0.40, 1.00, 0.00), CMYKColor(0.80, 0.00, 1.00, 0.00), toColor('red'), toColor('grey'), toColor('brown'), toColor('magenta'), toColor('darkblue'), toColor('pink') ], bgColor=None, plotColor=None, legendPos='top', legendFontName='Helvetica', legendFontSize=9.00, legendFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), titleText='', titleFontName='Helvetica-Bold', titleFontSize=14.00, titleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), dataLabelsType='%.1f', dataLabelsFontName='Helvetica', dataLabelsFontSize=9.00, dataLabelsFontColor=CMYKColor(0.05, 0.45, 1.00, 0.00), width=401.54384, height=150.86375) makeDrawing(html, "12.1 xAxisLabelAngle=-70", chartType='linechart', dataLabelsType='values', categoryNames=['Oranges', 'Lemons', 'Apples', 'Pears'], xAxisLabelAngle=-70) makeDrawing(html, "12.2 yAxisLabelAngle=-70", chartType='linechart', dataLabelsType='values', categoryNames=['Oranges', 'Lemons', 'Apples', 'Pears'], yAxisLabelAngle=-70) makeDrawing(html, "12.3 xAxisLabelAngle=70", chartType='linechart', dataLabelsType='values', categoryNames=['Oranges', 'Lemons', 'Apples', 'Pears'], xAxisLabelAngle=70) makeDrawing(html, "12.3 yAxisLabelAngle=70", chartType='linechart', dataLabelsType='values', categoryNames=['Oranges', 'Lemons', 'Apples', 'Pears'], yAxisLabelAngle=70) makeDrawing(html, "13.1 textData", chartType='linechart', dataLabelsType='values', textData="A B C D\na b c d") makeDrawing(html, "13.2 textData", chartType='column', dataLabelsType='values', textData="A B C D\na b c d") makeDrawing(html, "13.3 textData", chartType='bar', dataLabelsType='values', textData="A B C D\na b c d") makeDrawing(html, "14.1 Ron Error Empty Chart", chartType='clustered_column', data=[[0], [0], [0]], categoryNames=['Oct'], seriesNames=['Series 1', 'Series 2', 'Series 3'], chartColors=[ CMYKColor(0.00, 0.20, 0.34, 0.00), CMYKColor(0.00, 0.00, 0.40, 0.00), CMYKColor(0.20, 0.00, 0.09, 0.00) ], bgColor=None, plotColor=None, legendPos='right', legendFontName='Helvetica', legendFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), titleText='Banzai!', titleFontName='Helvetica-Bold', titleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), xTitleText='', xTitleFontName='Helvetica', xTitleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), yTitleText='', yTitleFontName='Helvetica', yTitleFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), xAxisVisible=1, xAxisLabelAngle=30, xAxisFontName='Helvetica', xAxisFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), yAxisVisible=1, yAxisFontName='Helvetica', yAxisFontColor=CMYKColor(0.00, 0.00, 0.00, 1.00), xAxisGridLines=0, yAxisGridLines=1, dataLabelsType=None, width=366.00000, height=347.50000) makeDrawing(html, "14.2 Liron Axis error", chartType='linechart', data=[[0.00, 0.00, 0.00, 0.00, -1.07]], categoryNames=['a', 'b', 'c', 'd', 'e'], seriesNames=['Series 1'], chartColors=[ Color(0.82, 0, 0), Color(0.32, 0, 0), Color(0.32, 0.16, 0.06), Color(0.32, 0.16, 0.06), Color(0.32, 0.16, 0.06) ], bgColor=None, plotColor=None, legendPos='right', legendFontName='Times-Roman', legendFontSize=12.00, legendFontColor=Color(0.00, 0.00, 0.00), titleText='', titleFontName='Times-Roman', titleFontSize=12.00, titleFontColor=Color(0.00, 0.00, 0.00), xTitleText='', xTitleFontName='Times-Roman', xTitleFontSize=12.00, xTitleFontColor=Color(0.00, 0.00, 0.00), yTitleText='', yTitleFontName='Times-Roman', yTitleFontSize=12.00, yTitleFontColor=Color(0.00, 0.00, 0.00), xAxisVisible=1, xAxisLabelAngle=30, xAxisFontName='Times-Roman', xAxisFontSize=12.00, xAxisFontColor=Color(0.00, 0.00, 0.00), yAxisVisible=1, yAxisFontName='Times-Roman', yAxisFontSize=12.00, yAxisFontColor=Color(0.00, 0.00, 0.00), xAxisGridLines=0, yAxisGridLines=0, dataLabelsType=None, width=431.00, height=343.00) import os, sys fn = os.path.join('pmout', 'qctests.html') open(fn, 'w').write('<html><head></head><body>%s</body></html>' % ('<hr width=100%>'.join(html))) if sys.platform == 'mac': from reportlab.lib.utils import markfilename markfilename(fn, ext='HTML')
def __process_question(self, question): # TODO: ... make this function pretty self.canvas.setFillColor(CMYKColor(0, 0, 0, 1)) self.canvas.setFont(utils.get_font(600), 10) answer = None if not question["svartype"] == "CHECKBOX_PANEL": infoheader = Paragraph(question["sporsmalstekst"], self.style["infoheader"]) info_w, info_h = infoheader.wrap( self.page_width - (self.theme["page_margin"] * 2), self.theme["question"]["header"]["font_size"], ) infoheader.drawOn(self.canvas, self.theme["page_margin"], self.y_position) self.y_position -= info_h + 5 if question["svartype"] == "PERIODER": answer = "{}–{}".format( utils.isostr_to_norwegian(question["svar"][0]["verdi"]), utils.isostr_to_norwegian(question["svar"][1]["verdi"]), ) elif question["svartype"] == "JA_NEI": checkbox_icon = svg2rlg("helpdful/resources/Checkboks.svg") renderPDF.draw( checkbox_icon, self.canvas, self.theme["page_margin"], self.y_position + self.theme["icon_offset"], ) answer = question["svar"][0]["verdi"].capitalize() elif question["svartype"] == "IKKE_RELEVANT": # TODO: make this answer type behave properly at the end of pages self.y_position += 20 style = self.style["svartype"][question["svartype"]] text = BeautifulSoup(question["undertekst"], "lxml") for info in text.find_all("li"): information = Paragraph(info.text, style, bulletText="●") w, h = information.wrap( self.page_width - style.leftIndent - (self.theme["page_margin"] * 2), self.y_position, ) self.y_position -= h + 10 information.drawOn(self.canvas, 45, self.y_position) self.y_position -= 20 answer = "" elif question["svartype"] == "CHECKBOX_PANEL": checkbox_icon = svg2rlg("helpdful/resources/Checkboks.svg") renderPDF.draw( checkbox_icon, self.canvas, self.theme["page_margin"], self.y_position + self.theme["icon_offset"], ) answer = question["sporsmalstekst"] else: answer = question["svar"][0]["verdi"] answer_paragraph = Paragraph( answer, self.style["svartype"][question["svartype"]]) answer_paragraph_w, answer_paragraph_h = answer_paragraph.wrap( self.page_width - (self.theme["page_margin"] * 2), self.theme["question"]["svartype"][question["svartype"]], ) answer_paragraph.drawOn(self.canvas, self.theme["page_margin"], self.y_position) self.y_position -= answer_paragraph_h if not question["svartype"] == "IKKE_RELEVANT": self.y_position -= 20 if self.needs_next_page(20 + self.theme["footer"]["height"] + answer_paragraph_h): self.canvas.showPage() self._create_new_page() for subquestion in question["undersporsmal"]: self.__process_question(subquestion)
#!/bin/env python import os from reportlab.platypus import Flowable from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT from reportlab.lib.colors import CMYKColor from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from pdfrw import PdfReader, PdfDict from pdfrw.buildxobj import pagexobj from pdfrw.toreportlab import makerl black = CMYKColor(0, 0, 0, 1) class PdfImage(Flowable): '''from http://stackoverflow.com/questions/31712386/ loading-matplotlib-object-into-reportlab/''' def __init__(self, img_data, width=200, height=200): self.img_width = width self.img_height = height self.img_data = img_data def wrap(self, width, height): return self.img_width, self.img_height def drawOn(self, canv, x, y, _sW=0): if _sW > 0 and hasattr(self, 'hAlign'): a = self.hAlign
from reportlab.lib.colors import CMYKColor swansea_blue = CMYKColor(1, 0.9, 0.32, 0.20) light_grey = CMYKColor(0, 0, 0, 0.1) medium_grey = CMYKColor(0, 0, 0, 0.2) black = CMYKColor(0, 0, 0, 1)
class RUBPDF(object): RUBBLUE = CMYKColor(1,.5,0,.6) RUBGREEN = CMYKColor(.23,0,.85,.24) RUBGRAY = CMYKColor(.03,.03,.03,.1) BLACK = CMYKColor(0,0,0,1) WHITE = CMYKColor(0,0,0,0) WEIGHT_REGULAR = 'R' WEIGHT_BOLD = 'Bd' FIGURES_TZ = '' FIGURES_MZ = 'Mz' def __init__(self, pagesize = A4, templatefile = None): pdfmetrics.registerFont(TTFont('FlamaBd', os.path.join(PDFFONTDIR,'RubFlama-Bold.ttf'))) pdfmetrics.registerFont(TTFont('FlamaR', os.path.join(PDFFONTDIR,'RubFlama-Regular.ttf'))) pdfmetrics.registerFont(TTFont('ScalaR', os.path.join(PDFFONTDIR,'RubScalaTZ.ttf'))) pdfmetrics.registerFont(TTFont('ScalaBd', os.path.join(PDFFONTDIR,'RubScalaTZBold.ttf'))) pdfmetrics.registerFont(TTFont('ScalaRMz', os.path.join(PDFFONTDIR,'RUBScalaMZ.ttf'))) pdfmetrics.registerFont(TTFont('ScalaBdMz', os.path.join(PDFFONTDIR,'RUBScalaMZBold.ttf'))) if templatefile: self.template_pdf = PdfFileReader(templatefile) else: self.template_pdf = None self.buffer = BytesIO() self.pdf = canvas.Canvas(self.buffer, pagesize = pagesize) def font_flama(self, size, weight = None, color = BLACK): self.pdf.setFillColor(color) if not weight: weight = self.WEIGHT_REGULAR self.pdf.setFont('Flama{}'.format(weight) , size) def font_scala(self, size, weight = None, color = BLACK, figures = ''): self.pdf.setFillColor(color) if not weight: weight = self.WEIGHT_REGULAR self.pdf.setFont('Scala{}{}'.format(weight, figures) , size) def get_in_response(self, response): if not self.template_pdf: self.pdf.showPage() self.pdf.save() response.write(self.buffer.getvalue()) self.buffer.close() return response def write2file( self, filename ): if self.template_pdf: self.pdf.save() self.buffer.seek(0) new_pdf = PdfFileReader(self.buffer) page = self.template_pdf.getPage(0) page.mergePage(new_pdf.getPage(0)) output = PdfFileWriter() output.addPage(page) output_stream = open( filename, 'wb') output.write(output_stream) output_stream.close() def for_content_file(self, endpage = True): if endpage: self.pdf.showPage() self.pdf.save() return self.buffer.getvalue()
from accounts.models import Group from decisions.models import Decision from docs.utils import calculate_meeting_duration, \ calculate_meeting_end_time, \ get_completed_tasks_list, \ get_outstanding_tasks_list, \ get_overdue_tasks_list from meetings.models import Meeting from meetings.utils import find_or_create_distribution_record from participants.models import Participant from tasks.models import Task from utilities.commonutils import set_path # Define colors heading_color = CMYKColor(0,0,0,0.85) body_color = CMYKColor(0,0,0,0.85) table_color = CMYKColor(0,0,0,0.85) shading_color = CMYKColor(0,0,0,0.2) # Define dimensions line_width = 0.75 printable_width = 170*mm # Set path to fonts FONT_PATH = os.path.join(settings.BASE_DIR, 'commonstatic/fonts/') # Register fonts body_font = 'DejaVuSansCondensed' heading_font = 'DejaVuSansCondensed' registerFont(TTFont(body_font, FONT_PATH + body_font + ".ttf"))