Search code examples
pythonpython-2.7pdfpdf-generationreportlab

How to generate a bill in pdf format using python 2.7


My need is to generate a bill in pdf format using python. That bill will change its data like no of passengers or travel details, date, time etc. But i need to generate a template like format for that. Which one i need to use? give me some example? I'm new to python, So please help me.

I tried to use reportlab module to generate pdf, But may i need to give exact x and y points of characters in this reportlab? Is there any other option? If my passenger details increase into more than one how can i automate or generate dynamic pdf?

I tried this one as sample two line:

import os
import sys
import unittest

from reportlab.pdfgen import canvas

canvas = canvas.Canvas("form.pdf")
canvas.setLineWidth(.3)

def titleOfTicket():
    canvas.setFont('Helvetica', 12)
    canvas.drawString(30,750,'Bus Ticket')
    canvas.drawString(550,750,"Mr.X")

def header():
    canvas.line(30,740,580,740)
    canvas.drawString(30,723,"Bangalore to Chennai")
    canvas.drawString(250,723,"thu, 19 june 2017")
    canvas.drawString(450,723,"Trip ID 12345678901245")
    canvas.line(30,715,580,715)


titleOfTicket()
header()

canvas.save()

Is there any other module or option to generate the template for a ticket easily? my sample format ticket is need to be like this Sample Ticket pdf


Solution

  • Just starting to use reportlab, so I don't know much yet. But I think the platypus submodule would make this much easier. It's described in the ReportLab User Guide. And I just found a blog post that might help, even if it's quite old: simple step by step reportlab tutorial.

    And what about the commercial version? That might be still easier to use, as far as I can tell from the website.