Create PDF with Watermark in PHP using Dompdf

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.

Download and Install Dompdf

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';

Add Watermark to PDF (text)

The following example code generates PDF and add watermark text to PDF file with Dompdf library using PHP.

  • Use the reference of the Dompdf, Options, and FontMetrics namespace.
  • Set the isPhpEnabled option to enable embedded PHP.
  • Instantiate dompdf class.
  • Add HTML content.
  • Render the HTML as PDF.
  • Instantiate canvas instance using getCanvas() method of Dompdf class.
  • Instantiate font metrics class.
  • Get the page height and width using get_width() and get_height() of the Canvas class.
  • Get the family file using getFont() method of FontMetrics class and specify the text font.
  • Specify the text want to add as a watermark.
  • Get the height and width of text using getFontHeight() and getTextWidth() of FontMetrics class.
  • Specify the horizontal and vertical positions of the text.
  • Write watermark text to PDF document using the text() method of Canvas class.
  • Output the generated PDF using the stream() function of Dompdf class.
// 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($font75);
$textWidth $fontMetrics->getTextWidth($text$font75);

// 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$font75);

// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream('document.pdf', array("Attachment" => 0));

Add Watermark to PDF (image)

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.

  • Specify the image path (images/codexworld-logo.png) to which you want to add as a watermark.
  • Add watermark image to PDF document using image() method of Canvas class.
// 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));

Watermark Text Configuration

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(000), $word_space 0.0$char_space 0.0$angle 0.0);

Watermark Image Configuration

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

Conclusion

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

2 Comments

  1. Stanley Onchari Said...
  2. Naveen Said...

Leave a reply

keyboard_double_arrow_up