1. Go to this page and download the library: Download tomedio/pdf-watermark library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tomedio / pdf-watermark example snippets
use PdfWatermark\PdfWatermarkerFactory;
use PdfWatermark\Watermark\AbstractWatermark;
// Create a factory (optionally specify the path to pdftk and a custom temp directory)
$factory = new PdfWatermarkerFactory('pdftk', '/path/to/custom/temp/dir');
// Create and configure a text watermark config
$textConfig = $factory->createTextWatermarkConfig('CONFIDENTIAL');
$textConfig
->setPosition(AbstractWatermark::POSITION_CENTER)
->setAngle(45)
->setOpacity(0.5)
->setFontSize(72)
->setTextColor(255, 0, 0) // Red text
->setBackgroundColor(255, 255, 255) // White background
->setBackgroundOpacity(0.0) // Transparent background
->setPages('all'); // Apply to all pages
// Create a watermarker with the text watermark
$watermarker = $factory->createWithTextWatermark($textConfig);
// Apply the watermark
$watermarker->apply('input.pdf', 'output.pdf');
use PdfWatermark\PdfWatermarkerFactory;
use PdfWatermark\Watermark\AbstractWatermark;
// Create a factory
$factory = new PdfWatermarkerFactory();
// Create and configure an image watermark config
$imageConfig = $factory->createImageWatermarkConfig('logo.png');
$imageConfig
->setPosition(AbstractWatermark::POSITION_BOTTOM_RIGHT)
->setOpacity(0.3)
->setAngle(0)
->setScale(0.2) // Scale to 20% of original size
->setPages('all');
// Create a watermarker with the image watermark
$watermarker = $factory->createWithImageWatermark($imageConfig);
// Apply the watermark
$watermarker->apply('input.pdf', 'output.pdf');
use PdfWatermark\PdfWatermarkerFactory;
use PdfWatermark\Watermark\AbstractWatermark;
// Create a factory
$factory = new PdfWatermarkerFactory();
// Create a watermarker
$watermarker = $factory->create();
// Create and configure a text watermark
$textConfig = $factory->createTextWatermarkConfig('CONFIDENTIAL');
$textConfig
->setPosition(AbstractWatermark::POSITION_CENTER)
->setAngle(45)
->setOpacity(0.5)
->setFontSize(72)
->setTextColor(255, 0, 0);
$textWatermark = $factory->createTextWatermark($textConfig);
$watermarker->addWatermark($textWatermark);
// Create and configure an image watermark
$imageConfig = $factory->createImageWatermarkConfig('logo.png');
$imageConfig
->setPosition(AbstractWatermark::POSITION_BOTTOM_RIGHT)
->setOpacity(0.3)
->setScale(0.2);
$imageWatermark = $factory->createImageWatermark($imageConfig);
$watermarker->addWatermark($imageWatermark);
// Apply the watermarks
$watermarker->apply('input.pdf', 'output.pdf');
// Apply to all pages (default)
$config->setPages('all');
// Apply to a specific page
$config->setPages(1); // First page only
// Apply to a range of pages
$config->setPages('2-5'); // Pages 2 to 5
// Apply to a range of pages to the end
$config->setPages('2-last'); // From page 2 to the last page
// Apply to the last page only
$config->setPages('last');
// Apply to multiple specific pages or ranges using a comma-separated string
$config->setPages('1-2,4'); // Pages 1, 2, and 4
// Apply to a combination of specific pages, ranges, and special values
$config->setPages('1,3-5,last'); // Pages 1, 3, 4, 5, and the last page
// Apply to multiple specific pages or ranges using an array
$config->setPages([1, '3-5', 'last']);
// Specify a custom temp directory in the factory
$factory = new PdfWatermarkerFactory('pdftk', '/path/to/custom/temp/dir');
// Or directly in the PdfWatermarker constructor
$watermarker = new PdfWatermarker('pdftk', '/path/to/custom/temp/dir');
// Create a text watermark with page number
$textConfig = $factory->createTextWatermarkConfig('Page %d');
$textConfig
->setPosition(AbstractWatermark::POSITION_BOTTOM_CENTER)
->setOpacity(0.5)
->setFontSize(10)
->setTextColor(0, 0, 0);
$watermarker = $factory->createWithTextWatermark($textConfig);
$watermarker->apply('input.pdf', 'output.pdf');
use PdfWatermark\Watermark\Config\TextWatermarkConfig;
$textConfig = $factory->createTextWatermarkConfig('CONFIDENTIAL');
// Using predefined font constants
$textConfig->setFontName(TextWatermarkConfig::FONT_HELVETICA);
$textConfig->setFontName(TextWatermarkConfig::FONT_COURIER);
$textConfig->setFontName(TextWatermarkConfig::FONT_TIMES);
$textConfig->setFontName(TextWatermarkConfig::FONT_SYMBOL);
// Or using a string directly
$textConfig->setFontName('Helvetica');
// You can also use custom fonts if they are available in your TCPDF installation
$textConfig->setFontName('YourCustomFont');
$textConfig
->setFontName(TextWatermarkConfig::FONT_HELVETICA)
->setFontStyle(TextWatermarkConfig::FONT_STYLE_BOLD) // Use constants for font styles
->setFontSize(24);
// Apply both bold and italic styles
$textConfig->setFontStyle(
TextWatermarkConfig::FONT_STYLE_BOLD | TextWatermarkConfig::FONT_STYLE_ITALIC
);
// Apply bold, underline, and strikethrough
$textConfig->setFontStyle(
TextWatermarkConfig::FONT_STYLE_BOLD |
TextWatermarkConfig::FONT_STYLE_UNDERLINE |
TextWatermarkConfig::FONT_STYLE_STRIKETHROUGH
);
$imageConfig = $factory->createImageWatermarkConfig('logo.png');
$imageConfig
->setPosition(AbstractWatermark::POSITION_BOTTOM_RIGHT)
->setWidth(200); // Set width to 200 points, height will be calculated automatically
// Or set height instead
$imageConfig
->setHeight(100); // Set height to 100 points, width will be calculated automatically
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.