Page 1 of 1

Adding watermark to PDF with php

Posted: 16 Dec 2020, 05:58
by JanMolendijk
Coursesweb next problem I try for hours to add watermark in my php document.

I getting errors or non result

Code: Select all

class PDF_Rotate extends FPDF is implent succesfull
I don`t get any result or errors when I place it on a wrong line.
Even $txt I don`t wanna have in my document but only the watermark.

The script is succesfull with even adding new line from post_text
what I asked in the topic before (found this solution by my self)

Hope you can help me with this ????

Code: Select all

<?php 
  
include('connection.php');

$id = (int) $_GET['id'];

$sql=mysqli_query($conn,"SELECT * FROM `w3_post` WHERE post_id='" . $id . "' ");
$users=mysqli_fetch_assoc($sql);
?>

<?php
include('database.php');
$database = new Database();
$result = $database->runQuery("SELECT title,post_text FROM w3_post where title='Wit Rusland Belarus'");
$header = $database->runQuery("SELECT UCASE(`COLUMN_NAME`) 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='comments' 
AND `TABLE_NAME`='w3_post'
and `COLUMN_NAME` in ('title','post_text')");

require('fpdf/fpdf.php');

?>

Code: Select all

<?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();
}
}

?>

Code: Select all

<?php

class PDF extends FPDF
{
protected $B = 0;
protected $I = 0;
protected $U = 0;
protected $HREF = '';

function WriteHTML($html)
{
    // HTML parser
    $html = str_replace("\n",'<br>',$html);
    $a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
    foreach($a as $i=>$e)
    {
        if($i%2==0)
        {
            // Text
            if($this->HREF)
                $this->PutLink($this->HREF,$e);
            else
                $this->Write(5,$e);
        }
        else
        {
            // Tag
            if($e[0]=='/')
                $this->CloseTag(strtoupper(substr($e,1)));
            else
            {
                // Extract attributes
                $a2 = explode(' ',$e);
                $tag = strtoupper(array_shift($a2));
                $attr = array();
                foreach($a2 as $v)
                {
                    if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
                        $attr[strtoupper($a3[1])] = $a3[2];
                }
                $this->OpenTag($tag,$attr);
            }
        }
    }
}

function OpenTag($tag, $attr)
{
    // Opening tag
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyle($tag,true);
    if($tag=='A')
        $this->HREF = $attr['HREF'];
    if($tag=='BR')
        $this->Ln(5);
}

function CloseTag($tag)
{
    // Closing tag
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyle($tag,false);
    if($tag=='A')
        $this->HREF = '';
}

function SetStyle($tag, $enable)
{
    // Modify style and select corresponding font
    $this->$tag += ($enable ? 1 : -1);
    $style = '';
    foreach(array('B', 'I', 'U') as $s)
    {
        if($this->$s>0)
            $style .= $s;
    }
    $this->SetFont('',$style);
}

function PutLink($URL, $txt)
{
    // Put a hyperlink
    $this->SetTextColor(0,0,255);
    $this->SetStyle('U',true);
    $this->Write(5,$txt,$URL);
    $this->SetStyle('U',false);
    $this->SetTextColor(0);
}
}

$html = $users['post_user_name'] .'<br><br> '. $users['title'] .' <br><br><br><br><br><br><br><br>

<br><br><br><br><br><br><br><br><br><br><br><br><br>
 '. $users['post_text'] .' <br><br>  You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
<u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
text, such as


<br><br>
<a href="http://www.fpdf.org">Brought by @Molendijk (visit website)</a>';

$pdf = new PDF();

$pdf = new PDF();
// First page
$pdf->AddPage();
$pdf->SetFont('Arial','',20);

$pdf->Write(10," $users[post_user_name]  ");

$pdf->SetFont('','U',20);
$link = $pdf->AddLink();

$pdf->Write(10,' Click to read',$link);

$pdf->SetFont('');

// Second page
$pdf->AddPage();

$pdf->SetLink($link);
$pdf->Image('http://145.53.93.209/Molendijk/add/load/pictures/'. $users['post_image'] .'',10,10,200,130,'','http://www.fpdf.org');
$pdf->SetLeftMargin(5);
$pdf->SetFontSize(14);
$pdf->WriteHTML($html);

$pdf->Output();
?>
The script above is complete working (usefull for everyone)
But when I add the code under non results show-up
+ also I don`t need $txt

class PDF_Rotate extends FPDF
class PDF extends PDF_Rotate
toghter is working normal this
example I downloaded from fpdf.org

Code: Select all

<?php


class PDF 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);
}
}

$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
$txt="FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say ".
	"without using the PDFlib library. F from FPDF stands for Free: you may use it for any ".
	"kind of usage and modify it to suit your needs.\n\n";
for($i=0;$i<25;$i++) 
	$pdf->MultiCell(0,5,$txt,0,'J');
$pdf->Output();
?>

Adding watermark to PDF with php

Posted: 16 Dec 2020, 08:44
by MarPlo
Sorry, to much code for me, and I have no experience with creating pdf documents.

Adding watermark to PDF with php

Posted: 16 Dec 2020, 09:35
by JanMolendijk
Ok thanks for supporting I gonna ask Stackoverflow....

Adding watermark to PDF with php

Posted: 19 Dec 2020, 06:56
by JanMolendijk
My account is blocked on stackoverflow because
according to them the question was asked wrong

Adding watermark to PDF with php

Posted: 19 Dec 2020, 08:39
by MarPlo
Well, it can't be helped; I'm not so good with pdf in php.