How to add your own watermark on PDF by using TCPDF

In this blog we will add a watermark on a PDF using TCPDF. Adding a watermark to a PDF document is a basic need in document editing work. TCPDF is a fantastic library for working with PDF documents in PHP.

Generate Watermark on PDF

Here you can follow these steps to generate watermark on PDF:

Step 1: Create one project folder like “generate_pdf

Step 2: Download TCPDF library

Include above downloaded library inside your project folder

Folder structure TCPDF library

Step 3: Create one PHP file like watermark_pdf_maker.php

First we need to include TCPDF library as below


Once you include the TCPDF library you required to override header() method with below code

class MyCustomPDFWithWatermark extends TCPDF {
	public function Header() {
		// Get the current page break margin
		$bMargin = $this->getBreakMargin();

		// Get current auto-page-break mode
		$auto_page_break = $this->AutoPageBreak;

		// Disable auto-page-break
		$this->SetAutoPageBreak(false, 0);

		// Define the path to the image that you want to use as watermark.
		$img_file = '';
		//$img_file = './watermark_logo.jpg'; //when you run this code locally

		// Render the image
		//$this->Image( filename, left, top, width, height, type, link, align, resize, dpi, align, ismask, imgmask, border, fitbox, hidden, fitonpage);
		$this->Image($img_file, 0, 10, 0, 0, '', '', '', false, 300, 'C', false, false, 0);

		// Restore the auto-page-break status
		$this->SetAutoPageBreak($auto_page_break, $bMargin);

		// Set the starting point for the page content

In above code you must have to define path to the image that you want to use as watermark. Here we have used below image as a watermark.

You can set all the settings of image with below parameters

//$this->Image( filename, left, top, width, height, type, link, align, resize, dpi, align, ismask, imgmask, border, fitbox, hidden, fitonpage);

Step 4: Set up the PDF document properties, such as the author, font size and orientation:

// Create a new PDF but using our own class that extends TCPDF
$pdf = new MyCustomPDFWithWatermark(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
// set default monospaced font
// set default header data
// set margins
$pdf->SetMargins('10', '40', '10');
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetFont('dejavusans', '', 10);

// Add a page

Step 5: Write HTML code for PDF

$content = ''; 

$content .= '
<style type="text/css">
font-family:"Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
<table cellpadding="0" cellspacing="0" style="border:1px solid #ddc;width:100%;">
<table style="width:100%;" >
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td colspan="2" align="center"><b>SHINERWEB TECHNOLOGIES</b></td></tr>
<tr><td colspan="2" align="center"><b>CONTACT: +91 97979  97979</b></td></tr>
<tr><td colspan="2" align="center"><b>WEBSITE: WWW.SHINERWEB.COM</b></td></tr>
<tr><td colspan="2"><b>CUST.NAME: Divyesh Patel </b></td></tr>
<tr><td><b>MOB.NO: 98254545458 </b></td><td align="right"><b>BILL DT.: '.date("d-m-Y").'</b> </td></tr>
<tr><td>&nbsp;</td><td align="right"><b>BILL NO.: INV-101</b></td></tr>
<tr><td colspan="2" align="center"><b>INVOICE</b></td></tr>
<tr class="heading" style="background:#eee;border-bottom:1px solid #ddd;font-weight:bold;">
	<td align="right">
<tr class="itemrows">
	<td><b>Domain</b><br><i>Write any remarks</i></td>
	<td align="right"><b>1500</b></td>
<tr class="itemrows">
	<td><b>Web Hosting</b><br><i>Write any remarks</i></td>
	<td align="right"><b>1000</b></td>
<tr class="itemrows">
	<td><b>SEO services</b><br><i>Write any remarks</i></td>
	<td align="right"><b>2500</b></td>
<tr class="total"><td colspan="2" align="right">------------------------</td></tr>
<tr><td colspan="2" align="right"><b>GRAND&nbsp;TOTAL:&nbsp;5000</b></td></tr>
<tr><td colspan="2" align="right">------------------------</td></tr>
<tr><td colspan="2" align="right"><b>PAYMENT MODE: CASH/ONLINE </b></td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td colspan="2" align="center"><b>THANK YOU ! VISIT AGAIN</b></td></tr>
<tr><td colspan="2">&nbsp;</td></tr>

Here we design invoice copy with HTML code and that all the code stored in $content variable.

Step 6: Generate PDF

$file_name = "INV_001.pdf";
$pdf->Output($file_name, 'I'); // I means Inline view
//$pdf->Output($file_name, 'D'); // D means download

In above code we have used writeHTML() and Output() method to generate PDF.

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply