I am using TCPDF to add an image watermark to my existing pdf. Only a few of the pdf files are loading the watermark properly (2nd image) and some of them do not support a watermark image (1st image). Also, I want to remove the watermark from the last page of the generated pdf. I am sharing a pdf sample without a watermark.
Following the code, used to create a watermark image to exiting pdf. I have created a library to add a header() and footer(). Why is it working fine in some PDF and others not?
use setasignFpdiFpdi; use setasignFpdiPdfParserStreamReader; function Header() { ////Logo $logo= FCPATH.'logo.png'; $this->Image($logo, 10, 10, 20, 15, 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false); // Set font $this->SetFont('freeserif', 'B', 15); $this->Cell(0, 20, $this->CustomHeaderText, 0, false, 'C', 0, '', 0, false, 'T', 'C'); // Set font $this->SetFont('freeserif', 'B', 9); $this->Cell(0, 10, ''.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); // Get the current page break margin $bMargin = $this->getBreakMargin(); // Get current auto-page-break mode $auto_page_break = $this->AutoPageBreak; //watermar opacity $this->SetAlpha(0.3); // Define the path to the image that you want to use as a watermark. $watermark_img= FCPATH.'watermark.png'; // Render the image $this->Image($watermark_img, 0, 110, 50, 50, 'PNG', '', 'M', false, 300, 'C', false, false, 0); // Restore the auto-page-break status $this->SetAutoPageBreak(true, 15); // Set the starting point for the page content $this->setPageMark(); }
1.Sample.pdf which is not loading watermark inside pdf
Advertisement
Answer
I have created a library which define all header() , footer() function. I have made changes in the library file.I have remove all watermark generation code from library and call it where the actual PDF is generation function and my error was solved
// initiate PDF library
$pdf = new Digilib(); $pdf->setDate($date); $pdf->name = ucfirst($name); $url_curl = $distination_folder . $Filename; $fileContent = file_get_contents($url_curl, false, stream_context_create(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false)))); $pageCount = $pdf->setSourceFile(StreamReader::createByString($fileContent)); for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) { $templateId = $pdf->importPage($pageNo); $size = $pdf->getTemplateSize($templateid); $pdf->AddPage('P', array($size['width'], $size['height'])); $pdf->useTemplate($templateId); $pdf->Image($logopath, 10, 10, 15, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false); $pdf->SetFont('freeserif', 'b', 15); $pdf->Cell(0, 2, $CustomHeaderText, 0, false, 'C', 0, '', 0, false, 'T', 'C'); $pdf->SetFont('freeserif', 'b', 9); $pdf->Cell(0, 2, '' . $pdf->getAliasNumPage() . '/' . $pdf->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); $ImageW = 105; //WaterMark Size $ImageH = 80; $pdf->setPage($pageNo); //WaterMark Page $myPageWidth = $pdf->getPageWidth(); $myPageHeight = $pdf->getPageHeight(); $myX = ($myPageWidth / 2) - 50; //WaterMark Positioning $myY = ($myPageHeight / 2) - 40; $pdf->SetAlpha(0.35); $pdf->Image($watermarkpath, $myX, $myY, $ImageW, $ImageH, '', '', 'C', true, 300); $pdf->SetAlpha(1); $pdf->SetFooterMargin(0); }