Edit Existing PDF multiple page File using FPDF & FPDI

▼魔方 西西 提交于 2019-11-30 16:35:34

It's hard to try without FPDI installed. But the core idea would be following I believe:

<?php

  require_once('fpdf.php');
  require_once('fpdi.php');

  // initiate FPDI
  $pdf = new FPDI();

  /* <Virtual loop> */
  $pdf->AddPage();
  $pdf->setSourceFile('ex.pdf');
  $tplIdx = $pdf->importPage(1);

  $pdf->useTemplate($tplIdx, 10, 10, 200);

  // now write some text above the imported page
  $pdf->SetFont('Arial');
  $pdf->SetTextColor(255,0,0);
  $pdf->SetXY(50, 50);
  $pdf->Write(0, "Ajay Patel");

  /* </Virtual loop/> */

  $pdf->AddPage();
  //$pdf->setSourceFile('ex.pdf');
  $tplIdx = $pdf->importPage(2);

  $pdf->useTemplate($tplIdx, 10, 10, 200); // dynamic parameter based on your page

  $pdf->SetFont('Arial');
  $pdf->SetTextColor(255,0,0);
  $pdf->SetXY(50, 50);
  $pdf->Write(0, "Ajay Patel2");

  $pdf->Output('newpdf1.pdf', 'D');
?>

If this works you can get rid of the second block of the code and out this on a loop (and dynamic positioning as well).

Thanks @J A Your idea works for me

I just posted answer for other to help them

<?php
require_once('fpdf.php');
require_once('fpdi.php');

// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('newpdf.pdf');

// import page 1
$tplidx = $pdf->importPage(1);
for ($i = 1; $i < 6; $i++) { 
              $tplidx = $pdf->ImportPage($i); 


                     $pdf->useTemplate($tplidx, 10, 10, 200);
                     $pdf->AddPage();

                     $pdf->SetFont('Arial');
                     $pdf->SetTextColor(0,0,0);
                     $pdf->SetFontSize(8);

                     if ($i==3) {
                        $pdf->SetXY(50, 124);
                        $pdf->Write(1, "Ajay Patel");

                        $pdf->SetXY(50, 133);
                        $pdf->Write(1, date("d/m/Y"));
                     }

                     if ($i==4) {
                        $pdf->SetXY(50, 171);
                        $pdf->Write(1, "Ajay Patel");

                        $pdf->SetXY(50, 185);
                        $pdf->Write(1, date("d/m/Y"));
                     }

                }

$pdf->Output('newpdf1.pdf', 'D');
?>
bjoern

You should really make use of the return value of setSourceFile to iterate over all pages:

Description

public int FPDI::setSourceFile ( string $filename )

Depending on the PDF version of the used document the PDF version of the resulting document will be adjusted to the higher version.

Parameters

$filename : string // A valid path to the PDF document from which pages should be imported from

Return Values

The number of pages in the document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!