Digital Signature PHP

2

I'm trying to digitally sign a document in PHP 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 doing, completely blocks the PDF, making it impossible to edit.

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

  

It is necessary that informed document is possible to change.

I have tried several ways to solve the problem myself and so far I have not been able to, can anyone help?

PS. I have tried to change the value of setSignature to 2, 3, but the file is still locked.

    
asked by anonymous 23.10.2017 / 15:23

1 answer

1

I was able 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);
}

By adding the Approval field with the string "A", the signature will be performed as required.

    
23.10.2017 / 18:08