<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PDF Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<atom:link href="https://atcodex.com/tag/pdf/feed/" rel="self" type="application/rss+xml" />
	<link>https://atcodex.com/tag/pdf/</link>
	<description>Achieve financial success without sacrificing well-being! AtCodex provides actionable strategies for managing money, time, and personal growth—all in one place.</description>
	<lastBuildDate>Sun, 19 Sep 2021 12:59:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://atcodex.com/wp-content/uploads/2020/05/cropped-New-Project-1-32x32.png</url>
	<title>PDF Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<link>https://atcodex.com/tag/pdf/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Add Watermark to Existing PDF using PHP</title>
		<link>https://atcodex.com/php/how-to-add-watermark-to-existing-pdf-using-php/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Sun, 19 Sep 2021 12:59:10 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PDF]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=1485</guid>

					<description><![CDATA[<p>PDF (Portable Document Format) is widely a popular file format which we used for variour purpose in online work. Generally, the PDF file is used to share text/image data for &#8230; </p>
<p>The post <a href="https://atcodex.com/php/how-to-add-watermark-to-existing-pdf-using-php/">How to Add Watermark to Existing PDF using PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">PDF (Portable Document Format) is widely a popular file format which we used for variour purpose in online work. </p>



<p class="wp-block-paragraph"> Generally, the PDF file is used to share text/image data for offline use. In the website we generate invoices, reports and other data for interenal use or to client against their used service on the website. In the web application, a PDF file is created with HTML content to make dynamic data available for download.</p>



<p class="wp-block-paragraph"> In this tutorial we will learn how we can easily create PDF documents dynamically and convert HTML content to PDF using PHP.</p>



<p class="wp-block-paragraph">Some time we need to print specific header and footer in the PDF like company name, date, watermark etc. for example when we get bill from some big comapny in PDF format then most of the time they watermark their PDF with their company name or logo. Same we will learn in today&#8217;s tutorial. Today we will learn how we can add watermark to existing PDF usign PHP.</p>



<p class="wp-block-paragraph">We can use  <a href="https://atcodex.com/laravel/convert-html-to-pdf-in-laravel-with-dompdf/" target="_blank" rel="noreferrer noopener">Dompdf  </a>to create the PDF from HTML and add the watter in that file but what if we already have PDF generated on our website or somewhere in the system, then if we want to add the watermark or some particular text to that existing PDF, that we can not do with  Dompdf . In this tutorial, we will learn how we can add a watermark to an existing PDF documents<strong> using <a href="https://www.php.net/" target="_blank" rel="noreferrer noopener">PHP</a></strong>.</p>



<p class="wp-block-paragraph">In this example script, we will use FPDF and FPDI libraries to add watermark images to PDF with PHP. You can add text or image as a watermark to an existing PDF documents using PHP.</p>



<h2 class="wp-block-heading">Install FPDF and FPDI Library</h2>



<p class="wp-block-paragraph">Run the following command to install FPDF and FPDI library using composer. If you don&#8217;t know how to use composer, please visit <a href="https://getcomposer.org/" target="_blank" rel="noreferrer noopener">https://getcomposer.org/</a></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">composer require setasign/fpdi-fpdf
</pre>



<p class="wp-block-paragraph"><strong>Note that</strong>: You don’t need to download the FPDF-FPDI library separately, all the required files are included in the source code.</p>



<p class="wp-block-paragraph">Include autoloader to load FPDI library and helper functions in the PHP file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// Load Fpdi library 
use setasign\Fpdi\Fpdi; 
require_once('vendor/autoload.php');</pre>



<h2 class="wp-block-heading">Add Text Watermark to PDF</h2>



<p class="wp-block-paragraph">The following example code adds&nbsp;<strong>watermark text to the existing PDF</strong>&nbsp;file using PHP.</p>



<ul class="wp-block-list"><li>Specify source PDF file and watermark text.</li><li>Configure text font, weight, and height using PHP <code>imagefontheight()</code> and <code>imagefontwidth()</code> function.</li><li>Create blank image with <code>imagecreatetruecolor()</code> function.</li><li>Set background and font color with <code>imagecolorallocate()</code> function.</li><li>Create image with <code>imagepng()</code> function.</li><li>Initialize Fpdi class and set the source PDF file using <code>setSourceFile()</code> method.</li><li>Add watermark to PDF pages using <code>importPage()</code>, <code>getTemplateSize()</code>, <code>addPage()</code>, <code>useTemplate()</code>, and <code>Image()</code> methods of Fpdi class.</li><li>Render the generated PDF using the <code>Output()</code> function of Fpdi class.</li></ul>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php 

// Load Fpdi library 
use setasign\Fpdi\Fpdi; 
require_once('vendor/autoload.php');

// Source file and watermark config 
$file = 'documents/test.pdf'; 
$text = 'atcodex.com'; 
 
// Text font settings 
$name = uniqid(); 
$font_size = 5; 
$opacity = 100; 
$ts = explode("\n", $text); 
$width = 0; 
foreach($ts as $k=>$string){ 
    $width = max($width, strlen($string)); 
} 
$width  = imagefontwidth($font_size)*$width; 
$height = imagefontheight($font_size)*count($ts); 
$el = imagefontheight($font_size); 
$em = imagefontwidth($font_size); 
$img = imagecreatetruecolor($width, $height); 
 
// Background color 
$bg = imagecolorallocate($img, 255, 255, 255); 
imagefilledrectangle($img, 0, 0, $width, $height, $bg); 
 
// Font color settings 
$color = imagecolorallocate($img, 0, 0, 0); 
foreach($ts as $k=>$string){ 
    $len = strlen($string); 
    $ypos = 0; 
    for($i=0;$i&lt;$len;$i++){ 
        $xpos = $i * $em; 
        $ypos = $k * $el; 
        imagechar($img, $font_size, $xpos, $ypos, $string, $color); 
        $string = substr($string, 1);       
    } 
} 
imagecolortransparent($img, $bg); 
$blank = imagecreatetruecolor($width, $height); 
$tbg = imagecolorallocate($blank, 255, 255, 255); 
imagefilledrectangle($blank, 0, 0, $width, $height, $tbg); 
imagecolortransparent($blank, $tbg); 
$op = !empty($opacity)?$opacity:100; 
if ( ($op &lt; 0) OR ($op >100) ){ 
    $op = 100; 
} 
 
// Create watermark image 
imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $op); 
imagepng($blank, $name.".png"); 
 
// Set source PDF file 
$pdf = new Fpdi(); 
if(file_exists("./".$file)){ 
    $pagecount = $pdf->setSourceFile($file); 
}else{ 
    die('Source PDF not found!'); 
} 
 
// Add watermark to PDF pages 
for($i=1;$i&lt;=$pagecount;$i++){ 
    $tpl = $pdf->importPage($i); 
    $size = $pdf->getTemplateSize($tpl); 
    $pdf->addPage(); 
    $pdf->useTemplate($tpl, 1, 1, $size['width'], $size['height'], TRUE); 
     
    //Put the watermark 
    $xxx_final = ($size['width']-50); 
    $yyy_final = ($size['height']-25); 
    $pdf->Image($name.'.png', $xxx_final, $yyy_final, 0, 0, 'png'); 
} 
@unlink($name.'.png'); 
 
// Output PDF with watermark 
$pdf->Output();</pre>



<h2 class="wp-block-heading">Add Image Watermark to PDF </h2>



<p class="wp-block-paragraph">The following example code adds a&nbsp;<strong>watermark image to an existing PDF</strong>&nbsp;file using PHP.</p>



<ul class="wp-block-list"><li>Specify source PDF file and watermark image.</li><li>Initialize Fpdi class and set the source PDF file using <code>setSourceFile()</code> method.</li><li>Add watermark image to PDF pages using <code>importPage()</code>, <code>getTemplateSize()</code>, <code>addPage()</code>, <code>useTemplate()</code>, and <code>Image()</code> methods of Fpdi class.</li><li>Render the generated PDF with watermark using the <code>Output()</code> function of Fpdi class.</li></ul>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">
&lt;?php 

// Load Fpdi library 
use setasign\Fpdi\Fpdi; 
require_once('vendor/autoload.php');

// Source file and watermark config 
$file = 'documents/Proposal.pdf'; 
$text_image = 'images/logo.png'; 
 
// Set source PDF file 
$pdf = new Fpdi(); 
if(file_exists("./".$file)){ 
    $pagecount = $pdf->setSourceFile($file); 
}else{ 
    die('Source PDF not found!'); 
} 
 
// Add watermark image to PDF pages 
for($i=1;$i&lt;=$pagecount;$i++){ 
    $tpl = $pdf->importPage($i); 
    $size = $pdf->getTemplateSize($tpl); 
    $pdf->addPage(); 
    $pdf->useTemplate($tpl, 1, 1, $size['width'], $size['height'], TRUE); 
     
    //Put the watermark 
    $xxx_final = ($size['width']-60); 
    $yyy_final = ($size['height']-25); 
    $pdf->Image($text_image, $xxx_final, $yyy_final, 0, 0, 'png'); 
} 
 
// Output PDF with watermark 
$pdf->Output();</pre>



<h2 class="wp-block-heading">Output PDF with Watermark</h2>



<p class="wp-block-paragraph"><strong>Output()</strong>  is a default method to output the final PDF in the browser.  You can directly save and downloa dthe generated PDF by using some parameters in the Output() method.<br><strong>First Parameter:</strong></p>



<ul class="wp-block-list"><li><code>I</code>&nbsp;– (default) Output PDF to browser.</li><li><code>D</code>&nbsp;– Download PDF file.</li><li><code>F</code>&nbsp;– Save PDF to the local file.</li></ul>



<p class="wp-block-paragraph"><strong>Second Parameter:</strong><br>Specify the file name of the PDF to download.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// Output to browser 
$pdf->Output(); 
 
// Download PDF file 
$pdf->Output('D', 'my-document.pdf'); 
 
// Save PDF to local file 
$pdf->Output('F', 'my-document.pdf');</pre>



<p class="wp-block-paragraph">Hope this tutorial solved your problem. For more about, how to convert html to PDF using dompdf you can simply visit below link.</p>



<p class="wp-block-paragraph">You can also check </p>



<p class="wp-block-paragraph"><a href="https://atcodex.com/laravel/convert-html-to-pdf-in-laravel-with-dompdf/" target="_blank" rel="noreferrer noopener">Convert HTML to PDF in Laravel with Dompdf</a></p>



<p class="wp-block-paragraph"><a href="https://atcodex.com/php/count-the-number-of-pages-in-a-pdf/" target="_blank" rel="noreferrer noopener">How to Count The Number of Pages in a PDF file</a></p>



<p class="wp-block-paragraph"><a href="https://atcodex.com/php/how-to-generate-pdf-file-using-php-and-mpdf-library/" target="_blank" rel="noreferrer noopener">How to generate PDF file using PHP and MPDF library</a></p>



<p class="wp-block-paragraph"><a href="https://atcodex.com/php/how-to-convert-pdf-to-jpeg-image-using-php/" target="_blank" rel="noreferrer noopener">How to Convert PDF to JPEG Image using PHP</a></p>
<p>The post <a href="https://atcodex.com/php/how-to-add-watermark-to-existing-pdf-using-php/">How to Add Watermark to Existing PDF using PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
