PR proposal watermark/background on every pdf page

Hi !

Good day! I’m John from http://bai.ph/
We came up to add up a feature that allows adding background images/watermark in every page of pdf files.

Current stage: we are able to repeat the images every pdf page

the image that we want to be repeated is the one on the left-most part

here’s what we did:

added this function to /frappe-bench/apps/frappe/frappe/utils/pdf.py

def append_pdf_test(fname):
from reportlab.pdfgen import canvas
from PyPDF2 import PdfFileWriter, PdfFileReader
import getpass

# Create the watermark from an image
c = canvas.Canvas('watermark.pdf')
# Draw the image at x, y. I positioned the x,y to be where i like here
c.drawImage('/home/'+getpass.getuser()+'/frappe-bench/sites/development/public/files/Side.png', 0, 250)
c.save()
# Get the watermark file you just created
watermark = PdfFileReader(open("watermark.pdf", "rb"))
# Get our files ready
output_file = PdfFileWriter()
input_file = PdfFileReader(open(fname, "rb"))
#input_file = filedata
# Number of pages in input document
page_count = input_file.getNumPages()
# Go through all the input file pages to add a watermark to them
for page_number in range(page_count):
    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
fname = "/home/"++getpass.getuser()++"/document-output.pdf"
with open(fname, "wb") as outputStream:
    output_file.write(outputStream)
return fname

Full file

Our next step is to add fields in Letter head, the fields are: Image file/path, X position, Y position, and a child table in where we can select which print format will this background/watermark will show.

UPDATE!

added watermark Type Attach Image in Letter Head doctype

Credits to our client http://code.ag/ for opening up this idea.

We are open for suggestions, if anybody else has a better approach on this. Looking forward

6 Likes

Hi, it is very great feature! Thank you. Sorry I don’t have any idea about approach only question. This watermark will be shown every times or it will be the checkbox in print view and you can choose when you want to show it?

2 Likes

As of the moment it is shown every time you click PDF, but thanks for bringing up the idea of having a checkbox whether to show the watermark or not

Could you help us on this. We too want water mark on every PDF page.

Hi @Cropg1 what have you done so far?

Tried to insert image…but sounds from the forum its not that straight forward…new to erpnext and not from computer science background @johnskywalker

@Cropg1 I’ll message your for your email, I’ll invite you to our Slack channel. We’ll see how we can help you on this. Not really sure if I can push this code in the core now, since this is way back v7.

@johnskywalker Thanks a lot bro !!

Hi, so have you received the success with adding watermark to pint form?

hi @johnskywalker sorry for delayed reply. We did not get success in doing so.

Can you apply invoice number or any dynamics fields on pdf? and flatten the output…