Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Add Watermark using PHP to Existing or New PDF

#1
Add Watermark using PHP to Existing or New PDF

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/07/add-watermark-using-php-to-existing-or-new-pdf.jpg" width="550" height="531" title="" alt="" /></div><div><div class="modified-on" readability="7.0697674418605"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on July 16th, 2022.</div>
<p>FPDF is a fantastic library for working with PDF documents in PHP. It is the most popular one with a large number of plugins that enhances its core features.</p>
<p>Adding a watermark to a PDF document is a basic need in document editing work. Imagine that you have a project where you need to create a feature-rich PDF document editor project with the sole purpose of managing watermarks.</p>
<p>This PHP script will help you as a foundation code to solve your basic watermarking needs. You can use this as a base and build on top of it if you require more.</p>
<p>I have covered two aspects of adding watermarks.</p>
<ol>
<li>Add a watermark to an <strong>existing</strong> PDF document.</li>
<li>Add a watermark to a <strong>new</strong> PDF document.</li>
</ol>
<p>I am also presenting an online demo that watermarks a PDF document using this PHP script.</p>
<p>If you are looking for <a href="https://phppot.com/php/php-watermark/">adding a watermark to a web page with a generated image using PHP</a>, refer to this linked article.</p>
<p><a class="demo" href="https://phppot.com/demo/php-pdf-watermark/">View demo</a></p>
<p><img loading="lazy" class="alignnone size-large wp-image-18713" src="https://phppot.com/wp-content/uploads/2022/07/php-watermark-pdf-550x531.jpg" alt="PHP Watermark PDF" width="550" height="531" srcset="https://phppot.com/wp-content/uploads/2022/07/php-watermark-pdf-550x531.jpg 550w, https://phppot.com/wp-content/uploads/20...00x290.jpg 300w, https://phppot.com/wp-content/uploads/20...68x741.jpg 768w, https://phppot.com/wp-content/uploads/20...rk-pdf.jpg 800w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>Add a watermark to an existing PDF document</h2>
<p>To add a watermark to an existing PDF document, I am using FPDF and FPDI libraries. FPDF is the foundation and FPDI is used to load and edit an existing document.</p>
<p>FPDI helps to load an existing PDF document and use it as a template in FPDF to create a document.</p>
<p>You need to download both <a href="http://www.fpdf.org/">FPDF</a> and <a href="https://github.com/Setasign/FPDI">FPDI</a> libraries and add it to the project. Refer to the project file structure image below.</p>
<p class="code-heading">existing-pdf-watermark.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require_once __DIR__ . '/fpdf/fpdf.php';
require_once __DIR__ . '/FPDI/src/autoload.php'; function addWatermark($x, $y, $watermarkText, $angle, $pdf)
{ $angle = $angle * M_PI / 180; $c = cos($angle); $s = sin($angle); $cx = $x * 1; $cy = (300 - $y) * 1; $pdf-&gt;_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, - $s, $c, $cx, $cy, - $cx, - $cy)); $pdf-&gt;Text($x, $y, $watermarkText); $pdf-&gt;_out('Q');
} $pdf = new \setasign\Fpdi\Fpdi();
$fileInput = 'example.pdf';
$pages_count = $pdf-&gt;setSourceFile($fileInput);
for ($i = 1; $i &lt;= $pages_count; $i ++) { $pdf-&gt;AddPage(); $tplIdx = $pdf-&gt;importPage($i); $pdf-&gt;useTemplate($tplIdx, 0, 0); $pdf-&gt;SetFont('Times', 'B', 70); $pdf-&gt;SetTextColor(192, 192, 192); $watermarkText = 'CONFIDENTIAL'; addWatermark(105, 220, $watermarkText, 45, $pdf); $pdf-&gt;SetXY(25, 25);
}
$pdf-&gt;Output();
?&gt;
</code></pre>
<h2>Add a watermark to a new PDF document</h2>
<p>Following PHP script is as simple as it gets. The Header function is used to render the PDF document page header. This is called by the AddPage function.</p>
<p>addWatermark is the key function responsible for creating the watermark. It sets the watermark text and performs the rotation to position it across the document. The X and Y location of where to start the watermark text and its color is defined in the Header function.</p>
<p>You can make adjustments by setting a smaller font, position, color, etc as per your choice.</p>
<p class="code-heading">new-pdf-watermark.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
require __DIR__ . '/fpdf/fpdf.php'; class PDF extends FPDF
{ function Header() { // setting the font, color and text for watermark $this-&gt;SetFont('Times', 'B', 48); $this-&gt;SetTextColor(140, 180, 205); $watermarkText = 'New PDF Watermark - PHP'; $this-&gt;addWatermark(35, 190, $watermarkText, 45); } function addWatermark($x, $y, $watermarkText, $angle) { $angle = $angle * M_PI / 180; $c = cos($angle); $s = sin($angle); $cx = $x * $this-&gt;k; $cy = ($this-&gt;h - $y) * $this-&gt;k; $this-&gt;_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, - $s, $c, $cx, $cy, - $cx, - $cy)); $this-&gt;Text($x, $y, $watermarkText); $this-&gt;_out('Q'); }
} $pdf = new PDF();
$pdf-&gt;AddPage();
$pdf-&gt;SetFont('Arial', '', 12);
$pdfDocumentContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. .\n\n";
for ($i = 0; $i &lt; 15; $i ++) { $pdf-&gt;MultiCell(0, 5, $pdfDocumentContent, 0, 'J');
}
$pdf-&gt;Output();
?&gt;
</code></pre>
<h2>PHP project structure</h2>
<p><img loading="lazy" class="alignnone size-full wp-image-18716" src="https://phppot.com/wp-content/uploads/2022/07/php-pdf-watermark-project-structure.jpg" alt="PHP PDF Watermark Project Structure" width="307" height="377" srcset="https://phppot.com/wp-content/uploads/2022/07/php-pdf-watermark-project-structure.jpg 307w, https://phppot.com/wp-content/uploads/20...44x300.jpg 244w" sizes="(max-width: 307px) 100vw, 307px"></p>
<p>I have given the complete PHP project as a free download below. The dependent libraries FPDF and FPDI are not available in the project download zip file. You can download them from their official website and add them to your project as per the given project structure above before adding watermarks.</p>
<p><a class="demo" href="https://phppot.com/demo/php-pdf-watermark/">View demo</a> <a class="download" href="https://phppot.com/downloads/php-pdf-watermark.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/php-watermark-pdf/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2022/07/...r-new-pdf/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016