Dynamic PDF generation is useful when you want to allow the user to download the text or HTML content in a file on the web application. In this case, the HTML content needs to be converted to a PDF file before downloading. You can easily convert HTML to PDF in PHP with Dompdf. Dompdf is the easiest way to create PDF documents with dynamic data using PHP. Dompdf library helps to create a PDF file and add the HTML content to PDF using PHP.
Dompdf provides various configuration options to enhance the PDF creation functionality and customize the PDF document. Add Watermark to PDF is a very useful feature to protect the copyright of the PDF document. In this tutorial, we will show you how to convert HTML to PDF and add watermark to PDF with Dompdf using PHP.
A watermark is an image or text that can appear either front or behind the content of the PDF document. In the example code, we will show both methods to add text and image watermark to PDF document with Dompdf in PHP.
1. First, download stable release of dompdf library, extract the Dompdf package and place it in the directory of your application.
Note that: You don’t need to download the Dompdf library separately, all the required files are included in the source code.
2. Include autoloader to load dompdf library and helper functions in the PHP script.
// Include autoloader
require_once 'dompdf/autoload.inc.php';
The following example code generates PDF and add watermark text to PDF file with Dompdf library using PHP.
isPhpEnabled
option to enable embedded PHP.// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Reference the Options namespace
use Dompdf\Options;
// Reference the Font Metrics namespace
use Dompdf\FontMetrics;
// Set options to enable embedded PHP
$options = new Options();
$options->set('isPhpEnabled', 'true');
// Instantiate dompdf class
$dompdf = new Dompdf($options);
// Load HTML content
$dompdf->loadHtml('<h1>Welcome to CodexWorld.com</h1>');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Instantiate canvas instance
$canvas = $dompdf->getCanvas();
// Instantiate font metrics class
$fontMetrics = new FontMetrics($canvas, $options);
// Get height and width of page
$w = $canvas->get_width();
$h = $canvas->get_height();
// Get font family file
$font = $fontMetrics->getFont('times');
// Specify watermark text
$text = "CONFIDENTIAL";
// Get height and width of text
$txtHeight = $fontMetrics->getFontHeight($font, 75);
$textWidth = $fontMetrics->getTextWidth($text, $font, 75);
// Set text opacity
$canvas->set_opacity(.2);
// Specify horizontal and vertical position
$x = (($w-$textWidth)/2);
$y = (($h-$txtHeight)/2);
// Writes text at the specified x and y coordinates
$canvas->text($x, $y, $text, $font, 75);
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream('document.pdf', array("Attachment" => 0));
The following example code generates PDF and add watermark image to PDF file with Dompdf library using PHP. The process is the same as the text watermark code (above) except for the image setup.
images/codexworld-logo.png
) to which you want to add as a watermark.// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Reference the Options namespace
use Dompdf\Options;
// Set options to enable embedded PHP
$options = new Options();
$options->set('isPhpEnabled', 'true');
// Instantiate dompdf class
$dompdf = new Dompdf($options);
// Load HTML content
$dompdf->loadHtml('<h1>Welcome to CodexWorld.com</h1>');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Instantiate canvas instance
$canvas = $dompdf->getCanvas();
// Get height and width of page
$w = $canvas->get_width();
$h = $canvas->get_height();
// Specify watermark image
$imageURL = 'images/codexworld-logo.png';
$imgWidth = 200;
$imgHeight = 20;
// Set image opacity
$canvas->set_opacity(.5);
// Specify horizontal and vertical position
$x = (($w-$imgWidth)/2);
$y = (($h-$imgHeight)/2);
// Add an image to the pdf
$canvas->image($imageURL, $x, $y, $imgWidth, $imgHeight);
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream('document.pdf', array("Attachment" => 0));
You can change the position, font size, color, spacing, and rotation (angle) of the watermark text in the PDF. See the available options for the text()
function of Canvas class.
/**
* Writes text at the specified x and y coordinates.
*
* @param float $x
* @param float $y
* @param string $text the text to write
* @param string $font the font file to use
* @param float $size the font size, in points
* @param array $color
* @param float $word_space word spacing adjustment
* @param float $char_space char spacing adjustment
* @param float $angle angle
*/
text($x, $y, $text, $font, $size, $color = array(0, 0, 0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0);
You can change the position, width, height, and resolution of the watermark image in the PDF. See the available options for the image()
function of Canvas class.
/**
* Add an image to the pdf.
*
* The image is placed at the specified x and y coordinates with the
* given width and height.
*
* @param string $img_url the path to the image
* @param float $x x position
* @param float $y y position
* @param int $w width (in pixels)
* @param int $h height (in pixels)
* @param string $resolution The resolution of the image
*/
image($img_url, $x, $y, $w, $h, $resolution = "normal");
Add Watermark to Existing PDF using PHP
This example code can be used to create PDF documents from HTML content dynamically with PHP in the web application. You can customize the generated PDF and add watermark to PDF with Dompdf using PHP. Download PDF feature can be provided to export HTML to PDF and download as a PDF document on the website.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Thank you so much for this tutorial.
I need to print text water mark for all page. is working fine, but opacity working only on first and last page. so how to set opacity for all page