Create digital signatures for PDF documents with PHP

SetaPDF-Signer

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:

PHP
<?php
require_once('library/SetaPDF/Autoload.php');
// 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', false);
// 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
$signer->addSignatureField(
    'Signature',                    // Name of the signature field
    1,                              // put appearance on page 1
    \SetaPDF_Signer_SignatureField::POSITION_LEFT_TOP,
    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->setLocation('setasign.com');
$signer->setContactInfo('+49 5351 523901-0');

// create an OpenSSL module instance
$module = new \SetaPDF_Signer_Signature_Module_Cms();
// set the sign certificate
$module->setCertificate(file_get_contents('certificate.pem'));
// set the private key for the sign certificate
$module->setPrivateKey(array(file_get_contents('private-key.pem'), 'password'));
// pass the intermediate certificate(s)
$module->setExtraCertificates(\SetaPDF_Signer_Pem::extractFromFile('intermediate-certificate.pem'));

// create a Signature appearance
$visibleAppearance = new \SetaPDF_Signer_Signature_Appearance_Dynamic($module);
// create a font instance for the signature appearance
$font = new \SetaPDF_Core_Font_TrueType_Subset(
    $document,
    'fonts/DejaVuSans.ttf'
);
// set the font
$visibleAppearance->setFont($font);
// 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
$visibleAppearance->setGraphic($signatureXObject);

// define the appearance
$signer->setAppearance($visibleAppearance);

// sign the document
$signer->sign($module);

We use certificates from GlobalSign for demonstration. If you want to validate the signature in another viewer application, make sure that the root certificate is added as a trusted identity.