-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
603 lines (471 loc) · 21.7 KB
/
Copy pathapp.py
File metadata and controls
603 lines (471 loc) · 21.7 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
from flask import Flask, render_template, request, send_file
import os
import fitz # PyMuPDF
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import io
from PyPDF2 import PdfMerger # ✅ Move import to top
import re
import zipfile
from difflib import get_close_matches
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
@app.route('/')
def index():
return render_template('index.html')
from zipfile import ZipFile
@app.route('/invert', methods=['GET', 'POST'])
def invert():
if request.method == 'POST':
files = request.files.getlist('pdf')
if not files or files[0].filename == '':
return 'No files selected.'
inverted_files = []
for file in files:
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(input_path)
output_name = 'inverted_' + file.filename
output_path = os.path.join(UPLOAD_FOLDER, output_name)
doc = fitz.open(input_path)
new_doc = fitz.open()
for page in doc:
pix = page.get_pixmap()
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
arr = np.array(img)
inverted_arr = 255 - arr
inverted_img = Image.fromarray(inverted_arr)
img_bytes = io.BytesIO()
inverted_img.save(img_bytes, format='PNG')
img_bytes.seek(0)
rect = fitz.Rect(0, 0, pix.width, pix.height)
new_page = new_doc.new_page(width=rect.width, height=rect.height)
new_page.insert_image(rect, stream=img_bytes.read())
new_doc.save(output_path)
inverted_files.append(output_path)
# Create zip file of all inverted PDFs
zip_path = os.path.join(UPLOAD_FOLDER, 'inverted_pdfs.zip')
with ZipFile(zip_path, 'w') as zipf:
for file_path in inverted_files:
zipf.write(file_path, os.path.basename(file_path))
os.remove(file_path) # Clean up individual files
return send_file(zip_path, as_attachment=True)
return render_template('invert.html')
@app.route('/merge', methods=['GET', 'POST'])
def merge():
if request.method == 'POST':
files = request.files.getlist('pdfs')
if not files or files[0].filename == '':
return 'No files selected.'
merger = PdfMerger()
saved_files = []
for file in files:
path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(path)
saved_files.append(path)
merger.append(path)
output_path = os.path.join(UPLOAD_FOLDER, 'merged_output.pdf')
merger.write(output_path)
merger.close()
# Clean up temporary files
for f in saved_files:
os.remove(f)
return send_file(output_path, as_attachment=True)
return render_template('merge.html')
from zipfile import ZipFile
@app.route('/split', methods=['GET', 'POST'])
def split_pdf():
if request.method == 'POST':
files = request.files.getlist('pdfs')
if not files or files[0].filename == '':
return 'No files selected.'
split_paths = []
for file in files:
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(input_path)
doc = fitz.open(input_path)
for i in range(len(doc)):
new_doc = fitz.open()
new_doc.insert_pdf(doc, from_page=i, to_page=i)
split_name = f"{os.path.splitext(file.filename)[0]}_page{i+1}.pdf"
split_path = os.path.join(UPLOAD_FOLDER, split_name)
new_doc.save(split_path)
split_paths.append(split_path)
# Create ZIP of all split PDFs
zip_path = os.path.join(UPLOAD_FOLDER, 'split_pages.zip')
with ZipFile(zip_path, 'w') as zipf:
for path in split_paths:
zipf.write(path, os.path.basename(path))
os.remove(path) # Clean up individual files
return send_file(zip_path, as_attachment=True)
return render_template('split.html')
@app.route('/compress', methods=['GET', 'POST'])
def compress_pdf():
if request.method == 'POST':
files = request.files.getlist('pdfs')
if not files or files[0].filename == '':
return 'No PDF files selected.'
compressed_paths = []
for file in files:
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(input_path)
doc = fitz.open(input_path)
new_doc = fitz.open()
for page in doc:
# Render the page with reduced resolution (e.g., 100 DPI instead of default)
pix = page.get_pixmap(dpi=100)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
# Compress image by lowering quality
img_bytes = io.BytesIO()
img.save(img_bytes, format="JPEG", quality=50) # Lower quality = more compression
img_bytes.seek(0)
rect = fitz.Rect(0, 0, pix.width, pix.height)
new_page = new_doc.new_page(width=rect.width, height=rect.height)
new_page.insert_image(rect, stream=img_bytes.read())
output_filename = os.path.splitext(file.filename)[0] + "_compressed.pdf"
output_path = os.path.join(UPLOAD_FOLDER, output_filename)
new_doc.save(output_path)
compressed_paths.append(output_path)
# Package into a ZIP
zip_path = os.path.join(UPLOAD_FOLDER, 'compressed_pdfs.zip')
with ZipFile(zip_path, 'w') as zipf:
for path in compressed_paths:
zipf.write(path, os.path.basename(path))
os.remove(path) # Clean up
return send_file(zip_path, as_attachment=True)
return render_template('compress.html')
# Helper to convert hex to RGBA tuple with alpha
def hex_to_rgba_int(hex_color, alpha=100):
hex_color = hex_color.lstrip('#')
if len(hex_color) != 6:
raise ValueError("Invalid hex color format")
r = int(hex_color[0:2], 16)
g = int(hex_color[2:4], 16)
b = int(hex_color[4:6], 16)
return (r, g, b, alpha)
@app.route('/watermark', methods=['GET', 'POST'])
def watermark_pdf():
if request.method == 'POST':
files = request.files.getlist('pdfs')
watermark_text = request.form.get('watermark_text')
font_size = int(request.form.get('font_size', 20))
font_color = request.form.get('font_color', '#000000')
rotation = float(request.form.get('rotation', 0))
position = request.form.get('position', 'center')
custom_x = int(request.form.get('custom_x', 100))
custom_y = int(request.form.get('custom_y', 100))
if not files or not watermark_text:
return 'Please upload PDF(s) and enter watermark text.'
output_files = []
for file in files:
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(input_path)
doc = fitz.open(input_path)
for page in doc:
rect = page.rect
width, height = rect.width, rect.height
# Determine watermark coordinates
if position == 'top':
x, y = width / 2, 50
elif position == 'center':
x, y = width / 2, height / 2
elif position == 'bottom':
x, y = width / 2, height - 50
else: # custom
x, y = custom_x, custom_y
# Create watermark image using PIL
canvas_width = 1000
canvas_height = 200
image = Image.new("RGBA", (canvas_width, canvas_height), (255, 255, 255, 0))
draw = ImageDraw.Draw(image)
try:
font = ImageFont.truetype("arial.ttf", font_size)
except IOError:
font = ImageFont.load_default()
# Use getbbox for text dimension
bbox = font.getbbox(watermark_text)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
draw.text(
((canvas_width - text_width) // 2, (canvas_height - text_height) // 2),
watermark_text,
font=font,
fill=hex_to_rgba_int(font_color, alpha=100)
)
rotated_img = image.rotate(rotation, expand=True)
img_buffer = io.BytesIO()
rotated_img.save(img_buffer, format="PNG")
img_buffer.seek(0)
# Define placement rectangle on PDF
img_rect = fitz.Rect(
x - rotated_img.width / 2,
y - rotated_img.height / 2,
x + rotated_img.width / 2,
y + rotated_img.height / 2
)
page.insert_image(img_rect, stream=img_buffer.read(), overlay=True)
# Save the final PDF
output_filename = 'watermarked_' + file.filename
output_path = os.path.join(UPLOAD_FOLDER, output_filename)
doc.save(output_path)
output_files.append(output_path)
if len(output_files) == 1:
return send_file(output_files[0], as_attachment=True)
# If multiple PDFs, zip them
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
for filepath in output_files:
zipf.write(filepath, os.path.basename(filepath))
zip_buffer.seek(0)
return send_file(zip_buffer, as_attachment=True, download_name='watermarked_files.zip', mimetype='application/zip')
return render_template('watermark.html')
@app.route('/rotate', methods=['GET', 'POST'])
def rotate_pdf():
if request.method == 'POST':
files = request.files.getlist('pdfs')
rotate_target = request.form.get('rotate_target') # 'portrait' or 'landscape'
angle = int(request.form.get('angle', 90))
direction = request.form.get('direction', 'clockwise')
if not files or not rotate_target or not direction:
return "Please upload PDF(s) and choose rotation settings."
# Flip angle if anticlockwise
if direction == 'clockwise':
angle = -angle
output_files = []
for file in files:
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(input_path)
doc = fitz.open(input_path)
new_doc = fitz.open()
for page in doc:
width, height = page.rect.width, page.rect.height
orientation = 'landscape' if width > height else 'portrait'
pix = page.get_pixmap(dpi=150)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
if orientation == rotate_target:
img = img.rotate(angle, expand=True)
img_bytes = io.BytesIO()
img.save(img_bytes, format='PNG')
img_bytes.seek(0)
rotated_width, rotated_height = img.size
rect = fitz.Rect(0, 0, rotated_width, rotated_height)
new_page = new_doc.new_page(width=rotated_width, height=rotated_height)
new_page.insert_image(rect, stream=img_bytes.read())
output_filename = f"rotated_{file.filename}"
output_path = os.path.join(UPLOAD_FOLDER, output_filename)
new_doc.save(output_path)
output_files.append(output_path)
if len(output_files) == 1:
return send_file(output_files[0], as_attachment=True)
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
for f in output_files:
zipf.write(f, os.path.basename(f))
zip_buffer.seek(0)
return send_file(zip_buffer, as_attachment=True, download_name='rotated_pdfs.zip')
return render_template('rotate.html')
@app.route('/add_blank', methods=['GET', 'POST'])
def add_blank():
if request.method == 'POST':
files = request.files.getlist('pdfs')
position = request.form.get('position')
after_page = int(request.form.get('after_page', 0)) - 1 # user enters 1-based
num_blanks = int(request.form.get('num_blanks', 1))
bg_color_hex = request.form.get('bg_color', '#FFFFFF')
bg_rgb = tuple(int(bg_color_hex.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
if not files:
return 'Please upload at least one PDF.'
output_files = []
for file in files:
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(input_path)
doc = fitz.open(input_path)
# Get size from first page
ref_page = doc[0]
width = ref_page.rect.width
height = ref_page.rect.height
# Determine insertion point
if position == 'start':
insert_at = 0
elif position == 'end':
insert_at = len(doc)
elif position == 'after' and 0 <= after_page < len(doc):
insert_at = after_page + 1
else:
return f"Invalid page number for file: {file.filename}"
# Create blank image and insert
for i in range(num_blanks):
img = Image.new("RGB", (int(width), int(height)), color=bg_rgb)
img_buffer = io.BytesIO()
img.save(img_buffer, format="PNG")
img_buffer.seek(0)
page_index = insert_at + i
doc.insert_page(page_index, width=width, height=height)
page = doc[page_index]
rect = page.rect
page.insert_image(rect, stream=img_buffer.read())
# Save output
output_path = os.path.join(UPLOAD_FOLDER, 'blanked_' + file.filename)
doc.save(output_path)
output_files.append(output_path)
if len(output_files) == 1:
return send_file(output_files[0], as_attachment=True)
# Multiple files: return zipped
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
for path in output_files:
zipf.write(path, os.path.basename(path))
zip_buffer.seek(0)
return send_file(zip_buffer, as_attachment=True, download_name='blanked_files.zip', mimetype='application/zip')
return render_template('add_blank.html')
@app.route('/paste_image', methods=['GET', 'POST'])
def paste_image():
if request.method == 'POST':
files = request.files.getlist('pdfs')
image_file = request.files.get('image')
page_number = int(request.form.get('page_number', 1)) - 1
x = int(request.form.get('x', 0))
y = int(request.form.get('y', 0))
use_original_size = request.form.get('use_original_size') == 'yes'
if not files or not image_file:
return 'Upload at least one PDF and one image.'
image = Image.open(image_file)
# Calculate image dimensions
if use_original_size:
# Convert pixels to points (assuming 72 DPI)
dpi = image.info.get('dpi', (72, 72))
img_width_pt = (image.width / dpi[0]) * 72
img_height_pt = (image.height / dpi[1]) * 72
else:
img_width_pt = int(request.form.get('img_width', 100))
img_height_pt = int(request.form.get('img_height', 100))
image = image.resize((int(img_width_pt), int(img_height_pt)))
# Prepare image buffer
img_buffer = io.BytesIO()
image.save(img_buffer, format="PNG")
img_buffer.seek(0)
img_data = img_buffer.read()
output_files = []
for file in files:
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(input_path)
doc = fitz.open(input_path)
if page_number < 0 or page_number >= len(doc):
return f"Invalid page number for {file.filename}"
page = doc[page_number]
rect = fitz.Rect(x, y, x + img_width_pt, y + img_height_pt)
page.insert_image(rect, stream=img_data)
output_path = os.path.join(UPLOAD_FOLDER, 'image_added_' + file.filename)
doc.save(output_path)
output_files.append(output_path)
if len(output_files) == 1:
return send_file(output_files[0], as_attachment=True)
# Multiple files: zip them
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
for path in output_files:
zipf.write(path, os.path.basename(path))
zip_buffer.seek(0)
return send_file(zip_buffer, as_attachment=True, download_name='pasted_images.zip', mimetype='application/zip')
return render_template('paste_image.html')
@app.route('/edit_pages', methods=['GET', 'POST'])
def edit_pages():
if request.method == 'POST':
pdf_file = request.files.get('pdf')
action = request.form.get('action')
if not pdf_file or not action:
return "Please upload a PDF and choose an action."
input_path = os.path.join(UPLOAD_FOLDER, pdf_file.filename)
pdf_file.save(input_path)
doc = fitz.open(input_path)
if action == 'remove':
remove_str = request.form.get('remove_pages', '')
pages_to_remove = parse_page_numbers(remove_str, len(doc))
for i in sorted(pages_to_remove, reverse=True):
if 0 <= i < len(doc):
doc.delete_page(i)
elif action == 'cutpaste':
cut_page = int(request.form.get('cut_page', 0)) - 1
paste_after = int(request.form.get('paste_after_page', -1))
if 0 <= cut_page < len(doc):
cut_pdf = fitz.open()
cut_pdf.insert_pdf(doc, from_page=cut_page, to_page=cut_page)
doc.delete_page(cut_page)
paste_at = paste_after if paste_after < len(doc) else len(doc) - 1
doc.insert_pdf(cut_pdf, start_at=paste_at + 1)
elif action == 'swap':
a = int(request.form.get('page_a', 0)) - 1
b = int(request.form.get('page_b', 0)) - 1
if 0 <= a < len(doc) and 0 <= b < len(doc) and a != b:
temp_a = fitz.open()
temp_b = fitz.open()
temp_a.insert_pdf(doc, from_page=a, to_page=a)
temp_b.insert_pdf(doc, from_page=b, to_page=b)
doc.delete_page(max(a, b))
doc.delete_page(min(a, b))
doc.insert_pdf(temp_b, start_at=min(a, b))
doc.insert_pdf(temp_a, start_at=max(a, b))
output = io.BytesIO()
doc.save(output)
output.seek(0)
return send_file(output, as_attachment=True, download_name='edited.pdf', mimetype='application/pdf')
return render_template('edit_pages.html')
def parse_page_numbers(page_str, total_pages):
result = set()
if not page_str:
return result
parts = page_str.replace(' ', '').split(',')
for part in parts:
if '-' in part:
start, end = part.split('-')
result.update(range(int(start) - 1, int(end)))
else:
result.add(int(part) - 1)
return [p for p in result if 0 <= p < total_pages]
@app.route('/batch_invert', methods=['GET', 'POST'])
def batch_invert():
if request.method == 'POST':
files = request.files.getlist('pdfs')
if not files:
return "<h3>No files uploaded.</h3>"
output_files = []
for file in files:
try:
# Open uploaded file using PyMuPDF
input_pdf = fitz.open(stream=file.read(), filetype="pdf")
new_doc = fitz.open()
for page in input_pdf:
pix = page.get_pixmap()
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
# Invert the colors
arr = np.array(img)
inverted_arr = 255 - arr
inverted_img = Image.fromarray(inverted_arr)
# Convert back to PDF page
img_stream = io.BytesIO()
inverted_img.save(img_stream, format="PNG")
img_stream.seek(0)
rect = fitz.Rect(0, 0, pix.width, pix.height)
new_page = new_doc.new_page(width=rect.width, height=rect.height)
new_page.insert_image(rect, stream=img_stream.read())
# Save final inverted PDF to memory
buffer = io.BytesIO()
new_doc.save(buffer)
buffer.seek(0)
output_files.append((file.filename.replace('.pdf', '_inverted.pdf'), buffer))
except Exception as e:
return f"<h3>Error processing {file.filename}: {e}</h3>"
# Return single PDF or ZIP depending on file count
if len(output_files) == 1:
filename, file_buffer = output_files[0]
return send_file(file_buffer, as_attachment=True, download_name=filename, mimetype='application/pdf')
else:
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
for filename, buffer in output_files:
zipf.writestr(filename, buffer.read())
zip_buffer.seek(0)
return send_file(zip_buffer, as_attachment=True, download_name="inverted_pdfs.zip", mimetype="application/zip")
return render_template('batch_invert.html')
if __name__ == '__main__':
app.run(debug=True)