Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to update existing signature box (instead of always creating a new one) #89

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 55 additions & 11 deletions src/PDFDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class PDFDoc extends Buffer {
protected $_certificate = null;
protected $_signature_ltv_data = null;
protected $_signature_tsa = null;
protected $_signature_annotation_object_id = null;
protected $_appearance = null;
protected $_xref_table_version;
protected $_revisions;
Expand Down Expand Up @@ -258,6 +259,40 @@ public function get_object($oid, $original_version = false) {
return $object;
}

/**
* Function that sets the signature annotation object that will be used to sign the document. If this is not set,
* there will be a new signature annotation object created.
* @param int $oid The object ID of the signature annotation object
* @return bool True if the object ID is a signature annotation object, false otherwise
*/
public function set_signature_annotation_object_id(int $oid) {
$pdfObject = $this->get_object($oid);
if (false === $pdfObject) {
return p_error('Signature box with ID: '.$oid.' not found');
}

$value = $pdfObject->get_value()->val();

if (!is_array($value)) {
return p_error('Object ID: '.$oid.' does not have a value');
}

if (!isset($value['Type']) || 'Annot' !== $value['Type']->val()) {
return p_error('Object ID: '.$oid.' has invalid type value');
}

if (!isset($value['Subtype']) || 'Widget' !== $value['Subtype']->val()) {
return p_error('Object ID: '.$oid.' has invalid subtype value');
}

if (!isset($value['FT']) || 'Sig' !== $value['FT']->val()) {
return p_error('Object ID: '.$oid.' has invalid FT value');
}

$this->_signature_annotation_object_id = $oid;
return true;
}

/**
* Function that sets the appearance of the signature (if the document is to be signed). At this time, it is possible to set
* the page in which the signature will appear, the rectangle, and an image that will be shown in the signature form.
Expand Down Expand Up @@ -443,17 +478,21 @@ protected function _generate_signature_in_document() {
}

// Create the annotation object, annotate the offset and append the object
$annotation_object = $this->create_object([
"Type" => "/Annot",
"Subtype" => "/Widget",
"FT" => "/Sig",
"V" => new PDFValueString(""),
"T" => new PDFValueString('Signature' . get_random_string()),
"P" => new PDFValueReference($page_obj->get_oid()),
"Rect" => $recttoappear,
"F" => 132 // TODO: check this value
]
);
if (null === $this->_signature_annotation_object_id) {
$annotation_object = $this->create_object([
"Type" => "/Annot",
"Subtype" => "/Widget",
"FT" => "/Sig",
"V" => new PDFValueString(""),
"T" => new PDFValueString('Signature' . get_random_string()),
"P" => new PDFValueReference($page_obj->get_oid()),
"Rect" => $recttoappear,
"F" => 132 // TODO: check this value
]
);
} else {
$annotation_object = $this->get_object($this->_signature_annotation_object_id);
}

// Prepare the signature object (we need references to it)
$signature = null;
Expand Down Expand Up @@ -562,6 +601,11 @@ protected function _generate_signature_in_document() {
$annotation_object["Rect"] = [ $recttoappear[0], $pagesize_h - $recttoappear[1], $recttoappear[2], $pagesize_h - $recttoappear[3] ];
}

if (null !== $this->_signature_annotation_object_id) {
$this->add_object($annotation_object);
return $signature;
}

if (!$newannots->push(new PDFValueReference($annotation_object->get_oid())))
return p_error("Could not update the page where the signature has to appear");

Expand Down