Import a PDF and sign it with Symfony

2019-06-11 11:27发布

I need to sign an existing pdf.

I'm using Symfony 3.4.12 and the bundle https://packagist.org/packages/whiteoctober/tcpdf-bundle to sign pdf's.

Inside the services I added this part:

AppBundle\Controller\AController:
    class: AppBundle\Controller\AController
    arguments: ['@white_october.tcpdf']

Inside the Controller I'm using this;

use WhiteOctober\TCPDFBundle\Controller\TCPDFController;

And with this code I can download a created pdf signed correctly:

private $tcpdf;

public function __construct(TCPDFController $tcpdf)
{
    $this->tcpdf = $tcpdf;
}

public function aAction ()
{
    $pdf = $this->tcpdf->create(
        'LANDSCAPE',
        PDF_UNIT,
        PDF_PAGE_FORMAT,
        true,
        'UTF-8',
        false
    );
    $pdf->SetAuthor('qweqwe');
    $pdf->SetTitle('Prueba TCPDF');
    $pdf->SetSubject('Your client');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    $pdf->setFontSubsetting(true);

    // set additional information
    $info = array(
        'Name' => 'xxx',
        'Location' => 'xxx',
        'Reason' => 'xxx xxxx',
        'ContactInfo' => 'http://www.xxx.xx',
    );

    // set document signature
    $pdf->setSignature('file:///var/www/html/publicCert.pem', 'file:///var/www/html/privateKey_cert.pem', 'xxxx', '', 2, $info, 'A');

    $pdf->SetFont('helvetica', '', 11, '', true);
    $pdf->AddPage();

    $html = '<h1>Working on Symfony</h1>';

    $pdf->writeHTMLCell(
        $w = 0,
        $h = 0,
        $x = '',
        $y = '',
        $html,
        $border = 0,
        $ln = 1,
        $fill = 0,
        $reseth = true,
        $align = '',
        $autopadding = true
    );

    $pdf->Output("example.pdf", 'I');
}

My objective is try to sign an existing pdf, so I tried to use FPDI to import it but I get confused because I know I can import a pdf with PDFI but I can't sign it and the opposite with the TCPDF, I can't import the pdf but I can sign a created one. And apparently I can't use functions from one to the other.

So, how I should fix this problem? Any idea? Could you show me an example?

1条回答
男人必须洒脱
2楼-- · 2019-06-11 12:08

Finally I couldn't solve this problem with the TCPDF bundle but I found a not too much expensive solution throw SetAsign where in this example you can see that you can select the pdf you want to sign it. I know it cost a price but FPDI and this libraries I could see that got some problems with the newest versions from the pdfs and Setasign is a more stable and maintained version compared with FPDF pluggins and will avoid a few problems for you.

The example and solution I found is explained in the next link:

"https://manuals.setasign.com/setapdf-signer-manual/the-main-class/#index-4-1"

查看更多
登录 后发表回答