-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
491 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Ramsey\Uuid\Uuid; | ||
use setasign\SetaPDF\Signer\Module\GlobalTrustTrust2Go\Batch; | ||
use setasign\SetaPDF\Signer\Module\GlobalTrustTrust2Go\Client; | ||
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; | ||
|
||
ini_set('display_errors', '1'); | ||
ini_set('display_startup_errors', '1'); | ||
error_reporting(E_ALL); | ||
|
||
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__ . '/assets/tektown/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__ . '/assets/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'] | ||
); | ||
|
||
$batch = new Batch($client, $requestId, $certificateSerialNumber); | ||
$batch->getTrustedCertificates() | ||
->add(SetaPDF_Signer_Pem::extractFromFile($caBundle)); | ||
|
||
// create a re-usable array of filenames (in/out) | ||
$files = [ | ||
[ | ||
'in' => new SetaPDF_Core_Reader_File('assets/tektown/Laboratory-Report.pdf'), | ||
'out' => 'output/tektown-signed.pdf' | ||
], | ||
[ | ||
'in' => new SetaPDF_Core_Reader_String(file_get_contents('assets/lenstown/Laboratory-Report.pdf')), | ||
'out' => 'output/lenstown-signed.pdf' | ||
], | ||
[ | ||
'in' => 'assets/etown/Laboratory-Report.pdf', | ||
'out' => 'output/etown-signed.pdf' | ||
], | ||
[ | ||
'in' => 'assets/camtown/Laboratory-Report.pdf', | ||
'out' => 'output/camtown-signed.pdf' | ||
], | ||
]; | ||
|
||
/* This callback has to create/get and return the field instance that should be used for the signature. | ||
* It is also possible to add signature properties or create a field appearance in this callback. | ||
*/ | ||
$callback = static function( | ||
$key, | ||
array $file, | ||
SetaPDF_Signer $signer, | ||
SetaPDF_Signer_Signature_Module_Pades $padesModule, | ||
SetaPDF_Core_Document $document | ||
): SetaPDF_Signer_SignatureField { | ||
// set some signature properties | ||
$signer->setReason('Signature for document ' . basename($file['out'])); | ||
$signer->setLocation('Test Environment'); | ||
|
||
$appearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($padesModule); | ||
// // let's create a font instance to not use standard fonts (not embedded) | ||
// $font = new SetaPDF_Core_Font_Type0_Subset( | ||
// $document, | ||
// 'assets/fonts/DejaVu/ttf/DejaVuSans.ttf' | ||
// ); | ||
// // and pass it to the appearance module | ||
// $appearance->setFont($font); | ||
$signer->setAppearance($appearance); | ||
|
||
return $signer->addSignatureField( | ||
SetaPDF_Signer_SignatureField::DEFAULT_FIELD_NAME, | ||
1, | ||
SetaPDF_Signer_SignatureField::POSITION_LEFT_TOP, | ||
[ | ||
'x' => 20, | ||
'y' => -20 | ||
], | ||
200, | ||
50 | ||
); | ||
}; | ||
|
||
/* If you need control over the temporary file which needs to be created during the signature process, you can | ||
* create another callback, that has to return a writer instance for this file. | ||
* NOTE: You need to clean up these files on your own! | ||
*/ | ||
//$tempFileCallback = static function($key, $file): SetaPDF_Core_Writer_FileInterface { | ||
// return new SetaPDF_Core_Writer_File('output/tmp-' . $key); | ||
//}; | ||
|
||
// If you want to add timestamps to the signautre you can pass an appropriate module like this: | ||
if ($settings['tsUrl']) { | ||
$timestampModule = new SetaPDF_Signer_Timestamp_Module_Rfc3161_Curl($settings['tsUrl']); | ||
$timestampModule->setCurlOption(CURLOPT_USERPWD, $settings['tsUsername'] . ':' . $settings['tsPassword']); | ||
$timestampModule->setCurlOption(CURLOPT_CAINFO, $caBundle); | ||
$batch->setTimestampModule($timestampModule); | ||
} | ||
|
||
try { | ||
$batch->sign($files, true, $callback/*, $tempFileCallback*/); | ||
|
||
} catch (SetaPDF_Signer_ValidationRelatedInfo_Exception $e) { | ||
// If VRI (validation related information) cannot be resolved, let's check the logs: | ||
foreach ($batch->getVriLoggers() as $key => $logger) { | ||
foreach ($logger->getLogs() as $log) { | ||
echo str_repeat(' ', $log->getDepth() * 4); | ||
echo $log->getMessage() . "\n"; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Ramsey\Uuid\Uuid; | ||
use setasign\SetaPDF\Signer\Module\GlobalTrustTrust2Go\Batch; | ||
use setasign\SetaPDF\Signer\Module\GlobalTrustTrust2Go\Client; | ||
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; | ||
|
||
ini_set('display_errors', '1'); | ||
ini_set('display_startup_errors', '1'); | ||
error_reporting(E_ALL); | ||
|
||
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__ . '/assets/tektown/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__ . '/assets/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'] | ||
); | ||
|
||
$batch = new Batch($client, $requestId, $certificateSerialNumber); | ||
$batch->getTrustedCertificates() | ||
->add(SetaPDF_Signer_Pem::extractFromFile($caBundle)); | ||
|
||
// create a re-usable array of filenames (in/out) | ||
$files = [ | ||
[ | ||
'in' => new SetaPDF_Core_Reader_File('assets/tektown/Laboratory-Report.pdf'), | ||
'out' => 'output/tektown-signed.pdf' | ||
], | ||
[ | ||
'in' => new SetaPDF_Core_Reader_String(file_get_contents('assets/lenstown/Laboratory-Report.pdf')), | ||
'out' => 'output/lenstown-signed.pdf' | ||
], | ||
[ | ||
'in' => 'assets/etown/Laboratory-Report.pdf', | ||
'out' => 'output/etown-signed.pdf' | ||
], | ||
[ | ||
'in' => 'assets/camtown/Laboratory-Report.pdf', | ||
'out' => 'output/camtown-signed.pdf' | ||
], | ||
]; | ||
|
||
$batch->sign($files); |
Oops, something went wrong.