laravelpdflaravel-5fpdffpdi

If Possible to Read existing PDF content position and add the some content after over the existing content in laravel?


Laravel Package: "setasign/fpdi": "^2.3", "setasign/fpdf": "^1.8"

$pdf = new \setasign\Fpdi\Fpdi('L','mm','A4');
$pageCount = $pdf->setSourceFile(public_path().'/'.$url);
$pdf->setFont('Arial', 'B', 10);
for($i = 1; $i <= $pageCount; $i++){
    $tplIdx = $pdf->importPage($i);
    $pageDimensions = $pdf->getImportedPageSize($tplIdx);
    $pdf->addPage($pageDimensions['orientation'], $pageDimensions);
    $pdf->useTemplate($tplIdx);
 }

If It Possible read the content of the last page and get after page content ordinate of the current position. then Write new content without add new page or whitespaces


Solution

  • If Pdf Generate using DomPdf so you can generate HTML file using

    $pdf->getDomPDF()->outputHtml()
    
    

    Generated html file using DomPDF add some element like this:

    <div id="divId"></div>

    Then whenever you need to edit this document, you need to read the HTML file line by line and Edit document:

    $url=public_path() .'/test.html';
    $htmlFile='';
    foreach(file($url) as  $key => $line){
      if(strpos($line, 'id="divId"') !== false){
        $line='<div>Hello world</div>';
      }
      $htmlFile.=$line;
    }
    $pdf = PDF::loadHTML($htmlFile)->setPaper('a4', 'landscape');
    $pdf->output();