python - How to add a chart to a PDF using ReportLab -
i've python project generates pdf data in web app in python using reportlab. it's building large text string , adding canvas, , generating pdf canvas. i've been asked add line chart in middle of pdf. i've found lot of info out there on how turn chart directly pdf in reportlab, example this:
but nothing how add chart pdf part of other formatted content. here's code i'm working with:
class genpdf(r): @tornado.web.authenticated def get(self): """takes text , generates pdf""" msg = gen_report(subject, test_name) filename = "filename.txt" self.set_header("content-type", 'application/pdf; charset="utf-8"') self.set_header("content-disposition", "attachment; filename=%s.pdf" %filename) io = stringio.stringio() c = canvas.canvas(io, pagesize=a4) imagem = canvas.imagereader(stringio.stringio(open('logo.jpeg', 'rb').read())) c.drawimage(imagem, 430, 688, 100, 100) # draw in bottom left, 2 inches high , 2 inches wide text = c.begintext() text.settextorigin(100, 700) text.setfont("times-roman", 16) text.textline("test report") text.setfont("times-roman", 12) text.textlines(msg) text.textlines(class_map[test_name]['blurb']) text.textline("_____________________________________________________________________________") text.textlines(class_map[test_name]['scale']) text.textline("_____________________________________________________________________________") text.setfont("times-roman", 8) text.textlines(disclaimer) c.drawtext(text) c.showpage() c.save() pdf=io.getvalue() io.close() self.write(pdf)
where in add code add chart? tried adding drawing can't seem figure out can add drawing canvas, or pdf itself.
you can manipulate size , position chart on page this:
drawing = drawing(350, 190) lc = horizontallinechart() lc.x = 10 lc.y = 10 lc.height = 150 lc.width = 330 ... drawing.halign = 'center' drawing.add(lc)
Comments
Post a Comment