mike23415 commited on
Commit
45e4b1d
·
verified ·
1 Parent(s): 83f23b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -5
app.py CHANGED
@@ -5,6 +5,7 @@ import jinja2
5
  import pdfkit
6
  import torch
7
  import logging
 
8
  from threading import Thread
9
  from flask import Flask, request, send_file, jsonify
10
  from flask_cors import CORS
@@ -29,10 +30,18 @@ model_loaded = False
29
  load_error = None
30
  generator = None
31
 
 
 
 
 
 
 
 
 
 
 
32
  # Configure wkhtmltopdf
33
- # Use xvfb-run for headless PDF generation
34
- WKHTMLTOPDF_CMD = 'xvfb-run -a wkhtmltopdf'
35
- pdf_config = pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_CMD)
36
 
37
  def load_model():
38
  global model_loaded, load_error, generator
@@ -217,9 +226,27 @@ def generate_pdf():
217
  try:
218
  with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as f:
219
  pdf_path = f.name
 
 
 
 
 
220
 
221
- # Generate PDF using wkhtmltopdf with xvfb
222
- pdfkit.from_string(html, pdf_path, options=options, configuration=pdf_config)
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
  app.logger.info(f"PDF generated successfully at {pdf_path}")
225
  return send_file(pdf_path, mimetype='application/pdf', as_attachment=True,
 
5
  import pdfkit
6
  import torch
7
  import logging
8
+ import subprocess
9
  from threading import Thread
10
  from flask import Flask, request, send_file, jsonify
11
  from flask_cors import CORS
 
30
  load_error = None
31
  generator = None
32
 
33
+ # Find wkhtmltopdf path
34
+ WKHTMLTOPDF_PATH = '/usr/bin/wkhtmltopdf'
35
+ if not os.path.exists(WKHTMLTOPDF_PATH):
36
+ # Try to find it using which
37
+ try:
38
+ WKHTMLTOPDF_PATH = subprocess.check_output(['which', 'wkhtmltopdf']).decode().strip()
39
+ except:
40
+ app.logger.warning("Could not find wkhtmltopdf path. Using default.")
41
+ WKHTMLTOPDF_PATH = 'wkhtmltopdf'
42
+
43
  # Configure wkhtmltopdf
44
+ pdf_config = pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_PATH)
 
 
45
 
46
  def load_model():
47
  global model_loaded, load_error, generator
 
226
  try:
227
  with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as f:
228
  pdf_path = f.name
229
+
230
+ # Generate PDF using xvfb-run as a separate process
231
+ html_path = pdf_path + '.html'
232
+ with open(html_path, 'w', encoding='utf-8') as f:
233
+ f.write(html)
234
 
235
+ command = ['xvfb-run', '-a', WKHTMLTOPDF_PATH] + \
236
+ [f'--{k}={v}' for k, v in options.items() if v] + \
237
+ [html_path, pdf_path]
238
+
239
+ app.logger.info(f"Running command: {' '.join(command)}")
240
+ result = subprocess.run(command, capture_output=True, text=True)
241
+
242
+ if result.returncode != 0:
243
+ app.logger.error(f"PDF generation command failed: {result.stderr}")
244
+ # Fallback to direct pdfkit if available
245
+ app.logger.info("Trying fallback PDF generation with pdfkit")
246
+ pdfkit.from_string(html, pdf_path, options=options, configuration=pdf_config)
247
+
248
+ # Clean up HTML file
249
+ os.remove(html_path)
250
 
251
  app.logger.info(f"PDF generated successfully at {pdf_path}")
252
  return send_file(pdf_path, mimetype='application/pdf', as_attachment=True,