atifsial123 commited on
Commit
10eb027
·
verified ·
1 Parent(s): 9ff0c52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -44
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
- # Create a PDF
42
- pdf = FPDF()
43
- pdf.add_page()
44
- pdf.set_font("Arial", 'B', 16)
45
- pdf.cell(200, 10, txt=f"Invoice Report for {company_name}", ln=True, align="C")
46
- pdf.set_font("Arial", size=12)
47
- current_date = datetime.now().strftime("%d/%m/%Y")
48
- pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
49
- pdf.ln(10)
50
-
51
- # Table headers
52
- pdf.cell(50, 10, txt="Company Name", border=1, align="C")
53
- pdf.cell(40, 10, txt="Name", border=1, align="C")
54
- pdf.cell(40, 10, txt="Invoice No", border=1, align="C")
55
- pdf.cell(30, 10, txt="Date", border=1, align="C")
56
- pdf.cell(30, 10, txt="Amount", border=1, ln=True, align="C")
57
-
58
- # Filter records by company name
59
- filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
60
- print(f"Processing {len(filtered_data)} records for {company_name}...")
61
-
62
- for _, row in filtered_data.iterrows():
63
- pdf.cell(50, 10, txt=str(row['Company Name']), border=1, align="C")
64
- pdf.cell(40, 10, txt=str(row['Name']), border=1, align="C")
65
- pdf.cell(40, 10, txt=str(row['Invoice No']), border=1, align="C")
66
- pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
67
- pdf.cell(30, 10, txt=str(row['Amount']), border=1, ln=True, align="C")
68
-
69
- # Total amount
70
- total_amount = filtered_data["Amount"].sum()
71
- pdf.cell(160, 10, txt="Total Amount", border=1, align="R")
72
- pdf.cell(30, 10, txt=str(total_amount), border=1, ln=True, align="C")
73
-
74
- # Save PDF file
75
- pdf.output(pdf_file_path)
76
-
77
- # Check if file exists
78
- if os.path.exists(pdf_file_path):
79
- print(f"PDF successfully created at {pdf_file_path}")
 
 
80
  return pdf_file_path
81
- else:
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."