Need Assistance with Digital Signature in PHP using TCPDF

78 Views Asked by At

I am currently working on implementing digital signatures in PHP using TCPDF to sign and export documents in PDF format. However, when I attempt to validate the signed PDF on platforms like Adobe, I encounter an error stating that the digital signature is not valid.

I am trying to with this code but generated pdf not verified.

<?php

require_once "vendor/autoload.php";

use setasign\Fpdi\Tcpdf\Fpdi;

// HTML content to be signed
$htmlContent =
    "<h1>Hello World!</h1><p>This is a digitally signed document.</p>";

//  private key and certificate
$privateKey =
    "/home/newkabir/public_html/docuSign/certificates/ankitpanwar.pkey";
$certificate =
    "/home/newkabir/public_html/docuSign/certificates/ankitpanwar.cer";

// Create a signature
$signature = null;

openssl_pkcs7_sign(
    null,
    null,
    $htmlContent,
    [$certificate, $privateKey],
    $signature,
    PKCS7_BINARY | PKCS7_DETACHED
);

echo "signature is :- " . $signature . "</br>";
// Sign and export HTML content to PDF
signAndExportToPDF($htmlContent . $signature, $privateKey, $certificate);

// Function to sign and export the HTML content to a PDF
function signAndExportToPDF($htmlContent, $privateKey, $certificate)
{
    $pdf = new Fpdi();
    $pdf->AddPage();

    // Add HTML content
    $pdf->writeHTML($htmlContent);

    // Add a digital signature field
    $pdf->setSignature($privateKey, $certificate, "", "", 2, [
        "Name" => "ankit panwar",
        "Location" => "ajmer",
        "Reason" => "for demo purpose",
    ]);

    // Output the signed PDF
    $pdf->Output(
        "/home/newkabir/public_html/docuSign/signed_documentnewrsldkj.pdf",
        "F"
    );

    echo "PDF signed and exported successfully.";
}


0

There are 0 best solutions below