Marthee commited on
Commit
e592a75
·
verified ·
1 Parent(s): 92874af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -44
app.py CHANGED
@@ -1,60 +1,56 @@
1
- from flask import Flask, send_from_directory, jsonify, redirect, url_for
2
- import os
3
  import requests
4
  from io import BytesIO
 
5
 
6
  app = Flask(__name__)
7
 
8
- # Directory for storing temporary PDFs
9
- PDF_DIRECTORY = './temp_pdfs'
10
- TEMP_PDF_NAME = 'temp_downloaded.pdf'
11
-
12
- # Ensure the directory exists
13
- os.makedirs(PDF_DIRECTORY, exist_ok=True)
14
-
15
- @app.route('/download-pdf', methods=['GET'])
16
- def download_pdf():
17
- """
18
- Download PDF from a Dropbox link and save it temporarily.
19
- """
20
  link = 'https://www.dropbox.com/scl/fi/fjykwhhn9gu9t3kqrflxd/LA002-NOR-ZZ-ZZ-T-A-2403_Architectural-Specification-F10-Brick-and-Block-Walling_A4-_C01.pdf?rlkey=ek9i66i79m0hwp8z5yjs6rp5p&st=jh05a6qs&dl=0'
21
 
22
- # Modify Dropbox link for direct download
23
  if 'dl=0' in link:
24
  link = link.replace('dl=0', 'dl=1')
25
 
26
- try:
27
- response = requests.get(link)
28
- response.raise_for_status() # Raise exception for HTTP errors
29
- pdf_content = BytesIO(response.content) # Store PDF in memory
30
- local_pdf_path = os.path.join(PDF_DIRECTORY, TEMP_PDF_NAME)
31
-
32
- # Save the PDF to the temporary directory
33
- with open(local_pdf_path, 'wb') as f:
34
- f.write(pdf_content.getvalue())
35
-
36
- return jsonify({"message": "PDF downloaded successfully."})
37
- except Exception as e:
38
- return jsonify({"error": str(e)}), 500
39
 
40
- @app.route('/view-pdf', methods=['GET'])
41
- def view_pdf():
42
- """
43
- Serve the PDF for viewing in an iframe or direct link.
44
- """
45
- local_pdf_path = os.path.join(PDF_DIRECTORY, TEMP_PDF_NAME)
46
- if os.path.exists(local_pdf_path):
47
- return send_from_directory(PDF_DIRECTORY, TEMP_PDF_NAME)
48
- else:
49
- return jsonify({"message": "PDF not found. Please download it first via /download-pdf"}), 404
50
 
51
- @app.route('/pdf-page/<int:page>', methods=['GET'])
52
- def pdf_page(page):
53
- """
54
- Redirect to the PDF page with navigation.
55
- """
56
- return redirect(url_for('view_pdf') + f"#page={page}")
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  if __name__ == '__main__':
60
  app.run(host='0.0.0.0', port=7860)
 
1
+ from flask import Flask, send_file, render_template_string
 
2
  import requests
3
  from io import BytesIO
4
+ import os
5
 
6
  app = Flask(__name__)
7
 
8
+ # Route to serve the PDF file
9
+ @app.route('/view-pdf')
10
+ def view_pdf():
11
+ # Dropbox link
 
 
 
 
 
 
 
 
12
  link = 'https://www.dropbox.com/scl/fi/fjykwhhn9gu9t3kqrflxd/LA002-NOR-ZZ-ZZ-T-A-2403_Architectural-Specification-F10-Brick-and-Block-Walling_A4-_C01.pdf?rlkey=ek9i66i79m0hwp8z5yjs6rp5p&st=jh05a6qs&dl=0'
13
 
14
+ # Convert Dropbox link for direct download
15
  if 'dl=0' in link:
16
  link = link.replace('dl=0', 'dl=1')
17
 
18
+ # Download PDF
19
+ response = requests.get(link)
20
+ pdf_content = BytesIO(response.content) # Store the content in memory
 
 
 
 
 
 
 
 
 
 
21
 
22
+ # Save to a temporary file (optional, required for serving)
23
+ temp_file = "temp_downloaded_pdf.pdf"
24
+ with open(temp_file, 'wb') as f:
25
+ f.write(pdf_content.getbuffer())
 
 
 
 
 
 
26
 
27
+ # Serve the file
28
+ return send_file(temp_file, as_attachment=False)
 
 
 
 
29
 
30
+ # Route to display PDF in an iframe with navigation
31
+ @app.route('/pdf-viewer')
32
+ def pdf_viewer():
33
+ # Embed the iframe to navigate to page 2
34
+ html_content = '''
35
+ <!DOCTYPE html>
36
+ <html lang="en">
37
+ <head>
38
+ <meta charset="UTF-8">
39
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
40
+ <title>PDF Viewer</title>
41
+ </head>
42
+ <body>
43
+ <h1>PDF Viewer</h1>
44
+ <iframe
45
+ src="/view-pdf#page=2"
46
+ width="100%"
47
+ height="800px"
48
+ frameborder="0">
49
+ </iframe>
50
+ </body>
51
+ </html>
52
+ '''
53
+ return render_template_string(html_content)
54
 
55
  if __name__ == '__main__':
56
  app.run(host='0.0.0.0', port=7860)