Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Add a watermark to an existing PDF document

<?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->_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->Text($x, $y, $watermarkText);
    $pdf->_out('Q');
}

$pdf = new setasignFpdiFpdi();
$fileInput = 'example.pdf';
$pages_count = $pdf->setSourceFile($fileInput);
for ($i = 1; $i <= $pages_count; $i ++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($i);
    $pdf->useTemplate($tplIdx, 0, 0);
    $pdf->SetFont('Times', 'B', 70);
    $pdf->SetTextColor(192, 192, 192);
    $watermarkText = 'CONFIDENTIAL';
    addWatermark(105, 220, $watermarkText, 45, $pdf);
    $pdf->SetXY(25, 25);
}
$pdf->Output();
?>
Comment

Add a watermark to a new PDF document

<?php
require __DIR__ . '/fpdf/fpdf.php';

class PDF extends FPDF
{

    function Header()
    {
        // setting the font, color and text for watermark
        $this->SetFont('Times', 'B', 48);
        $this->SetTextColor(140, 180, 205);
        $watermarkText = 'New PDF Watermark - PHP';
        $this->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->k;
        $cy = ($this->h - $y) * $this->k;
        $this->_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->Text($x, $y, $watermarkText);
        $this->_out('Q');
    }
}

$pdf = new PDF();
$pdf->AddPage();
$pdf->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. .

";
for ($i = 0; $i < 15; $i ++) {
    $pdf->MultiCell(0, 5, $pdfDocumentContent, 0, 'J');
}
$pdf->Output();
?>
Comment

PREVIOUS NEXT
Code Example
Php :: dump request in ci 
Php :: check which php.ini file enabled from code 
Php :: The requested URL was not found on this server. Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/7.3.28 Server at localhost Port 80 error in laravel 
Php :: find only selected columns 
Php :: laravel check if pagination is empty 
Php :: cakephp 3 migrations foreign key 
Php :: wp ajax error handling 
Php :: ph address format 
Php :: php tasks 
Php :: in php einen div 
Php :: phpstormda php faylning tepasiga avto kommet yozish 
Php :: laravel make request 
Php :: howto+add+header+bar+laravel+app 
Php :: how to check request method in php 
Php :: phpdoc array type 
Php :: breaking long array in php 
Php :: laravel retain old value 
Php :: laravel passport login with username 
Php :: solaris 11 php mysql 
Php :: Yii2 Dynamic Relational, Eager loading 
Php :: how to clear post array on referesh + not refill when return 
Php :: Comment désactiver la barre latérale Widgets sur des pages spécifiques WordPress 
Php :: select next occurrence phpstorm 
Php :: php send to message to mobile number using springedge 
Php :: laravel components 
Php :: php 7.1.1 download 
Php :: how to store array in variable php 
Php :: provide difference between interface and abstract class php 
Php :: Syntax error or access violation: 1055 
Php :: change php version 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =