-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.php
72 lines (54 loc) · 2.26 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
use Ramsey\Uuid\Uuid;
use setasign\SetaPDF\Signer\Module\SAFE\Client;
use setasign\SetaPDF\Signer\Module\SAFE\Module;
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
require_once(__DIR__ . '/../vendor/autoload.php');
if (!file_exists('settings.php')) {
echo 'The settings.php file is missing. See settings.php.dist for an example.';
die();
}
$settings = require 'settings.php';
$apiUri = $settings['apiUri'];
$fileToSign = __DIR__ . '/assets/camtown/Laboratory-Report.pdf';
$resultPath = 'output/demo.pdf';
$processId = Uuid::uuid4()->toString();
$accessToken = $settings['accessToken'];
$credentialId = $settings['credentialId'];
$httpClient = new GuzzleHttp\Client();
$requestFactory = new Http\Factory\Guzzle\RequestFactory();
$streamFactory = new Http\Factory\Guzzle\StreamFactory();
$client = new Client(
$apiUri,
$settings['basicAuthUsername'],
$settings['basicAuthPassword'],
$httpClient,
$requestFactory,
$streamFactory
);
// fetch all information regarding your credential id like the certificates
$credentialInfo = $client->credentialsInfo($accessToken, $credentialId, $processId, $settings['clientName'], 'chain');
//echo '<pre>';
//var_dump($credentialInfo);
//echo '</pre>';
$certificates = $credentialInfo['cert']['certificates'];
// INFO: YOU SHOULD CACHE THE DATA IN $credentialInfo FOR LESS API REQUESTS
// the first certificate is always the signing certificate
$certificate = array_shift($certificates);
$module = new Module($accessToken, $client, $credentialId, $processId, $settings['clientName']);
$module->setCertificate($certificate);
$module->setExtraCertificates($certificates);
// specify a document name that is send to the API
$module->setDocumentName('my-document-name.pdf');
// create a writer instance
$writer = new SetaPDF_Core_Writer_File($resultPath);
// create the document instance
$document = SetaPDF_Core_Document::loadByFilename($fileToSign, $writer);
// create the signer instance
$signer = new SetaPDF_Signer($document);
$signer->sign($module);
echo '<a href="data:application/pdf;base64,' . base64_encode(file_get_contents($resultPath)) . '" ' .
'download="' . basename($resultPath) . '">download</a> | <a href="?">restart</a><br />';