Safeguarding or branding your digital documents is an important task in today’s world. Whether marking an invoice as “Paid,” a legal document as “Confidential,” or simply adding in your company’s logo for branding, watermarks present an effective solution. This tutorial will walk you through, step-by-step and clearly, adding a watermark to a PDF using PHP and enable you to automate this process on newly created and pre-existing PDFs. We will explore how to dynamically apply text or image watermarks, giving you complete control over your document’s presentation and security.
Getting Started with FPDF and FPDI
To accomplish watermarking to PDF file, we will use two powerful and popular open-source PHP libraries: FPDF and FPDI. Think of them as our digital toolkit for manipulating PDFs.
FPDF: This is an excellent library for generating PDF files from scratch. It can write text, draw shapes, and embed images using pure PHP. We will use it as a base for generating our new watermarked documents.
FPDI: This library works as an add-on for FPDF. The special ability of this is to import pages from existing PDF documents. This is the key that lets us read an existing PDF and stamp a watermark on top of its pages.
Before we move to practical example, you need to install these packages into your project. The easiest way to do this is with Composer, a dependency manager for PHP. Run the following command in your project’s terminal:
composer require setasign/fpdf setasign/fpdiHow to Watermark an Existing PDF in PHP
For this example, we will example scenario where you have a folder of generated reports or invoices and you need to mark all of them with a “DRAFT” watermark. Doing this manually would be incredibly time-consuming. Instead, we can write a PHP script to do it for us.
Here is the code to achieve this:
<?php
require_once('vendor/autoload.php');
use setasign\Fpdi\Fpdi;
class PDF extends Fpdi
{
public $_tplIdx;
public $angle = 0;
public function Header()
{
if (is_null($this->_tplIdx)) {
return;
}
$this->useTemplate($this->_tplIdx, 0, 0, 210);
$this->SetFont('Arial', 'B', 50);
$this->SetTextColor(255, 192, 203);
$this->SetXY(55, 150);
$this->Rotate(45);
$this->Write(0, 'CONFIDENTIAL');
$this->Rotate(0);
}
function Rotate($angle, $x=-1, $y=-1)
{
if ($x == -1) {
$x = $this->x;
}
if ($y == -1) {
$y = $this->y;
}
if ($this->angle != 0) {
$this->_out('Q');
}
$this->angle = $angle;
if ($angle != 0) {
$angle *= M_PI/180;
$c = cos($angle);
$s = sin($angle);
$cx = $x*$this->k;
$cy = ($this->h-$y)*$this->k;
$this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
}
}
function _endpage()
{
if ($this->angle != 0) {
$this->angle = 0;
$this->_out('Q');
}
parent::_endpage();
}
}
$sourcePdfFile = 'original-document.pdf';
$outputPdfFile = 'watermarked-document.pdf';
$pdf = new PDF();
$pageCount = $pdf->setSourceFile($sourcePdfFile);
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$pdf->_tplIdx = $pdf->importPage($pageNo);
$pdf->AddPage();
}
$pdf->Output($outputPdfFile, 'F');In above script, we create a custom PDF class that extends FPDI to handle the watermark rotation and placement. For each page, it first imports the original page using importPage(). Then, the Header() method automatically kicks in to place our rotated “CONFIDENTIAL” watermark on top.
Add Watermark to a New PDF Document
Let’s take another example where you want to add watermark with creating PDF file. It is ever more straightforward because we don’t need to import any existing files. We simply use FPDF to create a page, draw our watermark, and then write the main content over it.
<?php
require_once('vendor/autoload.php');
use Fpdf\Fpdf;
class WatermarkPDF extends Fpdf
{
protected $angle = 0;
function Watermark($text, $angle, $x, $y)
{
$this->SetFont('Arial', 'B', 50);
$this->SetTextColor(240, 240, 240);
$this->Rotate($angle, $x, $y);
$this->Text($x, $y, $text);
$this->Rotate(0);
}
function Rotate($angle, $x=-1, $y=-1)
{
if ($x == -1) {
$x = $this->x;
}
if ($y == -1) {
$y = $this->y;
}
if ($this->angle != 0) {
$this->_out('Q');
}
$this->angle = $angle;
if ($angle != 0) {
$angle *= M_PI/180;
$c = cos($angle);
$s = sin($angle);
$cx = $x*$this->k;
$cy = ($this->h-$y)*$this->k;
$this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
}
}
function _endpage()
{
if ($this->angle != 0) {
$this->angle = 0;
$this->_out('Q');
}
parent::_endpage();
}
}
$pdf = new WatermarkPDF();
$pdf->AddPage();
$pdf->Watermark('DRAFT DOCUMENT', 45, 45, 160);
$pdf->SetFont('Arial', '', 12);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY(20, 30);
$pdf->Write(5, 'This is the official project proposal report.');
$pdf->Ln(10);
$pdf->Write(5, 'It outlines the project scope, timeline, and budget.');
$pdf->Ln(10);
$pdf->Write(5, 'Please review the contents and provide your feedback.');
$pdf->Output('new-watermarked-pdf.pdf', 'F');This method first stamps your chosen watermark onto a blank page. It then writes your main content directly over it, creating a professional document where the watermark sits cleanly in the background.
Conclusion
You now have what it takes to add a watermark to a PDF programmatically in PHP. Combine the power of FPDF for creation and FPDI for importation, and you will be able to handle any given task of watermarking. You can checkout our post on Convert HTML to PDF in PHP Using Dompdf.

