Repository navigation
Expand file tree
/
Copy pathE007_signature_basic.php
More file actions
199 lines (176 loc) · 7.52 KB
/
Copy pathE007_signature_basic.php
File metadata and controls
199 lines (176 loc) · 7.52 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
/**
* E007_signature_basic.php
*
* @since 2026-04-21
* @category Library
* @package Pdf
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2002-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf
*
* This file is part of tc-lib-pdf software library.
*/
// NOTE: local file reads (images, fonts, attachments) are restricted to an allowlist of
// trusted paths that covers this package tree, so run the examples in place. To read assets
// from other locations, list them in the 'allowedPaths' entry of the fileOptions constructor
// parameter (see E047_remote_resources_security.php).
// NOTE: run make fonts in the project root to generate the dependencies and example fonts.
// autoloader when using Composer
require __DIR__ . '/../vendor/autoload.php';
// define fonts directory
\define('K_PATH_FONTS', \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-font/target/fonts'));
// main TCPDF object
$pdf = new \Com\Tecnick\Pdf\Tcpdf();
$pdf->setCreator('tc-lib-pdf');
$pdf->setAuthor('Nicola Asuni');
$pdf->setSubject('tc-lib-pdf example: 007');
$pdf->setTitle('PAdES B-B Digital Signature Example');
$pdf->setKeywords('TCPDF tc-lib-pdf example signature pades pades-b-b');
$pdf->setPDFFilename('007_signature_basic.pdf');
// Insert font before adding the first page.
$bfont = $pdf->font->insert($pdf->pon, 'helvetica', '', 11);
// Add a page and a simple signed-content block.
$page = $pdf->addPage();
$pdf->page->addContent($bfont['out']);
$text = 'This document is signed with a PAdES-BASELINE-B signature (/ETSI.CAdES.detached).';
$textCell = $pdf->getTextCell(
txt: $text,
posx: 15,
posy: 20,
width: 180,
height: 0,
offset: 0,
linespace: 1,
valign: \Com\Tecnick\Pdf\TextVAlign::Top,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
);
$pdf->page->addContent($textCell);
$text2 = 'The CMS carries the ESS signing-certificate-v2 attribute required by ETSI EN 319 142-1.';
$textCell2 = $pdf->getTextCell(
txt: $text2,
posx: 15,
posy: 27,
width: 180,
height: 0,
offset: 0,
linespace: 1,
valign: \Com\Tecnick\Pdf\TextVAlign::Top,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
);
$pdf->page->addContent($textCell2);
$certPath = \realpath(__DIR__ . '/data/cert/tcpdf.crt');
if ($certPath === false) {
throw new \RuntimeException('Missing signing certificate: examples/data/cert/tcpdf.crt');
}
$cert = 'file://' . $certPath;
// Preferred fluent API: $pdf->signature()->configure(...). The 'profile' option
// selects the PAdES baseline level, one of the Sign\Config::PROFILE_* constants;
// the digest is one of the Sign\DigestAlgorithm cases. The pre-facade
// $pdf->setSignature([...]) method still works and takes the same array. The signing
// process and this demo's limitations are rendered into the document itself (see the
// explanatory block added below).
$pdf->signature()->configure([
'profile' => \Com\Tecnick\Pdf\Sign\Config::PROFILE_PADES_B_B,
'digest_algorithm' => \Com\Tecnick\Pdf\Sign\DigestAlgorithm::Sha256->value,
'cert_type' => 2,
'info' => [
'ContactInfo' => 'https://github.com/tecnickcom/tc-lib-pdf',
'Location' => 'Demo Office',
'Name' => 'tc-lib-pdf',
'Reason' => 'PAdES-BASELINE-B signature example',
],
'password' => '',
'privkey' => $cert,
'signcert' => $cert,
]);
// Visible signature field plus one extra empty approval signature field.
$pdf->signature()->appearance()->place(posx: 15, posy: 35, width: 75, height: 20, page: -1, name: 'PrimarySignature');
// Optional custom appearance stream for the signature widget (/AP /N).
// Coordinate system starts from the lower-left corner of the widget rectangle.
$sigW = 75.0;
$sigH = 20.0;
$sigTopY = $page['height'] - $sigH;
// styles controls cell fill/border; set font and text color explicitly for AP text.
$sigAppearance = $bfont['out'];
$sigAppearance .= $pdf->color->getPdfColor('rgb(15%,15%,15%)');
$sigAppearance .= $pdf->getTextCell(
txt: 'Digitally signed by tc-lib-pdf',
posx: 0,
posy: $sigTopY,
width: $sigW,
height: $sigH,
offset: 2.5,
linespace: 0,
valign: \Com\Tecnick\Pdf\TextVAlign::Center,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
cell: null,
styles: [
'all' => [
'fillColor' => 'rgb(92%,96%,100%)',
'lineColor' => 'rgb(20%,32%,60%)',
'lineWidth' => 1.2,
],
],
strokewidth: 0,
wordspacing: 0,
leading: 0,
rise: 0,
jlast: true,
fill: true,
stroke: false,
underline: false,
linethrough: false,
overline: false,
clip: false,
drawcell: true,
);
$pdf->signature()->appearance()->stream(stream: $sigAppearance);
// Exposed object ID of the signature widget annotation.
// This can be useful for low-level workflows or custom QA checks.
$signatureWidgetObjId = $pdf->signature()->widgetObjectId();
$text3 = 'Primary signature widget object ID: ' . $signatureWidgetObjId;
$textCell3 = $pdf->getTextCell(
txt: $text3,
posx: 95,
posy: 35,
width: 100,
height: 0,
offset: 0,
linespace: 1,
valign: \Com\Tecnick\Pdf\TextVAlign::Top,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
);
$pdf->page->addContent($textCell3);
// A second, empty signature field. It is a valid unsigned placeholder (no CMS is
// written to it) that a later party can sign in an approval workflow. Only the
// PrimarySignature field above carries a signature in this example's output.
$pdf->signature()->emptyField(posx: 15, posy: 60, width: 75, height: 20, page: -1, name: 'ApprovalSignature');
// Render the signing process and the demo's limitations into the document so the
// output PDF is self-documenting (this text is page content, not part of the
// signed appearance stream).
$explainHtml = <<<HTML
<h2 style="font-size:11pt;color:#1f2a33;">How this PAdES-BASELINE-B signature is produced</h2>
<p style="font-size:9pt;color:#1f2a33;">The library reserves a <code>/Contents</code> placeholder inside a
<code>/SubFilter /ETSI.CAdES.detached</code> signature field, hashes the document over the two
<code>/ByteRange</code> spans that surround the placeholder, and embeds a detached CMS (PKCS#7). The
CAdES SignerInfo carries the mandatory content-type and message-digest attributes plus the ESS
signing-certificate-v2 attribute required by ETSI EN 319 142-1. Per the baseline the CMS signing-time
attribute is intentionally omitted; the claimed time is carried by the <code>/M</code> dictionary entry
instead. The produced CMS verifies cryptographically against the <code>/ByteRange</code> bytes.</p>
<h2 style="font-size:11pt;color:#7a1f1f;">Limitations of this demo</h2>
<ul style="font-size:9pt;color:#1f2a33;">
<li>B-B has no timestamp, so the <code>/M</code> signing time is a self-asserted claim and is not
cryptographically protected. Use <code>pades-b-t</code> (example E008) for a trusted signing time
backed by an RFC 3161 TSA.</li>
<li>The bundled certificate <code>examples/data/cert/tcpdf.crt</code> is self-signed. The CMS is
cryptographically valid, but the signer identity does not chain to a trusted root, so viewers report
the signature as valid with an unknown or untrusted signer. Sign with a CA-issued certificate for a
trusted identity verdict.</li>
</ul>
HTML;
$pdf->addHTMLCell(html: $explainHtml, posx: 15, posy: 85, width: 180);
$rawpdf = $pdf->getOutPDFString();
$pdf->renderPDF(rawpdf: $rawpdf);