When you wish to allow a user to download text or HTML information from a web application, dynamic PDF production comes in handy. The HTML material must be converted to a PDF file before being downloaded in this situation. Dompdf makes it simple to convert HTML to PDF in PHP. Dompdf is the simplest way to use PHP to build a PDF document with dynamic data. The Dompdf package allows you to use PHP to create a PDF file and add HTML information to it.
Dompdf offers a number of configuration options to help you get the most out of your PDF generation. One of the most useful features is the ability to add a watermark to a PDF document. In this article, we’ll teach you how to use Dompdf and PHP to convert HTML to PDF and apply a watermark to it.
A watermark is an image or text that appears in front of or behind the PDF document’s content. In the sample code, we’ll show how to use Dompdf in PHP to add a text and picture watermark to a PDF document.
Dompdf can be downloaded and installed.
1. Download a stable release of dompdf from GitHub’s archive. Extract the Dompdf package and place it in your application’s directory.
It’s worth noting that you don’t need to download the Dompdf library individually; the source code contains all of the necessary files.
2. Include autoloader in the PHP script to load dompdf libraries and helper functions.
// Include autoloader
require_once 'dompdf/autoload.inc.php';
Watermarking a PDF (text)
Using the Dompdf library and PHP, the following example code generates a PDF file and adds a watermark text to it.
Use the namespace references for Dompdf, Options, and FontMetrics.
To enable embedded PHP, set the isPhpEnabled option to true.
Create a new instance of the dompdf class.
Add HTML content to the page.
Create a PDF from the HTML.
Using the getCanvas() function of the Dompdf class, create a canvas object.
Create a class for font metrics.
Get the page height and width using the Canvas class’s get width() and get height() methods.
Get the family file using the FontMetrics class’s getFont() function and set the text font.
Choose the text to which you’d want to apply a watermark.
Get the height and width of text using the FontMetrics class’s getFontHeight() and getTextWidth() methods.
Set the text’s horizontal and vertical positions.
Using the text() method of the Canvas class, add a watermark to a PDF document.
Using the Dompdf class’s stream() function, output the created 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));
Watermarking a PDF (image)
Using the Dompdf library and PHP, the following example code generates a PDF file and adds a watermark image to it. Except for the picture setup, the approach is the same as the text watermark code (above).
Choose the image path to which you’d like to apply a watermark.
Using the image() function of the Canvas class, add a watermark image to a 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));
Configuration of the Watermark Text
In the PDF, you can alter the watermark text’s position, font size, colour, spacing, and rotation (angle). View the text() function of the Canvas class’s available options.
/**
* 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);a
Configuration of the Watermark Image
The position, width, height, and resolution of the watermark image in the PDF can all be changed. View the image() function of the Canvas class’s available choices.
/**
* 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");