简体   繁体   中英

Generate pdf with reportlab

In django project trying to create a pdf using python-reportlab

    from reportlab.pdfgen import canvas
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

    p = canvas.Canvas(response)
    p.drawString(10, 800, "Name")
    p.drawString(10, 900, "Address")
    p.drawString(10, 1000, "School")
    p.showPage()
    p.save()

On output pdf It only shows the Name , what happened to other two strings ?

The coordinates for "Address" and "School" are simply outside of the page. The origin of the reportlab coordinate system is bottom-left, with the x coordinate going to the right and the y coordinate going up. Try the following instead:

p.drawString(10, 800, "Name")
p.drawString(10, 790, "Address")
p.drawString(10, 780, "School")

So this works. Just gave smaller values to y co-ordinates. Try playing with these values according to your needs.

p = canvas.Canvas(response)
p.drawString(10,750,'Name')
p.drawString(10,650,'Address')
p.drawString(10,550,"School")
p.showPage()
p.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM