Search code examples
phpfpdf

inserting the watermark and printing the PDF after generating by FPDF


I have generated one PDF using FPDF library. Now I am not able to Get any Clue to Insert water mark Onthe PDF and Print the PDF. I tried Different method available on the ne but I m not able to achieve it any help is highly appreciated. My code for this is:

<?php
require_once("includes/config.php");
//

require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdf\fpdf.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\autoload.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\fpdi.php');
    
    class mypdf extends FPDF{
    function Heeader()
    {
function Header()
{
    /* Put the watermark */
    $this->SetFont('Arial','B',50);
    $this->SetTextColor(255,192,203);
    $this->RotatedText(35,190,'W a t e r m a r k   d e m o',45);
}

function RotatedText($x, $y, $txt, $angle)
{
    /* Text rotated around its origin */
    $this->Rotate($angle,$x,$y);
    $this->Text($x,$y,$txt);
    $this->Rotate(0);
}
}

    function Footer()
    {
        
        // Position at 1.5 cm from bottom
        $this->SetY(179);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Text color in gray
        $this->SetTextColor(128);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
    }
    function headerTable(){
        $this->setfont('Arial','BU',14);
        $this->Ln();
        $this->cell(270,14,'List Of Records' ,0,0,'C');
        $this->Ln();
        $currentDate = date("j/n/Y");
        $this->cell(270,14,$currentDate,0,0,'R');
        $this->Ln();
        $this->Ln();
        $this->setfont('Arial','B',12);
        $this->cell(12,10,'Sl No',1,0,'L');
        $this->cell(75,10,'USER NAME' ,1,0,'c');
        $this->cell(80,10,'AFFILIATION',1,0,'c');
        $this->cell(34,10,'SAMPLE TYPE' ,1,0,'c');
        $this->cell(30,10,'SAMPLE ID',1,0,'c');
        $this->cell(50,10,'DATE',1,0,'c');
        $this->Ln();        
        }
        
    function viewTable(){
        $this->setfont('Arial','B',12);
        $search=$_POST['search1'];
        $option=$_POST['option1'];
        $period=$_POST['period1'];
        $datefrom=$_POST['datefrom1'];
        $dateto=$_POST['dateto1'];
        $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));


            if($period ==null){
            $sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date 
            FROM tblfacility 
            JOIN tbluser on tbluser.id=tblfacility.user 
            where ".$search." ='".$option."' ";}
            else{
            $sql="SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where ".$search." ='".$option."' AND time between '".$datefrom."' and '".$dateto."' ";
            }
            $query = $dbh->prepare($sql);
            $query->execute();
            $results=$query->fetchAll(PDO::FETCH_OBJ);
            //echo "<prep>";
            //echo "this is the final";
            //print_r($sql);
            $i=1;           
            foreach($results as $result){
                $this->cell(12,10,$i,1,0,'L');
                $this->cell(75,10,$result->user,1,0,'L');
                $this->cell(80,10,$result->affiliation,1,0,'L');
                $this->cell(34,10,$result->type,1,0,'L');
                $this->cell(30,10,$result->sampleid,1,0,'L');
                $this->cell(50,10,$result->time,1,0,'L');
                $this->Ln();
                $i++;
            }
            
        
    }
    
    }
$pdf = new mypdf();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->header();
$pdf->headerTable();
$pdf->viewTable();
$pdf->footer();
$pdf->output();


?>

What may be problem with my code


Solution

  • I think you are trying to use the example from http://www.fpdf.org/en/script/script9.php

    But this script is using another class "PDF_Rotate" that your "mypdf" class should inherit from.

    There is also a problem with your function Heeader() that should not exist at all.

    I added the PDF_Rotate class definition ( from http://www.fpdf.org/en/script/script9.php ) and removed the unnecessary Heeader function declaration in the script below:

    <?php
    require_once("includes/config.php");
    //
    
    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdf\fpdf.php');
    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\autoload.php');
    require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\fpdi.php');
    
    
    
    class PDF_Rotate extends FPDF
    {
        var $angle=0;
    
        function Rotate($angle,$x=-1,$y=-1)
        {
            if ($x == - 1)
                $x = $this->x;
            if ($y == - 1)
                $y = $this->y;
            if ($this->angle != 0)
                $this->_out('Q');
            $this->angle = $angle;
            if ($angle != 0) {
                $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));
            }
        }
    
        function _endpage()
        {
            if($this->angle!=0)
            {
                $this->angle=0;
                $this->_out('Q');
            }
            parent::_endpage();
        }
    }
    
    
    class mypdf extends PDF_Rotate
    {
        function Header()
        {
            /* Put the watermark */
            $this->SetFont('Arial', 'B', 50);
            $this->SetTextColor(255, 192, 203);
            $this->RotatedText(35, 190, 'W a t e r m a r k   d e m o', 45);
        }
    
        function RotatedText($x, $y, $txt, $angle)
        {
            /* Text rotated around its origin */
            $this->Rotate($angle, $x, $y);
            $this->Text($x, $y, $txt);
            $this->Rotate(0);
        }
    
        function Footer()
        {
            // Position at 1.5 cm from bottom
            $this->SetY(179);
            // Arial italic 8
            $this->SetFont('Arial', 'I', 8);
            // Text color in gray
            $this->SetTextColor(128);
            // Page number
            $this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');
        }
    
        function headerTable()
        {
            $this->setfont('Arial', 'BU', 14);
            $this->Ln();
            $this->cell(270, 14, 'List Of Records', 0, 0, 'C');
            $this->Ln();
            $currentDate = date("j/n/Y");
            $this->cell(270, 14, $currentDate, 0, 0, 'R');
            $this->Ln();
            $this->Ln();
            $this->setfont('Arial', 'B', 12);
            $this->cell(12, 10, 'Sl No', 1, 0, 'L');
            $this->cell(75, 10, 'USER NAME', 1, 0, 'c');
            $this->cell(80, 10, 'AFFILIATION', 1, 0, 'c');
            $this->cell(34, 10, 'SAMPLE TYPE', 1, 0, 'c');
            $this->cell(30, 10, 'SAMPLE ID', 1, 0, 'c');
            $this->cell(50, 10, 'DATE', 1, 0, 'c');
            $this->Ln();
        }
    
        function viewTable()
        {
            $this->setfont('Arial', 'B', 12);
            $search = $_POST['search1'];
            $option = $_POST['option1'];
            $period = $_POST['period1'];
            $datefrom = $_POST['datefrom1'];
            $dateto = $_POST['dateto1'];
            $dbh = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
    
            if ($period == null) {
                $sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date
                FROM tblfacility
                JOIN tbluser on tbluser.id=tblfacility.user
                where " . $search . " ='" . $option . "' ";
            } else {
                $sql = "SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where " . $search . " ='" . $option . "' AND time between '" . $datefrom . "' and '" . $dateto . "' ";
            }
            $query = $dbh->prepare($sql);
            $query->execute();
            $results = $query->fetchAll(PDO::FETCH_OBJ);
            // echo "<prep>";
            // echo "this is the final";
            // print_r($sql);
            $i = 1;
            foreach ($results as $result) {
                $this->cell(12, 10, $i, 1, 0, 'L');
                $this->cell(75, 10, $result->user, 1, 0, 'L');
                $this->cell(80, 10, $result->affiliation, 1, 0, 'L');
                $this->cell(34, 10, $result->type, 1, 0, 'L');
                $this->cell(30, 10, $result->sampleid, 1, 0, 'L');
                $this->cell(50, 10, $result->time, 1, 0, 'L');
                $this->Ln();
                $i++;
            }
        }
    }
    
    $pdf = new mypdf();
    $pdf->AliasNbPages();
    $pdf->AddPage('L', 'A4', 0);
    $pdf->header();
    $pdf->headerTable();
    $pdf->viewTable();
    $pdf->footer();
    $pdf->output();