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

use setasign\SetaPDF2\Core\Document;
use setasign\SetaPDF2\Core\Font\TrueType\Subset as TrueTypeSubset;
use setasign\SetaPDF2\Core\Writer\HttpWriter;
use setasign\SetaPDF2\Signer\PemHelper;
use setasign\SetaPDF2\Signer\Signature\Appearance\Dynamic as DynamicAppearance;
use setasign\SetaPDF2\Signer\Signature\Module\Cms as CmsModule;
use setasign\SetaPDF2\Signer\SignatureField;
use setasign\SetaPDF2\Signer\Signer;

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

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

// create a signer instance for the document
$signer = new 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
    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 a CMS module instance
$module = new CmsModule();
// 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(PemHelper::extractFromFile('intermediate-certificate.pem'));

// create a Signature appearance
$visibleAppearance = new DynamicAppearance($module);
// create a font instance for the signature appearance
$font = new TrueTypeSubset($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 = 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 = 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.