combine two charts into the single PDF using ReportLab?
Question: I'm using Django and Reportlabs to generate reports in PDF. I'm referring to
[this tutorial](https://code.djangoproject.com/wiki/Charts).

I read [this thread](http://stackoverflow.com/questions/9339572/how-to-
combine-two-charts-into-the-single-pdf-using-reportlab) as well as [this
thread](http://stackoverflow.com/questions/9696211/python-how-to-make-
reportlab-move-to-next-page-in-pdf-output) also which says use
`canv.showpage()` and then I'll be able to combine 2 charts in 1 pdf, but
still I get only that chart which comes second in the code, in my case only
line graph.

How can I save 2 charts in 1 pdf?

Here is my code.

    
    
    import barchart
    import linechart
    from django.http import HttpResponse
    from reportlab.graphics.shapes import Drawing
    from reportlab.graphics.charts.barcharts import VerticalBarChart
    from reportlab.graphics import renderPDF    
    from reportlab.pdfgen import canvas
    
    
    def generate_report(request):
    
    
        #instantiate a drawing object
        canv = canvas.Canvas('output.pdf')#,pagesize=LETTER) 
        canv.setPageCompression(0)  
        d = barchart.MyBarChartDrawing()
    
        #extract the request params of interest.
        #I suggest having a default for everything.
        if 'height' in request:
            d.height = int(request['height'])
        if 'width' in request:
            d.width = int(request['width'])
    
        if 'numbers' in request:
            strNumbers = request['numbers']
            numbers = map(int, strNumbers.split(','))    
            d.chart.data = [numbers]   #bar charts take a list-of-lists for data
    
        if 'title' in request:
            d.title.text = request['title']
    
    
        #get a GIF (or PNG, JPG, or whatever)
        binaryStuff = d.asString('pdf')
    
        #binaryStuff.save()
        #return HttpResponse(binaryStuff, 'image/pdf')
        #instantiate a drawing object
        canv.showPage()
        a = linechart.MyLineChartDrawing()
    
        #extract the request params of interest.
        #I suggest having a default for everything.
    
    
        a.height = 300
        a.chart.height = 300
    
    
        a.width = 300
        a.chart.width = 300
    
        a.title._text = request.session.get('Some custom title')
    
    
    
        a.XLabel._text = request.session.get('X Axis Labell')
        a.YLabel._text = request.session.get('Y Axis Label')
    
        a.chart.data = [((1,1), (2,2), (2.5,1), (3,3), (4,5)),((1,2), (2,3), (2.5,2), (3.5,5), (4,6))]
    
    
    
        labels =  ["Label One","Label Two"]
        if labels:
            # set colors in the legend
            a.Legend.colorNamePairs = []
            for cnt,label in enumerate(labels):
                a.Legend.colorNamePairs.append((a.chart.lines[cnt].strokeColor,label))
    
    
        #get a GIF (or PNG, JPG, or whatever)
        binaryStuff1 = a.asString('pdf')
        canv.showPage()
        return HttpResponse(binaryStuff, 'pdf')
    

The barchart and linechart code is same from [this
site](https://code.djangoproject.com/wiki/Charts).

How would I save in one pdf file only?


Answer: Based on the code provided, you are getting only the Line Chart because you
are only passing BinaryStuff to the response (look at the last 3 lines of your
code).

Try creating the graphs as images, drawing the images to the canvas, then
returning the canvas as a PDF.

