Dynamic PDF generation is convenient when you have to permit the user to download the HTML content in a file on the web application. Therefore, we need to convert the HTML content to the PDF file before you download it. So, you can easily convert HTML to PDF in PHP with the help of Dompdf. It is the easiest way to make a PDF document with dynamic data using PHP. Dompdf library is constructive, and it will help to create a PDF file and add the HTML content to PDF using PHP.
Dompdf offers a lot of configuration options to improve PDF creation functionality. Adding Watermark to PDF is one of the most beneficial features among them. With the help of this tutorial, we will explain to you how to add watermark to PDF and convert HTML to PDF with Dompdf using PHP.
So, before proceeding, first, understand what a watermark is?
Watermark is an image or text that appears either front or behind the content of the PDF document. In the example code, we will explain both methods to add image and text watermark to PDF with Dompdf in PHP.
Download and Install Dompdf
- Download an archive version of dompdf (stable release) from GitHub. Extract the Dompdf package and place it in the directory of your application.
Note: There is no need to download the Dompdf library separately; all the files that you need are included in the source code.
- Include autoloader to load dompdf libraries and helper functions in the PHP script.
// Include autoloader require_once 'dompdf/autoload.inc.php';
Add Watermark to PDF (text)
The following example code creates a PDF and adds watermark text to PDF file with Dompdf library using PHP.
- Use the reference of the Dompdf, Options, and FontMetrics namespace.
- Set the PhpEnabled option to permit embedded PHP.
- Instantiate dompdf class.
- Now, add HTML content.
- Extract the HTML as PDF.
- Instantiate canvas instance using getCanvas() method of Dompdf class.
- Instantiate font metrics class.
- Use get_width() and get_height() of Canvas class to get the page height and width.
- Use getFont() method of FontMetrics class and specify text font to get family files.
- Get height and width of text using getFontHeight() and getTextWidth() of FontMetrics class.
- Specify the vertical and horizontal position of the text.
- Use text() method of Canvas class and write watermark text to PDF document.
- Use stream() function of Dompdf class to generate the PDF.
// 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', 'landscape'); // 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));
SEE ALSO: Convert HTML to MS Word Document using PHP
Add Watermark to PDF (image)
The following example code will make a PDF and adds a watermark image to PDF file with Dompdf library with the help of PHP. The process is the same as the text watermark code (above) except the image setup.
- Specify the image path in which you want to add a watermark.
- Use the image() method of Canvas class to add a watermark image to PDF document.
// 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', 'landscape'); // 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));
SEE ALSO: Add WYSIWYG HTML Editor to Textarea with CKEditor
Watermark Text Configuration
You can change the position (font size, colour, spacing, and rotation) of the watermark text in the PDF.
/** * 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);
Watermark Image Configuration
You can change the position (height, width, and resolution) of the watermark image in the PDF.
/** * 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");
Also, read our previous blog- Convert HTML to PDF using JavaScrip