How to delete text from PDF using FPDI/ FPDF - php

I Want to edit PDF in php using FPDI/FPDF.I want to replace particular text.I have try many solution but they are not giving the desired result.All are writing some new text in new position. I want to search some text and replace that text with a new text.Is This Possible?If yes please explain.Code:
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pdf->AddPage();
//Set the source PDF file
$pagecount = $pdf->setSourceFile("test.pdf");
//Import the first page of the file
$tpl = $pdf->importPage(1);
//Use this page as template
$pdf->useTemplate($tpl);
//Go vertical position
$pdf->SetY(15);
//Select Arial italic 8
$pdf->SetFont('Arial','I',8);
//Print centered cell with a text in it
$pdf->Cell(0, 10, "Hello World", 0, 0, 'C');
//want something like this
$pdf->replace("old_text","new_text");
$pdf->Output("my_modified_pdf.pdf", "F");

It is not possible to edit a PDF document with FPDI and also it is not possible to replace text of an imported PDF page with FPDI.

Related

How to create a pdf key tags in php

May you please help is my first time doing PDF. I want to create a key tags that will fill in the whole PDF so that I can be able to cut them when i print it. I am failing to place a border outside the text to make a square or rectangle. The first three text must be bold the last test not bold.
I want to create something like this:
<?php
$root =realpath($_SERVER['DOCUMENT_ROOT']);
require($root.'/#services/fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Line(10,10,45,10);
$pdf->Line(10,10,10,25);
$pdf->Line(10,10,45,10);
$pdf->SetFont('Arial','B',8);
$pdf->Write(0.1,"Block E",0,'C');
$pdf->Ln(3);
$pdf->SetFont('Arial','B',8);
$pdf->Write(0.1,"Block / Unit - Area",0,'C');
$pdf->Ln(3);
$pdf->SetFont('Arial','B',8);
$pdf->Write(0.1,"Floor",0,'C');
$pdf->Ln(3);
$pdf->SetFont('Arial','',8);
$pdf->Write(0.1,"Door No. Door No",0,'C');
$pdf->Output();
?>

How do I center text horizontally within an FPDF generated PDF file

I am using FPDI/FPDF to dynamically add text to a PDF file. I would like for the text to be centered horizontally on the PDF regardless of how long the text is, but I am not sure how to set this using $pdf->SetXY
Here is what I have so far:
<?php
use setasign\Fpdi\Fpdi;
require_once('fpdf.php');
require_once('autoload.php');
// initiate FPDI
$pdf = new Fpdi();
$pdf->AddPage();
$pdf->setSourceFile('test2.pdf');
$tplIdx = $pdf->importPage(1);
$size = $pdf->getTemplateSize($tplIdx);
$pdf->useTemplate($tplIdx, null, null, $size['width'], $size['height'],FALSE);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(0, 0, 0);
// Here's what I have tried
$pdf->SetXY($size['width']/2, 100);
//
$pdf->Write(0, 'Lorem Ipsum');
$pdf->Output();
I tried to use the width of the imported PDF divided by 2 as the X position, however this makes it so that the text starts at that point rather than being centered on it.

Why lines/images draw only on last page of pdf using dompdf in php

I am trying to draw line/image on every page of pdf using dompdf but it starts from second page, why this is so ? anyone has any idea ?
Here is my code
$dompdf = new DOMPDF();
$dompdf->load_html($message2);
$dompdf->set_paper('a4','portrait');
$dompdf->render();
$canvas = $dompdf->get_canvas();
//For Header
$header = $canvas->open_object();
$canvas->image($header_image1,'jpg',0, 0, 595, 100);
$canvas->line(0,100,595,100,array(0,0,0),1);
$canvas->close_object();
$canvas->add_object($header, "all");
//For Footer
$footer = $canvas->open_object();
$canvas->line(0,740,650,740,array(0,0,0),1);
$canvas->image($footer_image1,'jpg',0, 742, 595, 100);
$canvas->close_object();
$canvas->add_object($footer, "all");
$output = $dompdf->output();
this code draw line/image on pdf but it only display on last page.
I have two pages in pdf and my line/images are drawn on second page not on first page.
Please suggest any solution.
Adding objects in DomPDF works from the current page onwards. In other words, your objects will get added, but only from the page you currently have and then onwards to any new pages you add.
In your code, you've already converted your HTML to PDF, so the current page is more than likely the last page in your document. So your header / footer are added there, but not to any previous pages.
To place content on every page, domPDF provides two methods: page_text and page_script.
In your case the following type of code should do the trick:
$canvas->page_script('
$pdf->line(10,730,800,730,array(0,0,0),1);
');
Code in the page_script function is then executed for every PDF page.
The line is not displayed for me because embedded php was disabled.
This solved the problem:
$dompdf->set_option("isPhpEnabled", true);
This line has to be placed before $dompdf->render().
how to add image in all pdf page using header and footer.below is my code.
$pdf = App::make('dompdf');
$pdf->loadFile('invoice.html');
$pdf->output();
$dom_pdf = $pdf->getDomPDF();
$canvas = $dom_pdf ->get_canvas();
$image1="logo.png";
$canvas->image($image1,'png', 0, 0, 50, 25);
$canvas->page_text(10, 10, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, array(0, 0, 0));
$pdf->save('pdf_report/eft_payment-'.$RandomAccountNumber.'.pdf');

FPDF Get page numbers at footer on Every A4 size page

I am creating PDF reports using FPDF. Now how do I generate page numbers on each page of a report at the bottom of the page.
Below is the sample code for generating a 2 page PDF.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$start_x=$pdf->GetX();
$current_y = $pdf->GetY();
$current_x = $pdf->GetX();
$cell_width = 25; $cell_height=14;
$j = 20; // This value will be coming from Database so we dont know how many pages the report is going to be
for ($i = 0; $i<$j ; $i++){
$pdf->MultiCell($cell_width,$cell_height,'Hello1',1);
$current_x+=$cell_width;
$pdf->Ln();
}
$pdf->Output();
?>
Note : The $j value will be coming from the database so we don't know how many pages is the report going to be.
To add an A4 page, with portrait orientation, do:
$pdf->AddPage("P","A4");
Create a new class which extends the FPDF class, and override the pre-defined Footer method.
Example:
class PDF extends FPDF
{
function Footer()
{
// Go to 1.5 cm from bottom
$this->SetY(-15);
// Select Arial italic 8
$this->SetFont('Arial','I',8);
// Print centered page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}
}
According to my comment you can place
$pdf->PageNo();
on your page where ever you like. Also you can add a placeholder to this
$pdf->AliasNbPages();
What would look like
$pdf->AliasNbPages('{totalPages}');
By default it's {nb}. It's not necessary to add a placeholder
Than you could add the pagesum like
$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{totalPages}", 0, 1);
or without your own placeholder
$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{nb}", 0, 1);
this would produce e.g.
Page 1/10
in case there were 10 pages :)
But beware
Using the placeholder will mess up the width of the cell. So if you have e.g. 180 page-width than 90 isn't the mid anymore (In the line where you use the placeholder). You will see if you try :)

render HTML from PHP using FPDF

having some problems rendering html when I convert to PDF. It's driving me mental and I don't know what's going on. The idea is that I have an html form in a mysql database which stores HTML tags and what not, I pull it out using PHP, render the HTML and display it as a PDF. My issue is that it won't render the HTML. it's just text and the formatting is way off. Here is the code I have so far:
$indemResult = mysqli_query($conn,"SELECT * FROM Indemnity");
$indemRow = mysqli_fetch_array($indemResult);
require('../include/PDFConverter/fpdf.php');
$pdf = new PDF();
$pdf->SetMargins(0,0,0);
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
//Insert Banner
$pdf->Image('../assets/pdfBanner.png');
$pdf->Ln();
$pdf->Ln();
//Insert Indemnity form
$pdf->SetXY(50, 65);
$pdf->cMargin = 10;
$pdf->SetFont('Arial','',24);
$pdf->Cell(0, 10, $pdf->Write(1,'Indemnity Form'), 0, 1,'C', false);
$pdf->SetFont('Arial','',12);
$text=$pdf->WriteHTML(utf8_decode($indemRow['form']));
$wrap=$pdf->WordWrap($text,120);
$pdf->MultiCell(0,0,$pdf->Write($wrap, ''));
My problem was in the WriteHTML class. It didn't support UL LI tags, so I used this instead:
http://fpdf.de/downloads/addons/53/
I stripped the createPDF class, in the class PDF extends FPDF I removed $_title, $_url, $_debug=false from the PDF function, I also removed $bi from the write HTML function and inside the writeHTML function I removed:
$this->bi=$bi;
if ($bi)
$html=strip_tags($html, "<a><img><p>
<font><tr><blockquote><h1><h2><h3><h4><pre><red><blue><ul><li><hr><b><i><u><strong><em>");
I hope this helps people who are trying to render HTML before putting it onto PDF

Categories