DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Bo
Bo

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)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›