def build_pdf(rows): "Build PDF" pdffile = StringIO() doc = BaruwaPDFTemplate(pdffile, topMargin=50, bottomMargin=18) import baruwa here = os.path.dirname(os.path.dirname(os.path.abspath(baruwa.__file__))) logo = os.path.join(here, 'baruwa', 'public', 'imgs', 'logo.png') img = Image(logo) logobj = [(img, _('Audit Log exported report'))] logo_table = Table(logobj, [2.0 * inch, 5.4 * inch]) logo_table.setStyle(PIE_TABLE) parts = [logo_table] parts.append(Spacer(1, 20)) parts.append(Paragraph(_('Audit Logs'), STYLES['Heading1'])) heading = (( Paragraph(_('Date/Time'), STYLES["Heading6"]), Paragraph(_('Username'), STYLES["Heading6"]), Paragraph(_('Info'), STYLES["Heading6"]), Paragraph(_('Hostname'), STYLES["Heading6"]), Paragraph(_('Remote IP'), STYLES["Heading6"]), Paragraph(_('Action'), STYLES["Heading6"]), )) rows.insert(0, heading) table = Table(rows, [ 1.10 * inch, 1.23 * inch, 1.96 * inch, 1.69 * inch, 0.95 * inch, 0.45 * inch, ]) table.setStyle( TableStyle([ ('FONTSIZE', (0, 0), (-1, -1), 8), ('FONT', (0, 0), (-1, -1), 'Helvetica'), ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ('GRID', (0, 0), (-1, -1), 0.15, colors.black), ])) parts.append(table) doc.title = _('Baruwa Audit log export') doc.build(parts) return pdffile.getvalue()
def build_pdf(rows): "Build PDF" pdffile = StringIO() doc = BaruwaPDFTemplate(pdffile, topMargin=50, bottomMargin=18) import baruwa here = os.path.dirname( os.path.dirname(os.path.abspath(baruwa.__file__)) ) logo = os.path.join(here, 'baruwa', 'public', 'imgs', 'logo.png') img = Image(logo) logobj = [(img, _('Audit Log exported report'))] logo_table = Table(logobj, [2.0 * inch, 5.4 * inch]) logo_table.setStyle(PIE_TABLE) parts = [logo_table] parts.append(Spacer(1, 20)) parts.append(Paragraph(_('Audit Logs'), STYLES['Heading1'])) heading = ((Paragraph(_('Date/Time'), STYLES["Heading6"]), Paragraph(_('Username'), STYLES["Heading6"]), Paragraph(_('Info'), STYLES["Heading6"]), Paragraph(_('Hostname'), STYLES["Heading6"]), Paragraph(_('Remote IP'), STYLES["Heading6"]), Paragraph(_('Action'), STYLES["Heading6"]), )) rows.insert(0, heading) table = Table(rows, [1.10 * inch, 1.23 * inch, 1.96 * inch, 1.69 * inch, 0.95 * inch, 0.45 * inch,]) table.setStyle(TableStyle([ ('FONTSIZE', (0, 0), (-1, -1), 8), ('FONT', (0, 0), (-1, -1), 'Helvetica'), ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ('GRID', (0, 0), (-1, -1), 0.15, colors.black), ])) parts.append(table) doc.title = _('Baruwa Audit log export') doc.build(parts) return pdffile.getvalue()
class PDFReport(object): """Generates a PDF report""" def __init__(self, logo, title='Baruwa mail report'): "Init" self.pdf = StringIO() self.doc = BaruwaPDFTemplate(self.pdf, topMargin=50, bottomMargin=18) img = u'' if os.path.exists(logo): img = Image(logo) logobj = [(img, title)] logo_table = Table(logobj, [2.0 * inch, 5.4 * inch]) logo_table.setStyle(PIE_TABLE) self.parts = [logo_table] self.parts.append(Spacer(1, 20)) self.sentry = 0 def _draw_square(self, color): "draws a square" square = Drawing(5, 5) sqr = Rect(0, 2.5, 5, 5) sqr.fillColor = color sqr.strokeColor = color square.add(sqr) return square def _pie_chart(self, data, title, headers, sortby): "Build PIE chart" headings = [headers] rows = [[self._draw_square(PIE_CHART_COLORS[index]), getattr(row, 'address'), getattr(row, 'count'), format_byte_size(getattr(row, 'size')), ''] for index, row in enumerate(data)] if len(rows) and len(rows) != 10: missing = 10 - len(rows) add_rows = [('', '', '', '', '') for ind in range(missing)] rows.extend(add_rows) if not rows: return headings.extend(rows) piedata = [getattr(row, sortby) for row in data] total = sum(piedata) labels = [("%.1f%%" % ((1.0 * getattr(row, sortby) / total) * 100)) for row in data] pie = PieChart() pie.chart.labels = labels pie.chart.data = piedata headings[1][4] = pie table_with_style = Table(headings, [0.2 * inch, 2.8 * inch, 0.5 * inch, 0.7 * inch, 3.2 * inch]) table_with_style.setStyle(TABLE_STYLE) paragraph = Paragraph(title, STYLES['Heading1']) self.parts.append(paragraph) self.parts.append(table_with_style) self.parts.append(Spacer(1, 70)) if (self.sentry % 2) == 0: self.parts.append(PageBreak()) def _bar_chart(self, data, title, headers): "Build the bar chart" self.parts.append(Paragraph(title, STYLES['Heading1'])) rows = [( Table([[self._draw_square(colors.white), Paragraph(headers['date'], STYLES["Heading6"])]], [0.35 * inch, 1.13 * inch, ]), Table([[self._draw_square(colors.green), Paragraph(headers['mail'], STYLES["Heading6"])]], [0.35 * inch, 1.13 * inch, ]), Table([[self._draw_square(colors.pink), Paragraph(headers['spam'], STYLES["Heading6"])]], [0.35 * inch, 1.13 * inch, ]), Table([[self._draw_square(colors.red), Paragraph(headers['virus'], STYLES["Heading6"])]], [0.35 * inch, 1.13 * inch, ]), Table([[self._draw_square(colors.blue), Paragraph(headers['volume'], STYLES["Heading6"])]], [0.35 * inch, 1.13 * inch, ]), ),] if not data: return graph, rows = build_barchart(data, rows=rows, header=headers['totals']) graph_table = Table([[graph], ], [7.4 * inch]) self.parts.append(graph_table) self.parts.append(Spacer(1, 20)) graph_table = Table(rows, [1.48 * inch, 1.48 * inch, 1.48 * inch, 1.48 * inch, 1.48 * inch]) graph_table.setStyle(TableStyle([ ('FONTSIZE', (0, 0), (-1, -1), 8), ('FONT', (0, 0), (-1, -1), 'Helvetica'), ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ('GRID', (0, 0), (-1, -1), 0.15, colors.black), ('FONT', (0, -1), (-1, -1), 'Helvetica-Bold'), ])) self.parts.append(graph_table) def add(self, data, title, headers, sortby=None, chart='pie'): """Add the charts""" self.sentry += 1 if chart == 'pie': self._pie_chart(data, title, headers, sortby) if chart == 'bar': self._bar_chart(data, title, headers) else: pass def build(self): """Generate the PDF""" self.doc.title = 'Baruwa Usage Reports' self.doc.build(self.parts) pdf = self.pdf.getvalue() self.pdf.close() return pdf