-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
242 lines (212 loc) · 8.1 KB
/
Copy pathapp.py
File metadata and controls
242 lines (212 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from flask import Flask, render_template, request, send_file, flash
import os
from werkzeug.utils import secure_filename
from PyPDF2 import PdfReader, PdfWriter
import fitz # PyMuPDF
from pdf2docx import Converter
from docx2pdf import convert as docx2pdf
import pandas as pd
import img2pdf
from fpdf import FPDF
app = Flask(__name__)
app.secret_key = "supersecret"
UPLOAD_FOLDER = "uploads"
PROCESSED_FOLDER = "processed"
for folder in [UPLOAD_FOLDER, PROCESSED_FOLDER]:
if not os.path.exists(folder):
os.makedirs(folder)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
# ---------- Helper ----------
def save_file(file):
filename = secure_filename(file.filename)
path = os.path.join(UPLOAD_FOLDER, filename)
file.save(path)
return path
# ---------- Routes ----------
@app.route("/")
def index():
return render_template("index.html")
# ---------- PDF Operations ----------
@app.route("/encrypt", methods=["GET", "POST"])
def encrypt_pdf():
if request.method == "POST":
file = request.files["pdf"]
password = request.form["password"]
path = save_file(file)
reader = PdfReader(path)
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
writer.encrypt(password)
output_path = os.path.join(PROCESSED_FOLDER, "encrypted.pdf")
with open(output_path, "wb") as f:
writer.write(f)
return send_file(output_path, as_attachment=True)
return render_template("encrypt.html")
@app.route("/decrypt", methods=["GET", "POST"])
def decrypt_pdf():
if request.method == "POST":
file = request.files["pdf"]
password = request.form["password"]
path = save_file(file)
reader = PdfReader(path)
if reader.is_encrypted:
reader.decrypt(password)
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
output_path = os.path.join(PROCESSED_FOLDER, "decrypted.pdf")
with open(output_path, "wb") as f:
writer.write(f)
return send_file(output_path, as_attachment=True)
return render_template("decrypt.html")
@app.route("/split", methods=["GET", "POST"])
def split_pdf():
if request.method == "POST":
file = request.files["pdf"]
start = int(request.form["start"])
end = int(request.form["end"])
path = save_file(file)
reader = PdfReader(path)
writer = PdfWriter()
for i in range(start - 1, end):
writer.add_page(reader.pages[i])
output_path = os.path.join(PROCESSED_FOLDER, "split.pdf")
with open(output_path, "wb") as f:
writer.write(f)
return send_file(output_path, as_attachment=True)
return render_template("split.html")
@app.route("/compress", methods=["GET", "POST"])
def compress_pdf():
if request.method == "POST":
file = request.files["pdf"]
path = save_file(file)
doc = fitz.open(path)
output_path = os.path.join(PROCESSED_FOLDER, "compressed.pdf")
doc.save(output_path, deflate=True, garbage=4)
doc.close()
return send_file(output_path, as_attachment=True)
return render_template("compress.html")
@app.route("/nup", methods=["GET", "POST"])
def nup_pdf():
if request.method == "POST":
file = request.files["pdf"]
pages_per_sheet = int(request.form["nup"])
path = save_file(file)
doc = fitz.open(path)
new_doc = fitz.open()
rect = doc[0].rect
w, h = rect.width, rect.height
if pages_per_sheet == 2:
new_rect = fitz.Rect(0, 0, w * 2, h)
else:
new_rect = fitz.Rect(0, 0, w * 2, h * 2)
for i in range(0, len(doc), pages_per_sheet):
page = new_doc.new_page(width=new_rect.width, height=new_rect.height)
for j in range(pages_per_sheet):
if i + j >= len(doc):
break
if pages_per_sheet == 2:
rect_dest = fitz.Rect(j * w, 0, (j + 1) * w, h)
else:
rect_dest = fitz.Rect((j % 2) * w, (j // 2) * h,
(j % 2 + 1) * w, (j // 2 + 1) * h)
page.show_pdf_page(rect_dest, doc, i + j)
output_path = os.path.join(PROCESSED_FOLDER, "nup.pdf")
new_doc.save(output_path)
new_doc.close()
return send_file(output_path, as_attachment=True)
return render_template("nup.html")
@app.route("/merge", methods=["GET", "POST"])
def merge_pdf():
if request.method == "POST":
files = request.files.getlist("pdfs")
writer = PdfWriter()
for file in files:
path = save_file(file)
reader = PdfReader(path)
for page in reader.pages:
writer.add_page(page)
output_path = os.path.join(PROCESSED_FOLDER, "merged.pdf")
with open(output_path, "wb") as f:
writer.write(f)
return send_file(output_path, as_attachment=True)
return render_template("merge.html")
# ---------- Conversions ----------
@app.route("/word_to_pdf", methods=["GET", "POST"])
def word_to_pdf():
if request.method == "POST":
file = request.files["file"]
path = save_file(file)
output_path = os.path.join(PROCESSED_FOLDER, "converted.pdf")
docx2pdf(path, output_path)
return send_file(output_path, as_attachment=True)
return render_template("word_to_pdf.html")
@app.route("/pdf_to_word", methods=["GET", "POST"])
def pdf_to_word():
if request.method == "POST":
file = request.files["file"]
path = save_file(file)
output_path = os.path.join(PROCESSED_FOLDER, "converted.docx")
cv = Converter(path)
cv.convert(output_path, start=0, end=None)
cv.close()
return send_file(output_path, as_attachment=True)
return render_template("pdf_to_word.html")
@app.route("/excel_to_pdf", methods=["GET", "POST"])
def excel_to_pdf():
if request.method == "POST":
file = request.files["file"]
path = save_file(file)
df = pd.read_excel(path)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=10)
for row in df.values.tolist():
pdf.cell(200, 10, txt=" | ".join(map(str, row)), ln=1)
output_path = os.path.join(PROCESSED_FOLDER, "converted.pdf")
pdf.output(output_path)
return send_file(output_path, as_attachment=True)
return render_template("excel_to_pdf.html")
@app.route("/pdf_to_excel", methods=["GET", "POST"])
def pdf_to_excel():
if request.method == "POST":
file = request.files["file"]
path = save_file(file)
doc = fitz.open(path)
text = ""
for page in doc:
text += page.get_text()
df = pd.DataFrame({"content": [text]})
output_path = os.path.join(PROCESSED_FOLDER, "converted.xlsx")
df.to_excel(output_path, index=False)
return send_file(output_path, as_attachment=True)
return render_template("pdf_to_excel.html")
@app.route("/image_to_pdf", methods=["GET", "POST"])
def image_to_pdf():
if request.method == "POST":
file = request.files["file"]
path = save_file(file)
output_path = os.path.join(PROCESSED_FOLDER, "converted.pdf")
with open(output_path, "wb") as f:
f.write(img2pdf.convert(path))
return send_file(output_path, as_attachment=True)
return render_template("image_to_pdf.html")
@app.route("/pdf_to_image", methods=["GET", "POST"])
def pdf_to_image():
if request.method == "POST":
file = request.files["file"]
path = save_file(file)
doc = fitz.open(path)
images = []
for i, page in enumerate(doc):
pix = page.get_pixmap()
output_path = os.path.join(PROCESSED_FOLDER, f"page_{i+1}.png")
pix.save(output_path)
images.append(output_path)
return send_file(images[0], as_attachment=True)
return render_template("pdf_to_image.html")
if __name__ == "__main__":
import os
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=False)