I want to add digital signature into pdf document, with help of php. I have the .pfx digital signature format with me.

Dani AI

Generated

If you already have a .pfx, you can feed its cert and private key to TCPDF without converting on disk. Parse the PFX in PHP, then call setSignature() before you add content, and setSignatureAppearance() on the page where you want the visible box.

<?php
$pkcs12 = file_get_contents(__DIR__ . '/signing-cert.pfx');
if (!openssl_pkcs12_read($pkcs12, $creds, 'pfx-password')) {
    throw new RuntimeException('Cannot read PFX or wrong password.');
}
$certificate = $creds['cert'];   // PEM-encoded certificate (optionally include chain)
$privateKey  = $creds['pkey'];   // PEM-encoded private key

$pdf = new TCPDF();
$info = ['Name'=>'Acme CA','Reason'=>'Approval','Location'=>'NY','ContactInfo'=>'it@example.com'];
$pdf->setSignature($certificate, $privateKey, '', '', 2, $info); // 2 = allow form fill/sign only
$pdf->AddPage();
$pdf->writeHTML('<p>Signed PDF content...</p>');
$pdf->setSignatureAppearance(150, 240, 40, 15); // x,y,w,h on current page
$pdf->Output(__DIR__ . '/signed.pdf', 'F');

Common gotchas I see when folks hit TCPDF "issues" (agree with that details matter):

  • Do not modify or re-write the file after Output(). Any byte change breaks the signature. Use the right cert_type (1, 2, or 3) if you need certified restrictions. See TCPDF docs and example 052 for the exact parameter order and usage. TCPDF example 052 and TCPDF setSignature docs.
  • If openssl_pkcs12_read() fails on newer PHP (OpenSSL 3), your PFX may use legacy ciphers. Either repack it with modern algorithms or enable the legacy provider in OpenSSL. See the PHP manual notes for openssl_pkcs12_read(). PHP manual: openssl_pkcs12_read.
  • Need advanced/PAdES features or TSA/OCSP/LTV and TCPDF is fighting you? Consider a purpose-built signer like SetaPDF-Signer (commercial), which handles chains, timestamps, and visible signatures cleanly. SetaPDF-Signer manual.

if you can share the exact error from TCPDF or OpenSSL, we can pinpoint which of the above is biting you.

Recommended Answers

All 3 Replies

I searched in google, all the results are pointing to TCPDF, I tried with using this library, but it has some issues to sign the pdf.

If you don't share the issues or report a bug to the keepers of TCPDF they won't respoind or fix it. Not to leave you without recourse I am running into folk that don't understand how things work. That is, if you find a bug, you have to report the bug. If you don't then you accept that the bug is OK.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.