-1

I have been working with the following code to put a image stamp onto a single page in a multi page pdf document. the problem i have is if i set the page to be page 5 it will not save the rest of the PDF if i make it the last page it works i want to be able to select any page in the document. below is my code

  <?php

  require_once('fpdf/fpdf.php');
  require_once('fpdi/fpdi.php');
$pdf = new FPDI();
$fullPathToPDF = 'scann/Mackay/my.pdf';

$pdf->setSourceFile($fullPathToPDF);
//echo $pageCount;

for ($i = $t; $i <= $pageCount; $i++) {
    $pdf->importPage($t);
    $pdf->AddPage();
    $pdf->useTemplate($t);
}
//puting the stamp onto the page 
$pdf->Image('Posted.png', 100, 120, 0, 0, 'png');
//save the file 
$pdf->Output($fullPathToPDF, 'F');
?>
Tom
  • 17
  • 6

2 Answers2

1

You need to insert the image while you are looping through all the source file pages in your for loop:

require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
$pdf = new FPDI();
$fullPathToPDF = 'scann/Mackay/my.pdf';

$pageCount = $pdf->setSourceFile($fullPathToPDF);
//echo $pageCount;

for ($i = 1; $i <= $pageCount; $i++) {
    $pdf->importPage($i);
    $pdf->AddPage();
    $pdf->useTemplate($i);

    if($i == 5){
        //puting the stamp onto the page 
        $pdf->Image('Posted.png', 100, 120, 0, 0, 'png');
    }
}

//save the file 
$pdf->Output($fullPathToPDF, 'F');
?>
Wade
  • 129
  • 4
1

Wades solution did not work for me, I think the newest version of FPDI changed something about the handling. I had to assign $pdf->importPage($i) into a new variable and pass that to useTemplate to get it to work.

for ($i = 1; $i <= $pageCount; $i++) {
    $pdf->AddPage();
    $imported = $pdf->importPage($i);
    $pdf->useTemplate($imported);

    if($i == 5){
        //puting the stamp onto the page 
        $pdf->Image('Posted.png', 100, 120, 0, 0, 'png');
    }
}
BenBen
  • 67
  • 3