dealfonso / sapp

Simple and Agnostic PDF Document Parser in PHP - sign PDF docs using PHP
GNU Lesser General Public License v3.0
121 stars 32 forks source link

Add signature options feature #67

Closed aphoe closed 7 months ago

aphoe commented 7 months ago

Developers can now add signature options (e.g., Name, Reason, Location, and ContactInfo) to a document's digital signature.

How it works

$certificate = [
  'cert' => $certificate,
  'pkey' => $private_key,
];

$content = file_get_contents('path/to/file.pdf');
$obj = PDFDoc::from_string($content);

$obj->set_signature_certificate($certificate);
$obj->set_signature_options($name, $reason, $location, $contactInfo); // Use this method to set the options

$signed = $obj->to_pdf_file_s();

file_put_contents('path/to/save/signed/file.pdf', $signed);
dealfonso commented 7 months ago

Hi, while your solution is working, there was already a function set_metadata in object PDFSignatureObject whose aim is to address this problem. The body of set_signature_options function should be moved to PDFSignatureObject (because the PDFValueString objects are missing) and so there is no need for modifying the constructor of PDFSignatureObject.

The reason to set the fields in the PDFSignatureObject is that this object encapsulates the appropriate key values, and avoids coupling the code from the PDFDoc object.

aphoe commented 7 months ago

@dealfonso Hi. The first thing I tried was using that option, but it didn't work. This is quite surprising because the set_metadata() method updated the $_value property of the PDFDoc class.

I understand the coupling part and agree. However, I tried many options, and this is the only one that works.

dealfonso commented 7 months ago

The problem is that set_metadata does not convert to PDFValueString (my fault).

aphoe commented 7 months ago

Oh! I see!

I'll work on that and create a PR if I have some time over the weekend.

Thank you for the help.