How To Generate PDF Files Using FPDF In Python

Posted By : Nikhil Verma | 29-Sep-2021

 Generate PDF Files Using FPDF In Python

 

What is FPDF?

PYFPDF is a library in Python, it is used for pdf file document generation. If we talk about the feature then we can customize like we can modify the page format, margin, unit, automatic line break and more.

 

How to install the FPDF library?

pip install pypdf2

By using this command on the terminal you can install this library on your system.

 

How Developers Can Use This Library?

Step-1: Create django project:

django-admin startproject generate_pdf

 

Step-2: create django app:

python manage.py startapp pdf_app

 

Step-3: Install FPDF library

pip install fpdf2

 

Step-4: Create a template directory

mkdir template

 

Step-5: Set the path of template director in settings.py file

 

Step-6: Create html file under the template directory

touch index.html

 

Step-7: Inside of html file

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>PDF Report</title>

    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">

</head>

<body>

    <div class="container shadow"

         style="max-width: 640px; margin: auto; margin-top: 2em; padding: 2em">

        <h4>To generate a report, please click the button below:</h4>

        <a class="btn btn-success" href="/report">Report</a>

    </div>

</body>

</html>

 

Step-8: Create Two method inside of app views.py file

from django.shortcuts import render

from fpdf import FPDF

from django.http import FileResponse

def index(request):

    context = {}

    return render(request, "index.html", context = context)

def report(request):

    sales = [

        {"item": "Speaker", "amount": "$120,00"},

        {"item": "UPS", "amount": "$10,00"},

        {"item": "PS5", "amount": "$1 000 000,00"},

    ]

    pdf = FPDF('L', 'mm', 'A4')

    pdf.add_page()

    pdf.set_font('courier', 'B', 16)

    pdf.cell(40, 10, 'Customer Order Items:',0,1)

    pdf.cell(40, 10, '',0,1)

    pdf.set_font('courier', '', 12)

    pdf.cell(200, 8, f"{'Item'.ljust(50)} {'Amount'.rjust(20)}", 1, 1)

    pdf.line(10, 30, 150, 30)

    pdf.line(10, 38, 150, 38)

    for line in sales:

        pdf.cell(200, 8, f"{line['item'].ljust(50)} {line['amount'].rjust(20)}", 0, 1)

    pdf.output('report.pdf', 'F')

    return FileResponse(open('report.pdf', 'rb'), as_attachment=True, content_type='application/pdf')

 

 

 

Step-9: Set the url for the method in Application urls.py file

Inside of urls.py file

from django.urls import path

from . import views

urlpatterns = [

    path('', views.index),

    path('report',views.report)

]

 

Step-10: Include this urls.py file in project urls.py file

from django.urls import path,include

path('', include('app.urls'))

Step-11: Execute the program

python manage.py runserver

Click on -> http://127.0.0.1:8000/

Click on -> Report 

 

If you follow the above steps, you will get a pdf file. This step makes it easy to generate pdf documents.

 

 

We, at Oodles, provide complete web application development services with a focus on solving complex business problems through next-gen technologies. Our custom ERP development services enable enterprises to efficiently manage their mission-critical workflows across web and mobile platforms. To learn more, drop us a line at [email protected].

 

 



 

 


Please remove URL from text

Comment is required

Sending message..
Connect with us