IronRabbit
IronRabbit

Reputation: 185

Import and sign a pdf in php

I have a pdf that i want to sign, using a certificate, with a php script. I decompose the action in 2 steps :

I can resolve the first part... I'm using TCPDI (https://github.com/pauln/tcpdi) and TCPDF.

Here's my script :

<?php require_once('tcpdf/config/tcpdf_config.php');
require_once('tcpdf/tcpdf.php');
require_once('tcpdi.php');
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->AddPage();
$pdf->setSourceFile('file.pdf');

$pdf->Output('output.pdf', 'I')
?>

I don't have any errors, but the output is blank, with a little black line on the header.

Do you know what's the problem ? I also tried with FPDF/FPDI.

Thanks.

Regards.

EDIT 25/11/2014: Here's my solution, using TCPDF :

$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdfdata = file_get_contents('/Users/ynp/Downloads/billofsale.pdf'); 
$pagecount = $pdf->setSourceData($pdfdata); 
for ($i = 1; $i <= $pagecount; $i++) { 
    $tplidx = $pdf->importPage($i);
    $pdf->AddPage();
    $pdf->useTemplate($tplidx); 
}

$pdf->Output();

You'll probably have some errors in fpdf_tpl.php, just change the prototype of the concerning functions.

Upvotes: 2

Views: 2463

Answers (1)

Jan Slabon
Jan Slabon

Reputation: 5058

You actually only define the soure file but you do not import any page of it. Try this:

$pageCount = $pdf->setSourceFile('file.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    $tplIdx = $pdf->importPage($pageNo);
    $pdf->AddPage();
    $pdf->useTemplate($tplIdx, null, null, 0, 0, true);
}

This way you import all existing pages into a structure which you can reuse with FPDI. The resulting document has a complete new internal structure and you may also lose content as described here.

If you need to sign the original document you may check out the SetaPDF-Signer component (not free!).

Upvotes: 2

Related Questions