PHP Digital Signature

Asked

Viewed 3,835 times

3

I am trying to perform the digital signature of a PHP document through A1 certificate.

$pdf = new \FPDI();

$pdf_path = $_SERVER["DOCUMENT_ROOT"] . "\\files\\" . md5(uniqid("")) . ".pdf";
$path_assinado = $_SERVER["DOCUMENT_ROOT"] . "\\files\\" . md5(uniqid("")) . ".pdf";

file_put_contents($pdf_path, $pdfDocument);

$pageCount = $pdf->setSourceFile($pdf_path);
for ($i = 1; $i <= $pageCount; $i++) {
  $pdf->AddPage();
  $page = $pdf->importPage($i);
  $pdf->useTemplate($page, 0, 0);
}

$info = [
  'Name'        => $certificado->getCompanyName(),
  'Date'    => date("Y.m.d H:i:s"),
  'Reason'      => 'Assinatura',
  'ContactInfo' => 'contact',
];
$pdf->setSignature($certificado->__toString(), $certificado->privateKey, $senha, '', 1, $info);

$pdf->Output($path_assinado, "F");

return file_get_contents($path_assinado);

I am able to perform the signature normally, but I need to send the signed file to TJRJ, and the signature that TCPDF is performing, completely blocks the PDF, making it impossible to edit.

When I upload the file, I get the following return:

It is necessary that an informed document may be amended.

I tried several ways to solve the problem alone and so far I could not, someone can give some help?

PS. I have tried to change the setSignature value to 2.3, but the file remains locked.

1 answer

3

I managed to solve the problem, solution:

private function assinarPdf($pdfDocument, Certificate $certificado, $senha) {
    $pdf = new \FPDI();

    $pdf_path = $_SERVER["DOCUMENT_ROOT"] . "\\files\\" . md5(uniqid("")) . ".pdf";
    $path_assinado = $_SERVER["DOCUMENT_ROOT"] . "\\files\\" . md5(uniqid("")) . ".pdf";

    file_put_contents($pdf_path, $pdfDocument);

    $pageCount = $pdf->setSourceFile($pdf_path);
    for ($i = 1; $i <= $pageCount; $i++) {
        $pdf->AddPage();
        $page = $pdf->importPage($i);
        $pdf->useTemplate($page, 0, 0);
    }

    $info = [
        'Name'        => $certificado->getCompanyName(),
        'Date'        => date("Y.m.d H:i:s"),
        'Reason'      => 'Assinatura',
        'ContactInfo' => 'contact',
    ];

    $pdf->setSignature($certificado->__toString(), $certificado->privateKey, $senha, '', 2, $info, "A");
    $pdf->Output($path_assinado, "F");

    return file_get_contents($path_assinado);
}

When adding the Approval field with the "A" string, the signature will be done as needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.