Create digital signatures for PDF documents with PHP


Digital sign PDF documents with PHP

Sign With a Visible Signature

This demo shows you how to create a visible signature. Since version 2 the Signer component is shipped with an appearance class that is able to create the appearance in various ways:

// or if you use composer require_once('vendor/autoload.php');

// create a Http writer
$writer = new SetaPDF_Core_Writer_Http('signed-with-visible-signature.pdf', true);
// load document by filename
$document = SetaPDF_Core_Document::loadByFilename('Laboratory-Report.pdf', $writer);

// create a signer instance for the document
$signer = new SetaPDF_Signer($document);

// add a field with the name "Signature" to the top left of page 1
    'Signature',                    // Name of the signature field
    1,                              // put appearance on page 1
    array('x' => 50, 'y' => -80),   // Translate the position (x 50, y -80 -> 50 points right, 80 points down)
    180,                            // Width - 180 points
    50                              // Height - 50 points

// set some signature properties
$signer->setReason("Just for testing");
$signer->setContactInfo('+49 5351 3803603');

// create an OpenSSL module instance
$module = new SetaPDF_Signer_Signature_Module_OpenSsl();
// set the sign certificate
// set the private key for the sign certificate
$module->setPrivateKey(array(file_get_contents('private-key.pem'), 'password'));

// create a Signature appearance
$visibleAppearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($module);
// choose a document to get the background from and convert the art box to an xObject
$backgroundDocument = SetaPDF_Core_Document::loadByFilename('images/SetaPDF-Signer-Logo.pdf');
$backgroundXObject = $backgroundDocument->getCatalog()->getPages()->getPage(1)->toXObject($document);
// set the background with 50% opacity
$visibleAppearance->setBackgroundLogo($backgroundXObject, .5);

// choose a document with a handwritten signature
$signatureDocument = SetaPDF_Core_Document::loadByFilename('images/Signature-SetaPDF-Demo.pdf');
$signatureXObject = $signatureDocument->getCatalog()->getPages()->getPage(1)->toXObject($document);
// set the signature xObject as graphic

// define the appearance

// sign the document