Repository navigation
Expand file tree
/
Copy pathbatch-simple.php
More file actions
79 lines (64 loc) · 2.41 KB
/
Copy pathbatch-simple.php
File metadata and controls
79 lines (64 loc) · 2.41 KB
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
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
use Ramsey\Uuid\Uuid;
use setasign\SetaPDF\Signer\Module\SAFE\Batch;
use setasign\SetaPDF\Signer\Module\SAFE\Client;
use setasign\SetaPDF2\Core\Reader\FileReader;
use setasign\SetaPDF2\Core\Reader\StringReader;
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 = 'signed.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);
$batch = new Batch($accessToken, $client, $credentialId, $processId, $settings['clientName']);
$batch->setCertificate($certificate);
$batch->setExtraCertificates($certificates);
$batch->getTrustedCertificates()
->add($certificates[count($certificates) - 1]); // add the root certificate from the chain as trusted (needed for LTV)
// create a re-usable array of filenames (in/out)
$files = [
[
'in' => new FileReader('assets/tektown/Laboratory-Report.pdf'),
'out' => 'output/tektown-signed.pdf'
],
[
'in' => new StringReader(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);