Marthee commited on
Commit
66783ec
·
verified ·
1 Parent(s): edeef68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -41
app.py CHANGED
@@ -1,56 +1,32 @@
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)
 
1
+ from flask import Flask, send_file, render_template
2
  import requests
3
  from io import BytesIO
4
  import os
5
 
6
+ app = Flask(__name__)
7
 
8
+ # Route to serve the PDF in iframe
9
  @app.route('/view-pdf')
10
  def view_pdf():
 
11
  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'
12
+
13
+ # Modify Dropbox link for direct download
14
  if 'dl=0' in link:
15
  link = link.replace('dl=0', 'dl=1')
16
 
17
+ # Download PDF content
18
  response = requests.get(link)
19
+ if response.status_code == 200:
20
+ pdf_path = 'temp_view.pdf'
21
+ with open(pdf_path, 'wb') as f:
22
+ f.write(response.content)
23
+
24
+ # Serve the PDF file
25
+ return send_file(pdf_path, mimetype='application/pdf')
26
+ else:
27
+ return "Failed to download PDF content", 404
28
+
29
+ # Run Flask server
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  if __name__ == '__main__':
32
  app.run(host='0.0.0.0', port=7860)