Digitally sign PDF using PHP, Zend and openssl

I am trying to create a simple procedure for signing PDF documents using PHP, openssl and the Zend framework (for unpacking / processing pdf).

I found this one , but it just won’t work, Zend cannot open any PDF files, even Zend pdf tests, and Zend will not tell you why, only that it “cannot”.

I am sure that I can efficiently create keys / certificates, since this is well documented, but is there a strong approach to attaching the generated certificate to a PDF file, since the above Zend extension offers this sometime?

function DigiSignPDF ($pdf_sign_request) { if (get_magic_quotes_gpc()) { $new_pdf = stripslashes($pdf_sign_request['raw_pdf']); } else { $new_pdf = $pdf_sign_request['raw_pdf']; } $test_pdf = stripslashes(file_get_contents('path/Some.pdf')); $test_pdf2 = ('path/Some.pdf'); $pdf = Zend_Pdf::load($new_pdf2); //below is the signing code, from another library, it works as long as Zend_Pdf works $certificate = file_get_contents('path/certificate.p12'); $certificatePassword = 'test123'; if (empty($certificate)) { throw new Zend_Pdf_Exception('Cannot retrieve/generate the certificate.'); } $pdf->attachDigitalCertificate($certificate,$certificatePassword); $eSig_pdf = $pdf->render(); file_put_contents('path/signed_pdf.pdf', $eSig_pdf); } 

Editing, adding code: the above only works if I use 'test_pdf2' as the input for Zend_Pdf. It recognizes the certificate as binary, no problem, but I need to be able to transfer the PDF without writing it to disk.

+8
php pdf openssl zend-framework sign
source share
2 answers

TCPDF supports signing PDF files. You may have found something useful in the source code.

+8
source share

Adding my solution as an answer for each request to halfer: this one solved, because I was passing the contents of Zend_Pdf as a string, I had to use Zend_Pdf :: parse ($ new_pdf); because it most likely says in the manual. (Oh)

Further; I solved almost all my problems with digitally signing PDF files of various versions and form components by switching to TCPDF, as some of the articles suggest. A similar caution has been met by TCPDF, but when using strings make sure that you use TCPDF 'writeHTMLCell' instead of 'writeHTML'. And watch out for the magic_quotes PHPs, weird spaces, coding and goblins.

0
source share

All Articles