Rammohan0504 commited on
Commit
87fa542
·
verified ·
1 Parent(s): 7149a12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -136,7 +136,7 @@ def build_table(title, rows):
136
  def save_results_to_pdf(profile_image_base64, test_results, summary, patient_name, patient_age, patient_gender, patient_id, filename):
137
  try:
138
  # Generate HTML content
139
- current_date = datetime.now().strftime("%B %d, %Y %I:%M %p IST") # Adjusted for IST
140
  health_card_html = f"""
141
  <!DOCTYPE html>
142
  <html>
@@ -196,8 +196,9 @@ def save_results_to_pdf(profile_image_base64, test_results, summary, patient_nam
196
  </html>
197
  """
198
 
199
- # Debug: Print HTML length
200
  print(f"HTML generated, length: {len(health_card_html)}")
 
201
 
202
  # Save HTML to a temporary file
203
  html_temp_path = "/tmp/health_card.html"
@@ -210,13 +211,26 @@ def save_results_to_pdf(profile_image_base64, test_results, summary, patient_nam
210
  print(f"Error saving HTML: {e}")
211
  return f"Error: Failed to save HTML - {str(e)}", None
212
 
213
- # Convert HTML to PDF using pdfkit
214
  try:
215
- pdfkit.from_file(html_temp_path, filename, options={"quiet": ""})
216
- print(f"PDF generated successfully at: {filename}") # Debug log
 
 
 
 
 
 
 
 
217
  except Exception as e:
218
  print(f"Error converting to PDF: {str(e)}")
219
- return f"Error: Failed to generate PDF - {str(e)}", None
 
 
 
 
 
220
 
221
  # Move to /tmp for Gradio access
222
  temp_pdf_path = "/tmp/" + os.path.basename(filename)
@@ -235,7 +249,7 @@ def save_results_to_pdf(profile_image_base64, test_results, summary, patient_nam
235
 
236
  # Build health card layout
237
  def build_health_card(profile_image, test_results, summary, pdf_filepath, patient_name="", patient_age="", patient_gender="", patient_id=""):
238
- current_date = datetime.now().strftime("%B %d, %Y %I:%M %p IST") # Adjusted for IST
239
 
240
  pdf_filename = os.path.basename(pdf_filepath) if pdf_filepath else "health_report.pdf"
241
 
@@ -291,7 +305,7 @@ def build_health_card(profile_image, test_results, summary, pdf_filepath, patien
291
  /* Hide input sections during print */
292
  .gradio-container {{ display: block; }}
293
  /* Keep only the health card visible */
294
- #health-card {{ display: block; }}
295
  }}
296
  </style>
297
  """
 
136
  def save_results_to_pdf(profile_image_base64, test_results, summary, patient_name, patient_age, patient_gender, patient_id, filename):
137
  try:
138
  # Generate HTML content
139
+ current_date = datetime.now().strftime("%B %d, %Y %I:%M %p IST")
140
  health_card_html = f"""
141
  <!DOCTYPE html>
142
  <html>
 
196
  </html>
197
  """
198
 
199
+ # Debug: Print HTML length and sample
200
  print(f"HTML generated, length: {len(health_card_html)}")
201
+ print(f"HTML sample: {health_card_html[:500]}...") # Print first 500 chars for inspection
202
 
203
  # Save HTML to a temporary file
204
  html_temp_path = "/tmp/health_card.html"
 
211
  print(f"Error saving HTML: {e}")
212
  return f"Error: Failed to save HTML - {str(e)}", None
213
 
214
+ # Convert HTML to PDF using pdfkit with verbose logging
215
  try:
216
+ pdfkit_options = {
217
+ "quiet": "",
218
+ "enable-local-file-access": "", # Allow local file access for images
219
+ "margin-top": "0",
220
+ "margin-bottom": "0",
221
+ "margin-left": "0",
222
+ "margin-right": "0"
223
+ }
224
+ pdfkit.from_file(html_temp_path, filename, options=pdfkit_options)
225
+ print(f"PDF generated successfully at: {filename}")
226
  except Exception as e:
227
  print(f"Error converting to PDF: {str(e)}")
228
+ # Save fallback HTML with timestamp
229
+ fallback_path = f"/tmp/health_card_fallback_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html"
230
+ with open(fallback_path, "w", encoding="utf-8") as f:
231
+ f.write(health_card_html)
232
+ print(f"Fallback HTML saved to: {fallback_path}")
233
+ return f"Error: Failed to generate PDF - {str(e)}. Fallback HTML at {fallback_path}", None
234
 
235
  # Move to /tmp for Gradio access
236
  temp_pdf_path = "/tmp/" + os.path.basename(filename)
 
249
 
250
  # Build health card layout
251
  def build_health_card(profile_image, test_results, summary, pdf_filepath, patient_name="", patient_age="", patient_gender="", patient_id=""):
252
+ current_date = datetime.now().strftime("%B %d, %Y %I:%M %p IST")
253
 
254
  pdf_filename = os.path.basename(pdf_filepath) if pdf_filepath else "health_report.pdf"
255
 
 
305
  /* Hide input sections during print */
306
  .gradio-container {{ display: block; }}
307
  /* Keep only the health card visible */
308
+ #health-card {{ display: block; }}
309
  }}
310
  </style>
311
  """