我目前正在致力于使用 TCPDF 在 PHP 中实现数字签名,以签署和导出 PDF 格式的文档。但是,当我尝试在 Adobe 等平台上验证签名的 PDF 时,遇到错误,指出数字签名无效。
我正在尝试使用此代码,但生成的 pdf 未经过验证。
<?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.";
}
为我工作:
$privateKeyPassword = env('DEVELOPMENT_PRIVATE_KEY_PASSWORD');
$certificatePath = 'file://' . realpath(Storage::path('certs/certificate.crt'));
$privateKeyPath = 'file://' . realpath(Storage::path('certs/private.key'));
$pdf->setSignature($certificatePath, $privateKeyPath, $privateKeyPassword, '', 2, $info);
Laravel 版本:11.22.0
setasign/fpdi 版本:2.6
PHP版本:8.2