2

I know this question has been asked many times but I read all related answer and my problem is not solved yet. I can add text and image on a new blank pdf file by code below.

<?Php
require('fpdf.php');
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();

/* $pdf->setSourceFile("test1.pdf"); */

$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(0, 5);
$pdf->Write(0, 'This is just a simple text');

$pdf->Image('sample.png',100,0);
$pdf->Output('file.pdf','F');
?>

But when I uncomment the line /* $pdf->setSourceFile("sourse.pdf"); */ to add text and image on existing pdf, I get Uncaught Error: Call to undefined method FPDF::setSourceFile() . Also when add

require('fpdi.php');
$pdf = new FPDI('P','mm','A4');

I get Uncaught Error: Class "setasign\Fpdi\FpdfTpl" not found . How can I solve this?

  • In the first error it would seem you're missing `FPDI`. In the second it would seem you're missing `FPDF`. The errors seem consistent with this hypothesis and your require statements seem to agree too. – nitrin0 Oct 01 '21 at 10:15

1 Answers1

3

You have to set source file and after that import page from source file to your new created file by this way you can edit it. Please find more details from https://www.webniraj.com/2016/09/12/creating-editing-a-pdf-using-php/

// Create new Landscape PDF
$pdf = new FPDI('l');

// Reference the PDF you want to use (use relative path)
$pagecount = $pdf->setSourceFile( 'certificate.pdf' );

// Import the first page from the PDF and add to dynamic PDF
$tpl = $pdf->importPage(1);
$pdf->AddPage();

// Use the imported page as the template
$pdf->useTemplate($tpl);

// render PDF to browser
$pdf->Output();
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
Foramkumar Patel
  • 299
  • 3
  • 10
  • Thanks for your answer but as I said I get error in line ``` $pdf = new FPDI('l'); ``` if this problem be solved I will add first page template and etc. – Saeed Eisakhani Oct 01 '21 at 11:06
  • you have to install lib for that composer require setasign/fpdf setasign/fpdi-fpdf Find more details https://github.com/Setasign/FPDI – Foramkumar Patel Oct 01 '21 at 11:14
  • I read reference and it says: If you do not use composer, just require the autoload.php in the /src folder: ```require_once('src/autoload.php');``` . I did that but I steel get Uncaught Error: Class "setasign\Fpdi\FpdfTpl" not found in – Saeed Eisakhani Oct 01 '21 at 11:32
  • Foramkumar Patel I really don't know what to install and how. I am a beginner. would you please tell me what to do? – Saeed Eisakhani Oct 02 '21 at 20:07