DEV Community

BC
BC

Posted on

Use `fpdf` to generate PDF file as receipt - Python Tips

You can use fpdf library to generate PDF file, for example a purchase receipt.

First install the library:

$ pip install fpdf
Enter fullscreen mode Exit fullscreen mode

Now let's create a PDF for receipt:

from fpdf import FPDF
# generate a receipt

def generate_receipt(date, amount):
  pdf = FPDF(orientation='P', unit='pt', format='A4')
  pdf.add_page()
  pdf.set_font("Times", "B", 24)
  pdf.cell(0, 80, "Purchase Receipt", 0, 1, "C")
  pdf.set_font("Times", "B", 14)
  pdf.cell(100, 25, "Payment Date:")
  pdf.set_font("Times", "", 12)
  pdf.cell(0, 25, "{}".format(date), 0, 1)
  pdf.cell(0, 5, "", 0, 1)
  pdf.set_font("Times", "B", 14)
  pdf.cell(100, 25, "Payment Total:")
  pdf.set_font("Times", "", 12)
  pdf.cell(0, 25, "${}".format(amount), 0, 1)
  return pdf.output(dest="S")
Enter fullscreen mode Exit fullscreen mode

Reference

Top comments (0)