Try ERPNext Buy Support Partners Foundation

[howto] Add watermark in generated pdfs

Steps to add a watermark to every page of a pdf report

def add_watermark(pdf_file, watermark_pdf=None):
    from frappe.email.email_body import get_filecontent_from_path
    from frappe.utils.pdf import get_file_data_from_writer

    if not watermark_pdf:
        # load watermark.pdf from /private/files
        watermark_pdf = "watermark_draft.pdf"
    path = get_files_path(watermark_pdf, is_private=True)
    watermark_pdf = PyPDF2.PdfFileReader(path)

    import io
    input_pdf = PyPDF2.PdfFileReader(io.BytesIO(pdf_file))

    writer = PyPDF2.PdfFileWriter()
    page_count = input_pdf.getNumPages()
    for page_number in range(page_count):
        input_page = input_pdf.getPage(page_number)
        input_page.mergePage(watermark_pdf.getPage(0))
        writer.addPage(input_page)

    filedata = get_file_data_from_writer(writer)

    return filedata
2 Likes