Jul-14-2021, 03:36 AM
I'm no expert like some of the people here, but this works.
A while ago the gf worked for an Adult Education Centre. One of her jobs was to scan old exams to pdf, then watermark them with the company's logo. I made a bit of Python to do the job.
In fact, I had to learn how to do all kinds of tricks with pdfs!! Gotta keep her happy!
I made this to put the watermark picture over the text. It works fine. You can adapt it to put your picture where you want it.
Just vary the x and y values.
You can also batch this by making 2 lists of pdfs and pictures and merge them one by one.
A while ago the gf worked for an Adult Education Centre. One of her jobs was to scan old exams to pdf, then watermark them with the company's logo. I made a bit of Python to do the job.
In fact, I had to learn how to do all kinds of tricks with pdfs!! Gotta keep her happy!
I made this to put the watermark picture over the text. It works fine. You can adapt it to put your picture where you want it.
Just vary the x and y values.
You can also batch this by making 2 lists of pdfs and pictures and merge them one by one.
#! /usr/bin/python3 # program to put a water mark in a pdf import os from reportlab.pdfgen import canvas from PyPDF2 import PdfFileWriter, PdfFileReader # after scanning and merging, the exams are here pathToMergedFiles = '/home/pedro/babystuff/mergedPdf/' # save the watermarked files here pathToWatermarkedFiles = '/home/pedro/babystuff/watermarkedPDFs/' # get the file names files = os.listdir(pathToMergedFiles) # filter out the files you don't want pdfNames = [] for file in files: if file.endswith('.pdf'): pdfNames.append(file) print('Where do you want the watermark on the pdf?') print('0,0 seems to be the bottom left corner of the page.') print('the values x = 120, y = 680 works for the first pdf') print('For full page watermark set x = 10, y = 10 something like that') print('enter the x value ...') xvalue = input() x = int(xvalue) print('enter the y value, bottom of the page is zero') print('enter the y value, top of the page is 720+') print('For full page watermark set x = 10, y = 10 something like that') print('enter the y value') yvalue = input() y = int(yvalue) print('Tell me the name of the watermark file.') print('enter something like purplerectangle.png or whiterectangle.png') print('this should be a .png') # get the name of the watermark picture wmfileName = input() # Create the watermark.pdf from an image c = canvas.Canvas(pathToMergedFiles + 'watermark.pdf') # Draw the image at x, y. I positioned the x,y to be where I like here c.drawImage(pathToMergedFiles + wmfileName, x, y, mask='auto') c.save() # Get the watermark pdf file you just created watermark = PdfFileReader(open(pathToMergedFiles + 'watermark.pdf', 'rb')) # Get our files ready this is for 1 pdf file print('What pdf file do you want to watermark?') print('Just enter a name like test2.pdf') pdfTowatermark = input() outputName = pdfTowatermark.split('.') saveFilename = outputName[0] + '_wmed.pdf' output_file = PdfFileWriter() input_file = PdfFileReader(open(pathToMergedFiles + pdfTowatermark, 'rb')) # Number of pages in input document page_count = input_file.getNumPages() ##print('Now merging the watermark and the first page ...') ### this just puts the watermark on the first page, page zero in a pdf ### Get rid of this if you want to watermark each page ## ##input_page = input_file.getPage(0) ##input_page.mergePage(watermark.getPage(0)) ##output_file.addPage(input_page) # add the rest of the pages without the rectangle # this puts the watermark on every page for page_number in range(0, page_count): #print('Now adding the other pages without the watermark ...') print('Watermarking page {} of {}'.format(page_number, page_count)) # merge the watermark with the page input_page = input_file.getPage(page_number) input_page.mergePage(watermark.getPage(0)) # add page from input file to output document output_file.addPage(input_page) # finally, write "output" to document-output.pdf with open(pathToWatermarkedFiles + saveFilename, "wb") as outputStream: output_file.write(outputStream) # get rid of the watermark.pdf file, ready for next time if it changes os.remove(pathToMergedFiles + 'watermark.pdf') print('All done!')