Extract signature from PDF using PHP

Let's say I have a digitally signed PDF document (with a completed form). I need to find a way to check this PDF file using PHP.

I know that I need to use the openssl_verify($data, $signature, $pub_key_id) function openssl_verify($data, $signature, $pub_key_id) . I need 3 parameters (I have an open certificate).

How to extract signed data and signature from a PDF file?

+4
source share
2 answers

You will need to parse the PDF - theoretically you can write your own parsing functions. However, you would save a lot of time using something ready-made.

What about Apache PDFBox ? You can execute it like any other shell script, or use the PHP / Java bridge .

If you decide to go manually, see the PDF specification for implementing your parser - you should simply ignore any complex parts and catch the signature by looking for startxref.

There are also proprietary solutions such as VersyPDF.PHP , but this may be a little redundant for your work (but on the plus side, you can get away with using the evaluation version as you don't care about the watermarked edition).

0
source

To extract the data and signature, you can use the numbers in the / ByteRange field in PDF format. It has 4 numbers like / ByteRange [abcd]. The signature is from b (th) bytes to c (th) bytes, and the rest is data. In PHP, you can use the stream_get_contents () function to get content by stream.

0
source

All Articles