php-tcpdfHow do I add a watermark to PDFs using PHP and TCPDF?
Using PHP and TCPDF, you can add a watermark to PDFs in the following way:
- Create a new PDF document using the
TCPDF()
constructor. - Use the
SetAlpha()
method to set the transparency of the watermark image. - Use the
Image()
method to add the watermark image to the PDF document. - Use the
SetXY()
method to set the coordinates of the watermark image. - Use the
SetFont()
method to set the font size and type of the watermark text. - Use the
SetTextColor()
method to set the color of the watermark text. - Use the
Cell()
method to add the watermark text to the PDF document.
Example code
<?php
// Include the TCPDF library
require_once('tcpdf.php');
// Create a new PDF document
$pdf = new TCPDF();
// Set the transparency of the watermark image
$pdf->SetAlpha(0.2);
// Add the watermark image to the PDF document
$pdf->Image('watermark.png', 10, 10, 100, 100);
// Set the font size and type of the watermark text
$pdf->SetFont('helvetica', '', 20);
// Set the color of the watermark text
$pdf->SetTextColor(255, 0, 0);
// Add the watermark text to the PDF document
$pdf->Cell(0, 0, 'Watermark Text', 0, 0, 'C');
// Output the PDF document
$pdf->Output('watermarked.pdf', 'I');
?>
Helpful links
More of Php Tcpdf
- How do I install TCPDF using Yum on a PHP server?
- How do I use the PHP TCPDF WriteHTMLCell function?
- How do I use the setfont function in PHP TCPDF?
- How can I generate a QR code using PHP and TCPDF?
- How do I install TCPDF with Composer in a PHP project?
- How can I use TCPDF's writeHTML() function in PHP?
- How do I use the setxy function in PHP TCPDF?
- How do I set the page orientation when using PHP TCPDF?
- How do I set the margins using PHP TCPDF?
See more codes...