Question : I’m generating charts using ReportLab. Charts are generated properly but into different PDF. I want to combine them into a single existing pdf.
basic structure of code is
class BreakdownPieDrawing():
def firstChart():
#code for generating first Pie chart
def secondChart():
#code for generating second Pie chart
if __name__=="__main__":
drawing1 = BreakdownPieDrawing()
drawing1.firstChart()
drawing1.save(formats=['pdf'],outDir='.',fnRoot='first')
drawing2 = BreakdownPieDrawing()
drawing2.secondChart()
drawing2.save(formats=['pdf'],outDir='.',fnRoot='second')
for full code Snippets please refer http://www.reportlab.com/snippets/4/
This code produces two separate PDFs. How can i combine them into single PDF.
I tried this to code :
def makePdf(self,drawing):
doc = SimpleDocTemplate('hello.pdf')
doc.build(drawing)
and then after I’m passing “BreakdownPieDrawing” class’s object into this method. But this approach is not working.
I’m new to reportLab and python so pardon me for such a ugly code.
So the question is how to add this charts into existing pdf. Any help would be greatly appreciated.
If you look closely at the code snippet you provided (disclaimer: I wrote 😉 ) specifically at line#33 you see
That lines adds the Pie chart to the drawing, you will then need to add your second chart to the same drawing you use the _add method, again like the snippet does on line#46 for adding the legend. Your drawing will have the two charts and when you save it as PDF you should get them both.