

- #WATERMARK MAKER WITH TWO PDF HOW TO#
- #WATERMARK MAKER WITH TWO PDF PDF#
- #WATERMARK MAKER WITH TWO PDF PORTABLE#
#WATERMARK MAKER WITH TWO PDF PDF#
input_file: The path of the PDF file to watermark.This function aims to merge the inputted PDF file with the generated watermark. # If required to watermark specific pages not all the document pages Pdf_reader = PdfFileReader(open(input_file, 'rb'), strict=False)įor page in range(pdf_reader.getNumPages()): Result, wm_buffer = create_watermark(wm_text) Now let's write the function that's responsible for adding a watermark to a given PDF file: def watermark_pdf(input_file: str, wm_text: str, pages: Tuple = None): Save_watermark() saves the generated watermark template to a physical file in case you need to visualize it. Saves the generated watermark template to disk def save_watermark(wm_buffer, output_file): Note that you can instead of using the drawString() method to write text, you can use drawImage() to draw an image, as written as a comment in the above function. Apply the parameters defined earlier on our created canvas using reportlab.

Creates a watermark file and stores it in memory.# Position according to the configured parameter # Rotate according to the configured parameter def create_watermark(wm_text: str):Ĭ = canvas.Canvas(output_buffer, pagesize=PAGESIZE) The above function returns a path for a temporary output file when no output file is specified, or in case the paths of the input and output files are equal. Tmp_file = os.path.join(input_path, 'tmp_' + input_filename) If not output_file or input_file = output_file: # If output file is equal to input_file -> generate a temporary output file # If output file is empty -> generate a temporary output file Input_filename = os.path.basename(input_file) def get_output_file(input_file: str, output_file: str):Ĭheck whether a temporary output file is needed or not It is worth noting that you cannot extract these attributes for an encrypted PDF file. The get_info() function collects the metadata of an input PDF file, the following attributes can be extracted: author, creator, producer, subject, title, and the number of pages. "File": input_file, "Encrypted": ("True" if pdf_reader.isEncrypted else "False") Pdf_reader = PdfFileReader(pdf_file, strict=False) # If PDF is encrypted the file metadata cannot be extracted Next, defining our first utility function: def get_info(input_file: str): # The rotation angle in order to display the watermark diagonally if needed # The position attributes of the watermark Starting with the code, let's import the libraries and define some configuration we'll need: from PyPDF4 import PdfFileReader, PdfFileWriterįrom PyPDF4.generic import TextStringObject, NameObject
#WATERMARK MAKER WITH TWO PDF HOW TO#
In this tutorial, you will learn how to watermark a PDF file or a folder containing a collection of PDF files using PyPDF4 and reportlab in Python.įirst, let's install the necessary libraries: $ pip install PyPDF4=1.27.0 reportlab=3.5.59 Because PDFs are more versatile than other file formats, the information they display is easily viewable from almost any operating system or device.

The familiarity of PDF led to its fast and widespread adoption as a solution in the field of digital archiving.
#WATERMARK MAKER WITH TWO PDF PORTABLE#
Portable Document Format (PDF), standardized as ISO 32000, is a file format developed by Adobe in 1993 to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.īased on the PostScript language, each PDF file encapsulates a complete description of a fixed-layout flat document, including the text, fonts, vector graphics, raster images, and other information needed to display it.
