Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Pdf Generator using Python

Unlock the potential to create professional PDF documents with the magic of Python. In this project, you’ll learn how to develop Python scripts that generate PDF files from scratch or modify existing ones.

Whether you’re a business professional needing custom reports, a writer looking to self-publish, or a developer enhancing your document automation skills, this step-by-step guide will empower you to harness the power of Python for PDF generation. Join us as we explore the world of document creation, turning your ideas into beautifully formatted PDFs with ease!

Requirements


pip install reportlab


from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

def generate_custom_pdf(title, customer_data, items):
    pdf_file = f"{title}.pdf"

    # Create a new PDF document
    c = canvas.Canvas(pdf_file, pagesize=letter)

    # Set font and font size
    c.setFont("Helvetica", 12)

    # Write invoice header
    c.drawString(50, 750, title)

    # Write customer information
    c.drawString(50, 700, "Customer Information:")
    y_offset = 680
    for key, value in customer_data.items():
        c.drawString(50, y_offset, f"{key}: {value}")
        y_offset -= 20

    # Write items table header
    c.drawString(50, y_offset - 20, "Items")
    c.drawString(50, y_offset - 40, "Item Name")
    c.drawString(200, y_offset - 40, "Quantity")
    c.drawString(300, y_offset - 40, "Price")
    c.drawString(400, y_offset - 40, "Description")

    # Write items
    y_offset -= 60
    total_amount = 0

    for item in items:
        item_name, quantity, price, description = item
        c.drawString(50, y_offset, item_name)
        c.drawString(200, y_offset, str(quantity))
        c.drawString(300, y_offset, f"${price:.2f}")
        c.drawString(400, y_offset, description)

        total_amount += quantity * price
        y_offset -= 20

    # Write total amount
    c.drawString(200, y_offset - 20, "Total:")
    c.drawString(300, y_offset - 20, f"${total_amount:.2f}")

    # Save the PDF document
    c.save()

if __name__ == "__main__":
    # Take user input for invoice title
    title = input("Enter Invoice Title: ")

    # Take user input for customer information
    customer_data = {}
    customer_data["Name"] = input("Enter Customer Name: ")
    customer_data["Address"] = input("Enter Customer Address: ")
    customer_data["Email"] = input("Enter Customer Email: ")

    # Take user input for items
    items = []
    while True:
        item_name = input("Enter Item Name (or 'done' to finish): ")
        if item_name.lower() == "done":
            break

        quantity = int(input("Enter Quantity: "))
        price = float(input("Enter Price: "))
        description = input("Enter Product Description: ")

        items.append((item_name, quantity, price, description))

    generate_custom_pdf(title, customer_data, items)


The post Pdf Generator using Python appeared first on ideasorblogs.



This post first appeared on Ideasorblogs, please read the originial post: here

Share the post

Pdf Generator using Python

×

Subscribe to Ideasorblogs

Get updates delivered right to your inbox!

Thank you for your subscription

×