-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_watermark.py
executable file
·308 lines (245 loc) · 8.07 KB
/
pdf_watermark.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
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
#!/usr/bin/env python
import pdfquery
from PyPDF2 import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.setrecursionlimit(3000)
#Custom Libraries
from inv import *
def main():
#1. Loads up the PDF and gets the number of pages
if len(sys.argv) >= 2:
pdf_name = sys.argv[1]
if len(sys.argv) > 2:
log_name = pdf_name + ".log.txt"
print "Check Logfile (" + log_name + ")"
sys.stdout = open(log_name, 'w')
print "About to open " + str(pdf_name)
sys.stdout.flush()
else:
#pdf_name = "binder_combined copy.pdf"
exit("Error: Please Input a PDF")
pdf = pdfquery.PDFQuery(pdf_name)
pdf_count = pdf.doc.catalog['Pages'].resolve()['Count']
#2. Create Invoices Object, which A) holds an array of invoices
# and B) holds
invoices = Invoices()
count = 0;
for page in range(pdf_count):
print "About to load " + str(page+1),
sys.stdout.flush()
try:
pdf.load(page)
except:
print "ERROR: Couldn't load page " + str(page+1)
sys.stdout.flush()
invoices.addUnknownInvoicePage();
continue
identifiers = invoices.getIdentifiers();
foundPage = False;
for identifier in identifiers:
pdf_id = pdf.pq('LTTextLineHorizontal:contains("'+str(identifier)+'")')
if(pdf_id):
print identifier,
searchFunction = identifiers[identifier];
#Call the search function with a blank invoice Object
blank_obj = Invoice();
obj = searchFunction(blank_obj,pdf )
invoices.add (page, searchFunction(Invoice(),pdf) )
#print "Invoice:",obj.num,"PO:",obj.po,"JOB:",obj.job
obj.printInvoice()
foundPage = True;
break;
if(not foundPage):
invoices.addUnknownInvoicePage();
print ""
printToPDF(pdf_name,invoices)
#Pass in invoice, and current page
def getWaterMarkedPage(invoice,page):
invoice_string = ''
job_string = ''
po_string = ''
if(invoice != None):
invoice.cleanInvoice()
if(invoice.num == None):
invoice_string = ""
else:
invoice_string = str(invoice.num)
if(invoice.job == None):
invoice.job = ""
else:
job_string = str(invoice.job)
if(invoice.po == None):
invoice.po = ""
else:
po_string = str(invoice.po)
packet = StringIO.StringIO()
##
can = canvas.Canvas(packet, pagesize=letter)
can.setStrokeColor('red')
can.setFillColorRGB(1,0,0)
can.setLineWidth(5)
can.setFont('Helvetica-Bold', 16)
#can.rect(x-20, y_min-20, 400, 100)
x=80
#x_max = 300
y_min = 375
y_max = y_min + 60
text_space = 30
string_max_len = 34
y_stack = [y_max]
if(len(invoice_string) < string_max_len):
last_y = y_stack[-1]
y_stack.append(last_y - text_space)
can.drawString(x, last_y, "INVOICE #: " + invoice_string)
if(len(job_string) < string_max_len):
last_y = y_stack[-1]
y_stack.append(last_y - text_space)
can.drawString(x, last_y, "JOB #: " + job_string)
else:
while len(job_string) > 0:
last_y = y_stack[-1]
y_stack.append(last_y - text_space)
can.drawString(x, last_y, "JOB #: " + job_string[0:string_max_len])
job_string = job_string[string_max_len:]
if(len(po_string) < string_max_len):
last_y = y_stack[-1]
y_stack.append(last_y - text_space)
can.drawString(x, last_y, "PO #: " + po_string)
width = 400
#height = 500
#Shift pixels based on how many extra lines were added
pixelShift = (len(y_stack) - 3) * 15
can.rect(x-10,y_max-80 - pixelShift,width+20,120+pixelShift)
#str1 = str(x-10)
#str2 = str(y_max-60)
#can.drawString(x+50,y_max+50,"X:{0} Y:{1}".format(str1,str2));
can.save()
packet.seek(0)
watermark = PdfFileReader(packet,strict=False)
pp=watermark.getPage(0)
try:
rotation = page['/Rotate']
except:
rotation = 0
if(rotation!=0):
page.mergeRotatedTranslatedPage(pp, rotation, pp.mediaBox.getWidth()/2, pp.mediaBox.getWidth()/2)
else:
page.mergePage(pp)
return page;
#missing the PO or Job
def printToPDF(pdf_name,invoices):
# read your existing PDF
existing_pdf = PdfFileReader(file(pdf_name, "rb"),strict=False)
output_file = pdf_name + ".watermarked.pdf"
invoices = invoices.getInvoices()
# Number of pages in input document
page_count = existing_pdf.getNumPages()
#PDF output
outputPDF = PdfFileWriter()
incomplete_pages = []
actual_page = 1;
for p in range(page_count):
invoice = invoices[p];
if(invoice != None):
complete_flag = invoice.setCompletionFlag()
else:
complete_flag = False
#CHECK TO SEE IF PAGE IS BROKEN
#Create a watermarked version of the page
watermarked_page = getWaterMarkedPage(invoice,existing_pdf.getPage(p))
#Print the page with the PDF
if(complete_flag == True):
outputPDF.addPage(watermarked_page)
print "Added COMPLETE PDF page # " + str(p + 1) + " (Page " + str(actual_page) + ")"
sys.stdout.flush()
actual_page+=1;
else:
incomplete_pages.append([watermarked_page,p])
'''
#Don't move pages that don't have invoice, assume they are placeholder pages
elif(complete_flag == False and invoice.unknown_invoice == True):
outputPDF.addPage(watermarked_page)
print "Added NON INVOICE PDF page # " + str(p + 1)+" (Page " + str(actual_page) + ")"
actual_page+=1;
#Save the PDF page for printing later
else:
incomplete_pages.append([watermarked_page,p])
'''
#Add incomplete pages
for page in incomplete_pages:
outputPDF.addPage(page[0]);
print "Added [missing PO or JOB#] PDF page # " + str(page[1] + 1) + " (Page " + str(actual_page) + ")"
actual_page+=1;
sys.stdout.flush()
# finally, write "output" to a real file
#new_pdf = PdfFileReader(packet)
outputStream = file(output_file, "wb")
outputPDF.write(outputStream)
outputStream.close()
print "success: printed PDF to "+ output_file
def printToPDF_multiple(pdf_name,invoices):
# read your existing PDF
existing_pdf = PdfFileReader(file(pdf_name, "rb"),strict=False)
output_file = pdf_name + ".watermarked.pdf"
output_file_incomplete = pdf_name + ".watermarked.incomplete.pdf"
output_file_missing = pdf_name + ".watermarked.missing.pdf"
invoices = invoices.getInvoices()
# Number of pages in input document
page_count = existing_pdf.getNumPages()
#PDF output
outputPDF = PdfFileWriter()
outputPDF_incomplete = PdfFileWriter()
outputPDF_missing = PdfFileWriter()
incomplete_pages = []
actual_page = 1;
for p in range(page_count):
invoice = invoices[p];
if(invoice != None):
complete_flag = invoice.setCompletionFlag()
else:
complete_flag = False
#CHECK TO SEE IF PAGE IS BROKEN
#Create a watermarked version of the page
watermarked_page = getWaterMarkedPage(invoice,existing_pdf.getPage(p))
#Print the page with the PDF
if(complete_flag == True):
outputPDF.addPage(watermarked_page)
print "Added COMPLETE PDF page # " + str(p + 1) + " (Page " + str(actual_page) + ")"
actual_page+=1;
#Don't move pages that don't have invoice, assume they are placeholder pages
elif(complete_flag == False and invoice.unknown_invoice == True):
outputPDF_missing.addPage(watermarked_page)
print "Added NON INVOICE PDF page # " + str(p + 1)+" (Page " + str(actual_page) + ")"
actual_page+=1;
#Save the PDF page for printing later
else:
incomplete_pages.append([watermarked_page,p])
sys.stdout.flush()
#Add incomplete pages
for page in incomplete_pages:
outputPDF_incomplete.addPage(page[0]);
print "Added [missing PO or JOB#] PDF page # " + str(page[1] + 1) + " (Page " + str(actual_page) + ")"
actual_page+=1;
sys.stdout.flush()
# finally, write "output" to a real file
#new_pdf = PdfFileReader(packet)
outputStream = file(output_file, "wb")
outputPDF.write(outputStream)
outputStream.close()
print "success: printed PDF to "+ output_file
outputStream = file(output_file_incomplete, "wb")
outputPDF_incomplete.write(outputStream)
outputStream.close()
print "success: printed PDF to "+ output_file_incomplete
outputStream = file(output_file_missing, "wb")
outputPDF_missing.write(outputStream)
outputStream.close()
print "success: printed PDF to "+ output_file_missing
#When printing to PDF, if previous invoice number matches current invoice number, and no PO, copy over PO
if __name__ == '__main__':
main()