jhanifen
jhanifen

Reputation: 4591

Adding Graph to Reportlab PDF

I have seen many reportlab graphing examples. Generating the graph is not the problem, I can't seem to figure out how to display the graph on the pdf.

Here is the code:

buffer = StringIO()
p = canvas.Canvas(buffer, pagesize = letter)

##### Beginning of code in question

d = Drawing(200, 100)
pc = Pie()
pc.x = 65
pc.y = 15
pc.width = 70
pc.height = 70
pc.data = [10,20,30,40,50,60]
pc.labels = ['a','b','c','d','e','f']
pc.slices.strokeWidth=0.5
pc.slices[3].popout = 10
pc.slices[3].strokeWidth = 2
pc.slices[3].strokeDashArray = [2,2]
pc.slices[3].labelRadius = 1.75
pc.slices[3].fontColor = colors.red
d.add(pc)

p.drawPath(d) ### THIS DOES NOT WORK, but need something similar

#####End of Code in Question

p.showPage() #Page Two

p.save() # Saves the PDF and Returns with Response\

pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response

This is how I display text. p.setFillColorRGB(1,1,1) header = p.beginText(100, 765) header.textLine("Page Heading Text") p.drawText(header)

Upvotes: 3

Views: 9656

Answers (4)

Davidson Lima
Davidson Lima

Reputation: 859

An improved answer from @siguy example, using reportlab 2.7.

from reportlab.lib.colors import red
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.piecharts import Pie

def savePdfGraph(request):
    d = Drawing(width=400, height=200)
    pc = Pie()
    pc.x = 150
    pc.y = 50
    pc.width = 70
    pc.height = 70
    pc.data = [10, 20, 30, 40, 50, 60]
    pc.labels = ['a', 'b', 'c', 'd', 'e', 'f']
    pc.slices.strokeWidth = 0.5
    pc.slices[3].popout = 10
    pc.slices[3].strokeWidth = 2
    pc.slices[3].strokeDashArray = [2, 2]
    pc.slices[3].labelRadius = 1.75
    pc.slices[3].fontColor = red
    d.add(pc)

    filename = 'test'
    base_dir = '/home/'
    path = os.path.join(base_dir, filename)
    d.save(formats=['pdf'], outDir=None, fnRoot=path)
    return redirect('/')

Output here:

Generated pdf graph with reportlab 2.7

Upvotes: 0

j_demarco
j_demarco

Reputation: 11

If you need to add the chart to your canvas use d.drawOn(p,0,0) instead of p.drawPath(d)

Upvotes: 1

siguy
siguy

Reputation: 31

skip the canvas and just use the Drawing widget- it will generate the PDF:

d = Drawing(200, 100)
pc = Pie()
pc.x = 65
pc.y = 15
pc.width = 70
pc.height = 70
pc.data = [10,20,30,40,50,60]
pc.labels = ['a','b','c','d','e','f']
pc.slices.strokeWidth=0.5
pc.slices[3].popout = 10
pc.slices[3].strokeWidth = 2
pc.slices[3].strokeDashArray = [2,2]
pc.slices[3].labelRadius = 1.75
pc.slices[3].fontColor = colors.red
d.add(pc)

d.save(formats=['pdf'],outDir=None,fnRoot='C:/test')

Upvotes: 3

jonesy
jonesy

Reputation: 3542

I wrote this some time ago, but it's been one of the most popular articles on the site, so I guess it works for some.

http://www.protocolostomy.com/2008/10/22/generating-reports-with-charts-using-python-reportlab/

If it's not enough to get you through, let me know and I'll come back to help out more later when I have more time.

Upvotes: 0

Related Questions