Search code examples
phppdffpdffpdi

PHP Fpdi error "unexpected T_USE, expecting T_FUNCTION"


I'm trying to merge two pdf with fpdi and fpdf but I got an error, and I didn't found anything about that. I took the basic code from https://www.setasign.com/products/fpdi/demos/concatenate-fake/ I'm also using CodeIgniter.

There is my code :

use setasign\Fpdi\Fpdi;
require_once(APPPATH. 'third_party/fpdf/fpdf.php');
require_once(APPPATH. 'third_party/fpdi/src/autoload.php');

$files = array($this->internalURL . "assets/uploadedContent/Test.pdf", $this->internalURL . "assets/uploadedContent/Test.pdf");
$pageCount = 0;


class ConcatPdf extends Fpdi
{
    public $files = array();

    public function setFiles($files)
    {
        $this->files = $files;
    }

    public function concat()
    {
        foreach($this->files AS $file) {
            $pageCount = $this->setSourceFile($file);
            for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
                $pageId = $this->ImportPage($pageNo);
                $s = $this->getTemplatesize($pageId);
                $this->AddPage($s['orientation'], $s);
                $this->useImportedPage($pageId);
            }
        }
    }
}

$pdf = new ConcatPdf();
$pdf->setFiles($files);
$pdf->concat();

$pdf->Output('I', './assets/uploadedContent/concat.pdf');

And I got this error :

Parse error: syntax error, unexpected T_USE, expecting T_FUNCTION in /var/www/sites/***********/public/application/third_party/fpdi/src/Fpdi.php on line 27

The error comes from Fpdi's file at this line :

use FpdiTrait;

I don't know if it comes from me or ?


Solution

  • You are using an outdated PHP version. Update to a recent version. FPDI 2 supports PHP 5.6 and up.