Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
from fpdf import FPDF
|
| 4 |
from datetime import datetime
|
|
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
# Initialize an empty DataFrame to store all invoice records
|
|
@@ -35,52 +39,50 @@ def add_invoice(company_name, name, invoice_no, date, amount):
|
|
| 35 |
# Function to generate and save the filtered invoice data to a PDF
|
| 36 |
def generate_invoice_pdf(company_name):
|
| 37 |
global invoice_data
|
| 38 |
-
pdf_file_path = "/content/invoice_report.pdf" # Define the file path
|
| 39 |
-
|
| 40 |
try:
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
return pdf_file_path
|
| 81 |
-
|
| 82 |
-
return "Error: PDF not generated."
|
| 83 |
-
|
| 84 |
except Exception as e:
|
| 85 |
print(f"Error during PDF generation: {e}")
|
| 86 |
return "Error: PDF generation failed."
|
|
|
|
| 1 |
+
# Install necessary libraries in Hugging Face Space
|
| 2 |
+
!pip install gradio fpdf pandas
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
from fpdf import FPDF
|
| 7 |
from datetime import datetime
|
| 8 |
+
import tempfile # Use this for temporary file handling
|
| 9 |
import os
|
| 10 |
|
| 11 |
# Initialize an empty DataFrame to store all invoice records
|
|
|
|
| 39 |
# Function to generate and save the filtered invoice data to a PDF
|
| 40 |
def generate_invoice_pdf(company_name):
|
| 41 |
global invoice_data
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
+
# Use a temporary directory to save the file
|
| 44 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
| 45 |
+
pdf_file_path = tmp_file.name # Get the temporary file path
|
| 46 |
+
|
| 47 |
+
# Create a PDF
|
| 48 |
+
pdf = FPDF()
|
| 49 |
+
pdf.add_page()
|
| 50 |
+
pdf.set_font("Arial", 'B', 16)
|
| 51 |
+
pdf.cell(200, 10, txt=f"Invoice Report for {company_name}", ln=True, align="C")
|
| 52 |
+
pdf.set_font("Arial", size=12)
|
| 53 |
+
current_date = datetime.now().strftime("%d/%m/%Y")
|
| 54 |
+
pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
|
| 55 |
+
pdf.ln(10)
|
| 56 |
+
|
| 57 |
+
# Table headers
|
| 58 |
+
pdf.cell(50, 10, txt="Company Name", border=1, align="C")
|
| 59 |
+
pdf.cell(40, 10, txt="Name", border=1, align="C")
|
| 60 |
+
pdf.cell(40, 10, txt="Invoice No", border=1, align="C")
|
| 61 |
+
pdf.cell(30, 10, txt="Date", border=1, align="C")
|
| 62 |
+
pdf.cell(30, 10, txt="Amount", border=1, ln=True, align="C")
|
| 63 |
+
|
| 64 |
+
# Filter records by company name
|
| 65 |
+
filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
|
| 66 |
+
print(f"Processing {len(filtered_data)} records for {company_name}...")
|
| 67 |
+
|
| 68 |
+
for _, row in filtered_data.iterrows():
|
| 69 |
+
pdf.cell(50, 10, txt=str(row['Company Name']), border=1, align="C")
|
| 70 |
+
pdf.cell(40, 10, txt=str(row['Name']), border=1, align="C")
|
| 71 |
+
pdf.cell(40, 10, txt=str(row['Invoice No']), border=1, align="C")
|
| 72 |
+
pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
|
| 73 |
+
pdf.cell(30, 10, txt=str(row['Amount']), border=1, ln=True, align="C")
|
| 74 |
+
|
| 75 |
+
# Total amount
|
| 76 |
+
total_amount = filtered_data["Amount"].sum()
|
| 77 |
+
pdf.cell(160, 10, txt="Total Amount", border=1, align="R")
|
| 78 |
+
pdf.cell(30, 10, txt=str(total_amount), border=1, ln=True, align="C")
|
| 79 |
+
|
| 80 |
+
# Save PDF file
|
| 81 |
+
pdf.output(pdf_file_path)
|
| 82 |
+
|
| 83 |
+
# Return the file path for download
|
| 84 |
return pdf_file_path
|
| 85 |
+
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error during PDF generation: {e}")
|
| 88 |
return "Error: PDF generation failed."
|