Marthee commited on
Commit
78a602d
·
verified ·
1 Parent(s): 4d6b875

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -44,6 +44,12 @@ def home():
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
  def getpdfcontent(pdf_path):
49
  # Handle Dropbox URLs
@@ -54,39 +60,39 @@ def getpdfcontent(pdf_path):
54
  response = requests.get(pdf_path)
55
  pdf_bytes = response.content
56
 
57
- if not pdf_bytes:
58
  raise ValueError("No valid PDF content found.")
59
 
60
- # Convert to BytesIO only after validation
61
  return BytesIO(pdf_bytes)
62
 
63
  @app.route('/view-pdf', methods=['GET'])
64
  def view_pdf():
65
- # supports /view-pdf?pdfLink=<encoded link>
66
  encoded_pdf_link = request.args.get('pdfLink')
67
  if not encoded_pdf_link:
68
  return "Missing pdfLink parameter.", 400
69
 
70
  pdf_link = unquote(encoded_pdf_link)
71
  print("Extracted PDF Link:", pdf_link)
72
-
73
  try:
74
- pdf_content=getpdfcontent(pdf_link)
75
- # pdf_content = InitialMarkups.extract_section_under_header(pdf_link)[0]
76
  except Exception as e:
77
  print("Error during PDF extraction:", e)
78
  return "PDF could not be processed.", 500
79
 
80
- if pdf_content is None :
81
  return "PDF content not found or broken.", 404
82
 
 
83
  return send_file(
84
- BytesIO(pdf_content),
85
  mimetype='application/pdf',
86
  as_attachment=False,
87
- download_name=f"annotated_page_{pageNumTextFound}.pdf"
88
  )
89
 
 
90
  @app.route('/get-pdf')
91
  def get_pdf():
92
  """Serve the processed PDF."""
 
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
+ from flask import Flask, request, send_file
48
+ from io import BytesIO
49
+ import requests
50
+ from urllib.parse import unquote
51
+
52
+ app = Flask(__name__)
53
 
54
  def getpdfcontent(pdf_path):
55
  # Handle Dropbox URLs
 
60
  response = requests.get(pdf_path)
61
  pdf_bytes = response.content
62
 
63
+ if not pdf_bytes or not pdf_bytes.startswith(b"%PDF"):
64
  raise ValueError("No valid PDF content found.")
65
 
66
+ # Return a BytesIO stream
67
  return BytesIO(pdf_bytes)
68
 
69
  @app.route('/view-pdf', methods=['GET'])
70
  def view_pdf():
 
71
  encoded_pdf_link = request.args.get('pdfLink')
72
  if not encoded_pdf_link:
73
  return "Missing pdfLink parameter.", 400
74
 
75
  pdf_link = unquote(encoded_pdf_link)
76
  print("Extracted PDF Link:", pdf_link)
77
+
78
  try:
79
+ pdf_content = getpdfcontent(pdf_link)
 
80
  except Exception as e:
81
  print("Error during PDF extraction:", e)
82
  return "PDF could not be processed.", 500
83
 
84
+ if pdf_content is None:
85
  return "PDF content not found or broken.", 404
86
 
87
+ # ✅ Do NOT wrap again in BytesIO
88
  return send_file(
89
+ pdf_content,
90
  mimetype='application/pdf',
91
  as_attachment=False,
92
+ download_name="annotated_page.pdf"
93
  )
94
 
95
+
96
  @app.route('/get-pdf')
97
  def get_pdf():
98
  """Serve the processed PDF."""