Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declaration of setasign\Fpdi\FpdfTplTrait::setPageFormat($size, $orientation) should be compatible with TCPDF::setPageFormat($format, $orientation = 'P') #142

Closed
williamdes opened this issue Jan 18, 2022 · 6 comments

Comments

@williamdes
Copy link

Bug report

phpstan:

     Internal error: Internal error: Declaration of setasign\Fpdi\FpdfTplTrait::setPageFormat($size, $orientation) should be compatible with TCPDF::setPageFormat($format, $orientation = 'P') in file  
    InvoiceServiceImpl.php 

Code snippet that reproduces the problem

<?php

namespace App\xxx;

use iio\libmergepdf\Driver\Fpdi2Driver;
use iio\libmergepdf\Merger as PdfMerger;
use setasign\Fpdi\Tcpdf\Fpdi as FpdiTcpdf;

class InvoiceServiceImpl
{
    public static function getPDFMerger(string $title, string $subject, array $keywords): PdfMerger
    {
        $tcpdi = new FpdiTcpdf();
        return new PdfMerger(
            new Fpdi2Driver(
                $tcpdi
            )
        );
    }
}

composer.json

        "tecnickcom/tcpdf": "^6.4.4",
        "setasign/fpdi": "^2.3",
        "iio/libmergepdf": "^4.0",
        
@JanSlabon
Copy link
Member

The merger class you use creates a kind of FPDF-alias for TCPDF here (we did simliar things back in version 1 of FPDI depending on what main class was available).

The setasign\Fpdi\FpdfTplTrait is normally not used in the class chain when FPDI is used with TCPDF but as our class FpdfTpl extends FPDF (see here) the trait is injected into the wrong class chain though this class is never used in your code. So everything related to the TCPDI class should be renamed or moved to an own namespace to avoid this annoying conflict.

The issue was already raised some month ago here without any reaction.

The easiest solution to that problem would be to simply ignore the PdfMerger class completely and write your own simple helper class or function as we demoed here.

@guianzollin
Copy link

guianzollin commented Aug 15, 2022

Try something this:

composer.json

"iio/libmergepdf": "^4.0",
"tecnickcom/tcpdf": "6.3.*",
"setasign/fpdi": "^2.0"

Using Fpdi to sign a pdf

use setasign\Fpdi\Tcpdf\Fpdi;

...

$pdf = new Fpdi('P', 'mm', 'A4');
$pages = $pdf->setSourceFile('file.pdf');
$certificate = file_get_contents('certificate.crt');

for ($i = 1; $i <= $pages; $i++) {
    $pdf->AddPage();
    $page = $pdf->importPage($i);
    $pdf->useTemplate($page, 0, 0);
    $pdf->setSignature($certificate, $certificate, 'tcpdfdemo', '', 2, array());
}

$pdf->Output($path, 'F');

Using libmergepdf to merge two pdfs

use iio\libmergepdf\Merger;
use iio\libmergepdf\Driver\TcpdiDriver;

...

$merger = new Merger(new TcpdiDriver);
$merger->addFile('file1.pdf');
$merger->addFile('file2.pdf');
$mergedPdf = $merger->merge();

return $mergedPdf;

@JanSlabon
Copy link
Member

So, can this be closed?

@williamdes
Copy link
Author

So, can this be closed?

I am not sure, until my snippet produces still an error

@JanSlabon
Copy link
Member

The issue is the PdfMerger class which is not from us (I described it in my previous reply with all details). It need to be fixed there.

@williamdes
Copy link
Author

The issue is the PdfMerger class which is not from us (I described it in my previous reply with all details). It need to be fixed there.

Thank you, it's clear now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants