
Add Watermark using PHP to Existing or New PDF
FPDF is a fantastic library for working with PDF documents in PHP. It is the most popular one with a large number of plugins that enhances its core features.
Adding a watermark to a PDF document is a basic need in document editing work. Imagine that you have a project where you need to create a feature-rich PDF document editor project with the sole purpose of managing watermarks.
This PHP script will help you as a foundation code to solve your basic watermarking needs. You can use this as a base and build on top of it if you require more.
I have covered two aspects of adding watermarks.
- Add a watermark to an existing PDF document.
- Add a watermark to a new PDF document.
I am also presenting an online demo that watermarks a PDF document using this PHP script.
If you are looking for adding a watermark to a web page with a generated image using PHP, refer to this linked article.
View demo
Add a watermark to an existing PDF document
To add a watermark to an existing PDF document, I am using FPDF and FPDI libraries. FPDF is the foundation and FPDI is used to load and edit an existing document.
FPDI helps to load an existing PDF document and use it as a template in FPDF to create a document.
You need to download both FPDF and FPDI libraries and add it to the project. Refer to the project file structure image below.
I have earlier written with an example to convert HTML to PDF in JavaScript. Check it out and may help you.
existing-pdf-watermark.php
<?php
require_once __DIR__ . '/fpdf/fpdf.php';
require_once __DIR__ . '/FPDI/src/autoload.php';
function addWatermark($x, $y, $watermarkText, $angle, $pdf)
{
$angle = $angle * M_PI / 180;
$c = cos($angle);
$s = sin($angle);
$cx = $x * 1;
$cy = (300 - $y) * 1;
$pdf->_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));
$pdf->Text($x, $y, $watermarkText);
$pdf->_out('Q');
}
$pdf = new setasignFpdiFpdi();
$fileInput="example.pdf";
$pages_count = $pdf->setSourceFile($fileInput);
for ($i = 1; $i <= $pages_count; $i ++) {
$pdf->AddPage();
$tplIdx = $pdf->importPage($i);
$pdf->useTemplate($tplIdx, 0, 0);
$pdf->SetFont('Times', 'B', 70);
$pdf->SetTextColor(192, 192, 192);
$watermarkText="CONFIDENTIAL";
addWatermark(105, 220, $watermarkText, 45, $pdf);
$pdf->SetXY(25, 25);
}
$pdf->Output();
?>
Add a watermark to a new PDF document
Following PHP script is as simple as it gets. The Header function is used to render the PDF document page header. This is called by the AddPage function.
addWatermark is the key function responsible for creating the watermark. It sets the watermark text and performs the rotation to position it across the document. The X and Y location of where to start the watermark text and its color is defined in the Header function.
You can make adjustments by setting a smaller font, position, color, etc as per your choice.
new-pdf-watermark.php
<?php
require __DIR__ . '/fpdf/fpdf.php';
class PDF extends FPDF
{
function Header()
{
// setting the font, color and text for watermark
$this->SetFont('Times', 'B', 48);
$this->SetTextColor(140, 180, 205);
$watermarkText="New PDF Watermark - PHP";
$this->addWatermark(35, 190, $watermarkText, 45);
}
function addWatermark($x, $y, $watermarkText, $angle)
{
$angle = $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));
$this->Text($x, $y, $watermarkText);
$this->_out('Q');
}
}
$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);
$pdfDocumentContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. .nn";
for ($i = 0; $i < 15; $i ++) {
$pdf->MultiCell(0, 5, $pdfDocumentContent, 0, 'J');
}
$pdf->Output();
?>
PHP project structure
I have given the complete PHP project as a free download below. The dependent libraries FPDF and FPDI are not available in the project download zip file. You can download them from their official website and add them to your project as per the given project structure above before adding watermarks.
View demo Download
↑ Back to Top
Credit: Source link