2

i am trying to get a hang of FPDI to combine PDFs. Been trying on the example codes given by setasign.

FPDF - fpdf.php is from fpdf.org, v1.81 Fpdi - i took the src from setasign, v2beta2

<?php

error_reporting(E_ALL);
ini_set('display_errors',1);
use \setasign\Fpdi;

// require_once('fpdf/fpdf.php');
// require_once('fpdi2/src/autoload.php');

require_once('fpdf181/fpdf.php');
require_once('fpdf181/fpdi/autoload.php');

// initiate FPDI
$pdf = new Fpdi\Fpdi();
// add a page
$pdf->AddPage();
// set the source file
$pdf->setSourceFile("projects/78/78_timesheet.pdf");
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useImportedPage($tplIdx, 10, 10, 100);

// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');

// $pdf->Output();

$dir = "reports/";
$file = 'test.pdf';
$dir_file = $dir.$file;
$pdf->Output($dir_file,'F');
?>

The code does not create any PDF. When i change $pdf = new Fpdi\Fpdi(); to $pdf = new FPDF(); it shows an error: PHP Fatal error: Call to undefined method FPDF::setSourceFile() in testpdfgen.php on line 19

what am i doing wrong here?

NewProgrammer
  • 295
  • 1
  • 4
  • 21
  • The file should be written to your disc. You made sure that it's not there? What about changing 'F' to 'D'? – Jan Slabon Sep 06 '17 at 14:20
  • It isn't there. When I change to $PDF=new FPDF(); and omit the set source, the PDF is created. But of coz without the intended PDF import. – NewProgrammer Sep 06 '17 at 14:28
  • Changed 'F' to 'D' and the following error showed up: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /fpdf181/fpdi/FpdiTrait.php on line 98 – NewProgrammer Sep 06 '17 at 14:49
  • 1
    You need PHP >= 5.6 with FPDI 2. I'm still wondering why this error message doesn't came up before. – Jan Slabon Sep 06 '17 at 15:08
  • i'll try to upgrade and see if it helps. thanks! – NewProgrammer Sep 06 '17 at 15:20
  • I did an upgrade to PHP 7.1 $pdf->Output($dir_file,'F'); works, thanks! – NewProgrammer Sep 07 '17 at 04:17
  • Jan Slabon, since u wrote the code for FPDI, i want to ask another question that's unrelated to the above problem but still involves FPDI. I want to add headers and footers into pages i created. In the tutorials from FPDF.org is to create a class PDF extend FPDF, then call as $pdf = new PDF(); how do i do it since i will be calling $pdf = new Fpdi\Fpdi(); ? thanks for helping me out so far – NewProgrammer Sep 07 '17 at 04:32
  • `class PDF extend Fpdi\Fpdi` but you should open another question for this instead of hijacking the comments of another question. – Jan Slabon Sep 07 '17 at 06:22

0 Answers0