Skip to content

Commit

Permalink
Added support for usage with the dynamic appearance module.
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSlabon committed Jun 28, 2022
1 parent 9edcd2a commit 585cb1c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 5 deletions.
88 changes: 88 additions & 0 deletions examples/appearance-demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\CurlHandler;
use Http\Factory\Guzzle\RequestFactory;
use Http\Factory\Guzzle\StreamFactory;
use Mjelamanov\GuzzlePsr18\Client as Psr18Wrapper;
use setasign\SetaPDF\Signer\Module\GlobalTrustTrust2Go\Client;
use setasign\SetaPDF\Signer\Module\GlobalTrustTrust2Go\Module;

date_default_timezone_set('Europe/Berlin');
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', '1');

require_once __DIR__ . '/../vendor/autoload.php';

if (!file_exists(__DIR__ . '/settings.php')) {
throw new RuntimeException('Missing settings.php!');
}
$settings = require __DIR__ . '/settings.php';

$file = __DIR__ . '/files/Laboratory-Report.pdf';
$certificateSerialNumber = $settings['certificateSerialNumber'];
// A requestID generated by the client to identify this signature operation (6 alphanumeric characters)
$requestId = '123456';

$caBundle = realpath(__DIR__ . '/files/globaltrust-all.pem');

$httpClient = new GuzzleClient([
'handler' => new CurlHandler(),
// note: guzzle requires this parameter to fully support PSR-18
'http_errors' => false,
'verify' => $caBundle,
// timeout by api after ~300 seconds
'timeout' => 360,
]);
// only required if you are using guzzle < 7
$httpClient = new Psr18Wrapper($httpClient);
$requestFactory = new RequestFactory();
$streamFactory = new StreamFactory();

$client = new Client(
$httpClient,
$requestFactory,
$streamFactory,
$settings['apiUrl'],
$settings['username'],
$settings['activationPin']
);
//var_dump($client->getCertificates());die();

$module = new Module($client, $requestId, $certificateSerialNumber);
$module->setDigest(SetaPDF_Signer_Digest::SHA_512);

$certificate = $client->getCertificateBySerialNumber($certificateSerialNumber);
$module->setCertificate($certificate['certificate']);
$module->setExtraCertificates($certificate['chain']);

$reader = new SetaPDF_Core_Reader_File($file);
$writer = new SetaPDF_Core_Writer_File(__DIR__ . '/signed.pdf');
// let's get the document
$document = SetaPDF_Core_Document::load($reader, $writer);

// now let's create a signer instance
$signer = new SetaPDF_Signer($document);
$signer->setAllowSignatureContentLengthChange(false);
//$signer->setSignatureContentLength(36000);

//// set some signature properties
$signer->setReason('Testing TRUST2GO');

$field = $signer->addSignatureField(
'Signature',
1,
SetaPDF_Signer_SignatureField::POSITION_RIGHT_TOP,
['x' => -160, 'y' => -100],
180,
60
);

$signer->setSignatureFieldName($field->getQualifiedName());

$appearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($module);
$signer->setAppearance($appearance);

$signer->sign($module);
14 changes: 13 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,25 @@ public function getCertificates(bool $activeonly = true, bool $useronly = false)
return \json_decode($responseBody, true);
}

/**
* @param string $certificateSerialNumber
* @return array
* @throws ClientExceptionInterface
* @deprecated Use getCertificateBySerialNumber() instead.
* @see getCertificateBySerialNumber()
*/
public function getCertificatesBySerialNumber(string $certificateSerialNumber): array
{
return $this->getCertificateBySerialNumber($certificateSerialNumber);
}

/**
* @param string $certificateSerialNumber
* @return array{certificate: string, chain: array<string>}
* @throws ClientExceptionInterface
* @throws Exception
*/
public function getCertificatesBySerialNumber(string $certificateSerialNumber): array
public function getCertificateBySerialNumber(string $certificateSerialNumber): array
{
$certificates = $this->getCertificates();
// search for the correct certificate chain
Expand Down
16 changes: 12 additions & 4 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ public function setCertificate($certificate)
$this->padesModule->setCertificate($certificate);
}

/**
* @return SetaPDF_Signer_X509_Certificate|string
*/
public function getCertificate()
{
return $this->padesModule->getCertificate();
}

/**
* Add additional certificates which are placed into the CMS structure.
*
Expand Down Expand Up @@ -128,10 +136,10 @@ public function addCrl($crl)
*/
public function createSignature(SetaPDF_Core_Reader_FilePath $tmpPath): string
{
if ($this->padesModule->getCertificate() === null) {
$certificates = $this->client->getCertificatesBySerialNumber($this->certificateSerialNumber);
$this->padesModule->setCertificate($certificates['certificate']);
$this->padesModule->setExtraCertificates($certificates['chain']);
if ($this->getCertificate() === null) {
$certificate = $this->client->getCertificateBySerialNumber($this->certificateSerialNumber);
$this->setCertificate($certificate['certificate']);
$this->setExtraCertificates($certificate['chain']);
}

$digest = $this->padesModule->getDigest();
Expand Down

0 comments on commit 585cb1c

Please sign in to comment.