-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatermarker.py
90 lines (68 loc) · 2.76 KB
/
watermarker.py
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
from os import walk
from reportlab.pdfgen import canvas
from PyPDF2 import PdfFileWriter, PdfFileReader
def get_pdf_list():
r = []
l = []
for (dirpath, dirnames, filenames) in walk("./left-watermarker"):
l.extend(filenames)
break
for (dirpath, dirnames, filenames) in walk("./right-watermarker"):
r.extend(filenames)
break
return r, l
canvas_width = 566
# Create the watermark from an image
c = canvas.Canvas('bin/right-watermark.pdf')
c.drawImage('bin/logo.png', canvas_width - 4, 2, width=30, height=30,
mask='auto', preserveAspectRatio=True)
c.drawString(canvas_width - 73, 12, "SNEEDS.IR")
c.save()
c = canvas.Canvas('bin/left-watermark.pdf')
c.drawImage('bin/logo.png', 2, 2, width=30, height=30, mask='auto')
c.drawString(37, 12, "SNEEDS.IR")
c.save()
right_pdf_list, left_pdf_list = get_pdf_list()
# Solving mac problem
# if right_pdf_list[0] == ".DS_Store":
# del (right_pdf_list[0])
# if left_pdf_list[0] == ".DS_Store":
# del (left_pdf_list[0])
print("\nRIGHT PDFs : ")
for booklet_name in right_pdf_list:
try:
watermark = PdfFileReader(open("bin/right-watermark.pdf", "rb"))
output_file = PdfFileWriter()
input_file = PdfFileReader(
open("right-watermarker/" + booklet_name, "rb"))
page_count = input_file.getNumPages()
# Go through all the left-watermarker file pages to add a watermark to them
for page_number in range(page_count):
print(
booklet_name + ": Watermarking page {} of {}".format(page_number, page_count - 1))
input_page = input_file.getPage(page_number)
input_page.mergePage(watermark.getPage(0))
output_file.addPage(input_page)
with open("output/" + booklet_name, "wb") as outputStream:
output_file.write(outputStream)
except:
print("Can't watermark one PDF.")
print("\nLEFT PDFs : ")
for booklet_name in left_pdf_list:
try:
watermark = PdfFileReader(open("bin/left-watermark.pdf", "rb"))
output_file = PdfFileWriter()
input_file = PdfFileReader(
open("left-watermarker/" + booklet_name, "rb"))
page_count = input_file.getNumPages()
# Go through all the left-watermarker file pages to add a watermark to them
for page_number in range(page_count):
print(
booklet_name + ": Watermarking page {} of {}".format(page_number, page_count - 1))
input_page = input_file.getPage(page_number)
input_page.mergePage(watermark.getPage(0))
output_file.addPage(input_page)
with open("output/" + booklet_name, "wb") as outputStream:
output_file.write(outputStream)
except:
print("Can't watermark one PDF.")