Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,22 +43,25 @@ def home():
|
|
| 43 |
else:
|
| 44 |
# Redirect to your PDF viewer route when ready
|
| 45 |
return redirect(url_for("view_pdf", **request.args))
|
| 46 |
-
PROCESSED_PDF_PATH = "static/processed.pdf"
|
| 47 |
|
| 48 |
@app.route('/view-pdf', methods=['GET'])
|
| 49 |
def view_pdf():
|
| 50 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 51 |
encoded_pdf_link = request.args.get("pdfLink")
|
| 52 |
-
page = request.args.get("page",
|
| 53 |
-
zoom = request.args.get("zoom",
|
| 54 |
|
| 55 |
if not encoded_pdf_link:
|
| 56 |
return "Missing pdfLink parameter.", 400
|
| 57 |
|
| 58 |
pdf_link = urllib.parse.unquote(encoded_pdf_link)
|
| 59 |
-
print(f"
|
| 60 |
|
| 61 |
-
# Only process
|
| 62 |
if not os.path.exists(PROCESSED_PDF_PATH):
|
| 63 |
try:
|
| 64 |
from InitialMarkups import extract_section_under_header
|
|
@@ -69,20 +72,20 @@ def view_pdf():
|
|
| 69 |
os.makedirs("static", exist_ok=True)
|
| 70 |
with open(PROCESSED_PDF_PATH, "wb") as f:
|
| 71 |
f.write(pdf_content)
|
| 72 |
-
print("✅ PDF processed
|
| 73 |
except Exception as e:
|
| 74 |
print("❌ Error processing PDF:", e)
|
| 75 |
return "PDF could not be processed.", 500
|
| 76 |
else:
|
| 77 |
-
print("⚙️ Using cached PDF
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
return render_template("
|
| 81 |
|
| 82 |
|
| 83 |
@app.route('/get-pdf')
|
| 84 |
def get_pdf():
|
| 85 |
-
"""Serve the
|
| 86 |
if not os.path.exists(PROCESSED_PDF_PATH):
|
| 87 |
return "PDF not processed yet.", 404
|
| 88 |
return send_file(PROCESSED_PDF_PATH, mimetype="application/pdf")
|
|
|
|
| 43 |
else:
|
| 44 |
# Redirect to your PDF viewer route when ready
|
| 45 |
return redirect(url_for("view_pdf", **request.args))
|
| 46 |
+
PROCESSED_PDF_PATH = "static/processed.pdf"
|
| 47 |
|
| 48 |
@app.route('/view-pdf', methods=['GET'])
|
| 49 |
def view_pdf():
|
| 50 |
+
"""
|
| 51 |
+
Processes the PDF only once (first time).
|
| 52 |
+
Later requests just open the same viewer tab and move to correct page/zoom.
|
| 53 |
+
"""
|
| 54 |
encoded_pdf_link = request.args.get("pdfLink")
|
| 55 |
+
page = request.args.get("page", "1")
|
| 56 |
+
zoom = request.args.get("zoom", "100")
|
| 57 |
|
| 58 |
if not encoded_pdf_link:
|
| 59 |
return "Missing pdfLink parameter.", 400
|
| 60 |
|
| 61 |
pdf_link = urllib.parse.unquote(encoded_pdf_link)
|
| 62 |
+
print(f"PDF requested: {pdf_link}, Page: {page}, Zoom: {zoom}")
|
| 63 |
|
| 64 |
+
# Only process once
|
| 65 |
if not os.path.exists(PROCESSED_PDF_PATH):
|
| 66 |
try:
|
| 67 |
from InitialMarkups import extract_section_under_header
|
|
|
|
| 72 |
os.makedirs("static", exist_ok=True)
|
| 73 |
with open(PROCESSED_PDF_PATH, "wb") as f:
|
| 74 |
f.write(pdf_content)
|
| 75 |
+
print("✅ PDF processed successfully.")
|
| 76 |
except Exception as e:
|
| 77 |
print("❌ Error processing PDF:", e)
|
| 78 |
return "PDF could not be processed.", 500
|
| 79 |
else:
|
| 80 |
+
print("⚙️ Using cached PDF — already processed.")
|
| 81 |
|
| 82 |
+
# Pass page & zoom to the viewer
|
| 83 |
+
return render_template("viewer.html", start_page=page, start_zoom=zoom)
|
| 84 |
|
| 85 |
|
| 86 |
@app.route('/get-pdf')
|
| 87 |
def get_pdf():
|
| 88 |
+
"""Serve the processed PDF."""
|
| 89 |
if not os.path.exists(PROCESSED_PDF_PATH):
|
| 90 |
return "PDF not processed yet.", 404
|
| 91 |
return send_file(PROCESSED_PDF_PATH, mimetype="application/pdf")
|