This project demonstrates how to add both image and text watermarks to every page of a PDF using the DOMPDF library in PHP. It allows you to add a semi-transparent image as a watermark and text vertically aligned on the left side of each page.
- Add a centered image watermark to every page.
- Add vertically aligned text watermark to the left side of each page.
- Control the opacity (transparency) for both image and text.
- Automatically adjusts watermark placement for multi-page PDFs.
Below is a screenshot of a generated PDF with the watermark applied:
- PHP 7.4 or higher.
- DOMPDF library installed via Composer.
- A valid PNG image for the watermark.
To set up and run this project, follow these steps:
-
Clone this repository:
git clone https://github.com/AromalShaji/dompdf-watermark.git
-
Navigate to the project folder:
cd dompdf-watermark
-
Install dependencies using Composer:
composer require dompdf/dompdf
-
Place your watermark image in the
public/uploads/
folder. -
Update the image path in the script:
public_path('uploads/X.png');
$canvas = $dompdf->getCanvas();
// Convert the image to Base64 for embedding
$imageBase64 = base64_encode(file_get_contents(public_path('uploads/X.png')));
$imageMimeType = 'image/png';
$imageWidth = 500;
$imageHeight = 500;
$watermarkText = "GENERATED BY DOMPDF WATERMARK SYSTEM";
// Font metrics are used for text positioning
$fontMetrics = $dompdf->getFontMetrics();
$canvas->page_script(function ($pageNumber, $pageCount, $canvas) use ($fontMetrics, $imageBase64, $imageMimeType, $imageWidth, $imageHeight, $watermarkText) {
// Get the width and height of the page
$pageWidth = $canvas->get_width();
$pageHeight = $canvas->get_height();
// Calculate the position of the image to center it on the page
$imageX = ($pageWidth - $imageWidth) / 2;
$imageY = ($pageHeight - $imageHeight) / 2;
// Set opacity for the image (semi-transparent)
$canvas->set_opacity(0.2);
// Add the image watermark to the page
$canvas->image('data:' . $imageMimeType . ';base64,' . $imageBase64, $imageX, $imageY, $imageWidth, $imageHeight, 'png');
// Set opacity back to 1 (fully opaque) for the text watermark
$canvas->set_opacity(1);
// Set the font for the text watermark
$font = $fontMetrics->getFont("Times-Roman");
$fontSize = 9;
// Calculate the width and height for the text watermark
$textWidth = $fontMetrics->getTextWidth($watermarkText, $font, $fontSize);
// Set the position for the text watermark (left side of the page, vertically aligned)
$textX = 20;
$textY = $pageHeight - 200;
// Add the text watermark to the page with vertical rotation
$canvas->page_text($textX, $textY, $watermarkText, $font, $fontSize, [0.5, 0.5, 0.5], 0.0, 3, -90);
});
---
### Explanation of Key Points:
- **Installation Instructions**:
- **Clone the repository**: `git clone https://github.com/your-username/dompdf-watermark-system.git`
- **Navigate to the project folder**: `cd dompdf-watermark-system`
- **Install Composer dependencies**: `composer require dompdf/dompdf`
- **Upload a watermark image**: Place the watermark image (`X.png`) inside the `public/uploads/` directory.
- **Code Explanation**:
- The script uses `DOMPDF` to generate a PDF with watermarks.
- The watermark image is base64 encoded and added to the center of each page with a low opacity.
- The text watermark is positioned on the left side of each page, rotated, and with added spacing between the characters.
Make sure you have Composer installed to manage the PHP dependencies.