AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
Home PHP and MySQL

Create PDF with Watermark in PHP using Dompdf

Create PDF with Watermark in PHP using Dompdf

Lakshika Mathur by Lakshika Mathur
December 27, 2019
Reading Time: 4 mins read
0
Create PDF with Watermark in PHP using Dompdf

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.

RELATED POSTS

What is Application Programming Interface (APIs)?

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

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

  1. 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.

  1. 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

 

Tags: DompdfWatermark
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

What is Application Programming Interface (APIs), Types, and Importance.
PHP and MySQL

What is Application Programming Interface (APIs)?

January 29, 2022
93
Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
784
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
167
How to Force Download File in PHP
PHP and MySQL

How to Force Download File in PHP

January 2, 2020
87
How to Connect to the Remote MySQL Database using PHP
PHP and MySQL

How to Connect to the Remote MySQL Database using PHP

January 1, 2020
29
How to Generate QR Code with PHP using Google Chart API
PHP and MySQL

How to Generate QR Code with PHP using Google Chart API

January 1, 2020
90
Next Post
Import and Export CSV File using PHP and MySQL

Import and Export CSV File using PHP and MySQL

Highlight Keyword in Search Results with PHP and MySQL     

Highlight Keyword in Search Results with PHP and MySQL     

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (1)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Some best free WordPress Themes for Affiliate Marketing Websites
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team

Go to mobile version