Create digital signatures for PDF documents with PHP

SetaPDF-Signer

Digital sign PDF documents with PHP

Certified Document Demo

This demo shows you how to add a certification signature to an existing PDF document.

PHP
<?php

use setasign\SetaPDF2\Core\Document;
use setasign\SetaPDF2\Core\Writer\HttpWriter;
use setasign\SetaPDF2\Signer\PemHelper;
use setasign\SetaPDF2\Signer\Signature\Module\Cms as CmsModule;
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('certified.pdf', false);
// load document by filename
$document = Document::loadByFilename('Laboratory-Report.pdf', $writer);

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

// set some signature properties
$signer->setReason('This document is fixed');
$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([file_get_contents('private-key.pem'), 'password']);
// pass the intermediate certificate(s)
$module->setExtraCertificates(PemHelper::extractFromFile('intermediate-certificate.pem'));

// set the certification level: don't allow any changes
$signer->setCertificationLevel(Signer::CERTIFICATION_LEVEL_NO_CHANGES_ALLOWED);

// sign/certify 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.