Repository navigation
Expand file tree
/
Copy pathgenerate.php
More file actions
330 lines (305 loc) · 12.6 KB
/
Copy pathgenerate.php
File metadata and controls
330 lines (305 loc) · 12.6 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
declare(strict_types=1);
/**
* Generate reference PDF/A documents for external conformance validation.
*
* Usage: php scripts/conformance/generate.php [output-dir]
*
* Output: one PDF per flavour (pdfa-1b, pdfa-1a, pdfa-2b, pdfa-2u, pdfa-3b,
* plus pdfa-2b-signed — signed, with a signature stamp), built with embedded
* Liberation fonts (run scripts/fetch-fonts.sh first), plus pdfa-1b-cff,
* pdfa-2u-cff and pdfa-2u-cff-cid — CFF-outline OpenType fonts (Source Sans 3
* and the CID-keyed test font in tests/fixtures/fonts) — and the vendored sRGB
* ICC profile. These files are the input for scripts/conformance/verapdf.sh
* (veraPDF validation in CI). Also the visual fixtures (form-fill,
* signature-stamp) for scripts/conformance/visual-check.sh.
*/
use Dskripchenko\PhpPdf\Document;
use Dskripchenko\PhpPdf\Element\Cell;
use Dskripchenko\PhpPdf\Element\Heading;
use Dskripchenko\PhpPdf\Element\Paragraph;
use Dskripchenko\PhpPdf\Element\Row;
use Dskripchenko\PhpPdf\Element\Run;
use Dskripchenko\PhpPdf\Element\SignatureField;
use Dskripchenko\PhpPdf\Element\Table;
use Dskripchenko\PhpPdf\Font\Ttf\TtfFile;
use Dskripchenko\PhpPdf\Image\PdfImage;
use Dskripchenko\PhpPdf\Layout\Engine;
use Dskripchenko\PhpPdf\Pdf\Document as PdfDocument;
use Dskripchenko\PhpPdf\Pdf\Forms\ExistingFormFiller;
use Dskripchenko\PhpPdf\Pdf\PdfAConfig;
use Dskripchenko\PhpPdf\Pdf\PdfFont;
use Dskripchenko\PhpPdf\Pdf\PdfXConfig;
use Dskripchenko\PhpPdf\Pdf\Signature\Appearance\Box;
use Dskripchenko\PhpPdf\Pdf\Signature\Appearance\ImageBlock;
use Dskripchenko\PhpPdf\Pdf\Signature\Appearance\QrBlock;
use Dskripchenko\PhpPdf\Pdf\Signature\Appearance\SignatureAppearance;
use Dskripchenko\PhpPdf\Pdf\Signature\Appearance\TextBlock;
use Dskripchenko\PhpPdf\Pdf\SignatureConfig;
use Dskripchenko\PhpPdf\Section;
use Dskripchenko\PhpPdf\Style\Alignment;
use Dskripchenko\PhpPdf\Style\RunStyle;
use Dskripchenko\PhpPdf\Style\VerticalAlignment;
$root = dirname(__DIR__, 2);
require $root.'/vendor/autoload.php';
$fontDir = $root.'/.cache/fonts/liberation-fonts-ttf-2.1.5';
if (! is_readable($fontDir.'/LiberationSans-Regular.ttf')) {
fwrite(STDERR, "Liberation fonts not cached — run scripts/fetch-fonts.sh first.\n");
exit(1);
}
$cffFontPath = $root.'/.cache/fonts/SourceSans3-Regular.otf';
$cffCidFontPath = $root.'/tests/fixtures/fonts/MiniCffCid-Regular.otf';
if (! is_readable($cffFontPath)) {
fwrite(STDERR, "Source Sans 3 not cached — run scripts/fetch-fonts.sh first.\n");
exit(1);
}
$iccPath = $root.'/resources/icc/sRGB2014.icc';
if (! is_readable($iccPath)) {
fwrite(STDERR, "Missing ICC profile: $iccPath\n");
exit(1);
}
$outDir = $argv[1] ?? $root.'/build/conformance';
if (! is_dir($outDir) && ! mkdir($outDir, 0777, true)) {
fwrite(STDERR, "Cannot create output dir: $outDir\n");
exit(1);
}
$flavours = [
'pdfa-1b' => [PdfAConfig::PART_1, PdfAConfig::CONFORMANCE_B],
'pdfa-1a' => [PdfAConfig::PART_1, PdfAConfig::CONFORMANCE_A],
'pdfa-2b' => [PdfAConfig::PART_2, PdfAConfig::CONFORMANCE_B],
'pdfa-2u' => [PdfAConfig::PART_2, PdfAConfig::CONFORMANCE_U],
'pdfa-3b' => [PdfAConfig::PART_3, PdfAConfig::CONFORMANCE_B],
];
$bold = new RunStyle(bold: true);
$section = new Section([
new Heading(1, [new Run('Conformance Reference Document')]),
new Paragraph([
new Run('This document is generated by dskripchenko/php-pdf as a reference '.
'artifact for automated PDF/A validation with veraPDF. It exercises '.
'embedded TrueType fonts with subsetting, Cyrillic text, bold runs '.
'and a table.'),
]),
new Heading(2, [new Run('Multilingual text')]),
new Paragraph([
new Run('Архивный формат PDF/A требует встраивания шрифтов и цветового '.
'профиля. Этот абзац проверяет кириллицу: съешь же ещё этих мягких '.
'французских булок, да выпей чаю.'),
]),
new Paragraph([
new Run('Emphasis check: ', $bold),
new Run('regular text after a bold run, with ligatures — office, floor, '.
'difficult.'),
]),
new Heading(2, [new Run('Tabular data')]),
new Table([
new Row([
new Cell([new Paragraph([new Run('Flavour', $bold)])]),
new Cell([new Paragraph([new Run('Standard', $bold)])]),
new Cell([new Paragraph([new Run('Validator', $bold)])]),
]),
new Row([
new Cell([new Paragraph([new Run('PDF/A-1b')])]),
new Cell([new Paragraph([new Run('ISO 19005-1')])]),
new Cell([new Paragraph([new Run('veraPDF')])]),
]),
new Row([
new Cell([new Paragraph([new Run('PDF/A-2u')])]),
new Cell([new Paragraph([new Run('ISO 19005-2')])]),
new Cell([new Paragraph([new Run('veraPDF')])]),
]),
]),
]);
$engine = new Engine(
defaultFont: new PdfFont(TtfFile::fromFile($fontDir.'/LiberationSans-Regular.ttf')),
boldFont: new PdfFont(TtfFile::fromFile($fontDir.'/LiberationSans-Bold.ttf')),
);
// X-3 and X-4 permit an RGB output intent (sRGB); X-1a mandates CMYK —
// its reference is generated only when the CGATS21 profile is cached
// (scripts/fetch-icc.sh).
$pdfxVariants = [
'pdfx-3' => PdfXConfig::VARIANT_X3,
'pdfx-4' => PdfXConfig::VARIANT_X4,
];
$cmykIccPath = $root.'/.cache/icc/CGATS21_CRPC1.icc';
if (! is_readable($cmykIccPath)) {
fwrite(STDERR, "note: pdfx-1a skipped — run scripts/fetch-icc.sh for the CMYK profile\n");
}
$emit = function (string $name, Document $document) use ($engine, $outDir): bool {
$path = $outDir.'/'.$name.'.pdf';
try {
file_put_contents($path, $document->toBytes($engine));
echo "generated $path\n";
return true;
} catch (\Throwable $e) {
fwrite(STDERR, "FAILED $name: {$e->getMessage()}\n");
return false;
}
};
$failures = 0;
foreach ($flavours as $name => [$part, $conformance]) {
$ok = $emit($name, new Document(
$section,
lang: 'en',
pdfA: new PdfAConfig(
$iccPath,
iccProfileName: 'sRGB2014',
lang: 'en',
title: 'php-pdf conformance reference — '.strtoupper($name),
author: 'dskripchenko/php-pdf',
part: $part,
conformance: $conformance,
),
));
$failures += $ok ? 0 : 1;
}
// CFF-outline OpenType fonts: CIDFontType0 + FontFile3/CIDFontType0C. PDF/A-1
// is the strictest case (PDF 1.4 base), the CID-keyed font checks that CIDs
// are written through the CFF charset.
$cffEngine = new Engine(defaultFont: new PdfFont(TtfFile::fromFile($cffFontPath)));
$cffCidEngine = new Engine(defaultFont: new PdfFont(TtfFile::fromFile($cffCidFontPath)));
$cffCases = [
'pdfa-1b-cff' => [$cffEngine, $section, PdfAConfig::PART_1, PdfAConfig::CONFORMANCE_B],
'pdfa-2u-cff' => [$cffEngine, $section, PdfAConfig::PART_2, PdfAConfig::CONFORMANCE_U],
// The test font has only the glyphs of this line.
'pdfa-2u-cff-cid' => [
$cffCidEngine,
new Section([new Paragraph([new Run('Hello World П')])]),
PdfAConfig::PART_2,
PdfAConfig::CONFORMANCE_U,
],
];
foreach ($cffCases as $name => [$cffCaseEngine, $cffSection, $part, $conformance]) {
$path = $outDir.'/'.$name.'.pdf';
try {
file_put_contents($path, (new Document(
$cffSection,
lang: 'en',
pdfA: new PdfAConfig(
$iccPath,
iccProfileName: 'sRGB2014',
lang: 'en',
title: 'php-pdf conformance reference — '.strtoupper($name),
author: 'dskripchenko/php-pdf',
part: $part,
conformance: $conformance,
),
))->toBytes($cffCaseEngine));
echo "generated $path\n";
} catch (\Throwable $e) {
fwrite(STDERR, "FAILED $name: {$e->getMessage()}\n");
$failures++;
}
}
foreach ($pdfxVariants as $name => $variant) {
$ok = $emit($name, new Document(
$section,
lang: 'en',
pdfX: new PdfXConfig(
$iccPath,
iccProfileName: 'sRGB2014',
outputConditionIdentifier: 'sRGB IEC61966-2.1',
outputCondition: 'sRGB colour space — reference display condition',
variant: $variant,
trapped: PdfXConfig::TRAPPED_FALSE,
title: 'php-pdf conformance reference — '.strtoupper($name),
author: 'dskripchenko/php-pdf',
),
));
$failures += $ok ? 0 : 1;
}
if (is_readable($cmykIccPath)) {
$ok = $emit('pdfx-1a', new Document(
$section,
lang: 'en',
pdfX: new PdfXConfig(
$cmykIccPath,
iccProfileName: 'CGATS21_CRPC1',
outputConditionIdentifier: 'CGATS21-2-CRPC1',
outputCondition: 'CGATS 21-2 Reference Print Condition 1',
registryName: 'http://www.color.org',
variant: PdfXConfig::VARIANT_X1A,
trapped: PdfXConfig::TRAPPED_FALSE,
title: 'php-pdf conformance reference — PDFX-1A',
author: 'dskripchenko/php-pdf',
),
));
$failures += $ok ? 0 : 1;
}
// Visual fixture for ExistingFormFiller: a small template with Latin and
// Cyrillic text fields and a checkbox, filled, flattened, and stamped with an
// image — exercises the whole fill/flatten/stamp pipeline in one rendered page.
$template = PdfDocument::new(compressStreams: false);
$templatePage = $template->addPage();
$templatePage->addFormField('text', 'full_name', 72, 700, 250, 20, defaultValue: '');
$templatePage->addFormField('text', 'city', 72, 670, 250, 20, defaultValue: '');
$templatePage->addFormField('checkbox', 'agree', 72, 640, 14, 14);
$formFillOk = true;
try {
$bytes = ExistingFormFiller::fromBytes($template->toBytes())
->useFont($fontDir.'/LiberationSans-Regular.ttf')
->setValue('full_name', 'Jane Roe')
->setValue('city', 'Москва')
->setValue('agree', true)
->flatten()
->stampImage(0, $root.'/tests/fixtures/sample.png', 72, 200, 80, 60)
->toBytes();
file_put_contents($outDir.'/form-fill.pdf', $bytes);
echo "generated $outDir/form-fill.pdf\n";
} catch (\Throwable $e) {
fwrite(STDERR, "FAILED form-fill: {$e->getMessage()}\n");
$formFillOk = false;
}
$failures += $formFillOk ? 0 : 1;
// Signature stamps. The signer is a committed test certificate with a fixed
// serial and validity, and the signing time is fixed, so the stamp text — and
// the visual golden — do not change between runs.
$pki = $root.'/tests/fixtures/pki';
$signature = new SignatureConfig(
(string) file_get_contents($pki.'/stamp-signer.crt'),
(string) file_get_contents($pki.'/stamp-signer.key'),
reason: 'Согласование',
location: 'Москва',
signedAt: new \DateTimeImmutable('2026-10-13 12:00:00+03:00'),
fieldName: 'approval',
);
$stampSection = new Section([
new Heading(1, [new Run('Signature stamps')]),
new Paragraph([new Run('Договор поставки № 42. The field "approval" is signed and shows the '.
'certificate; "counterparty" is left empty for a later signer.')]),
new SignatureField('approval', appearance: SignatureAppearance::electronicSignatureStamp('ru')),
new SignatureField(
'counterparty',
heightPt: 50,
appearance: new SignatureAppearance(
[new TextBlock("Место для подписи\n{party}", align: Alignment::Center, verticalAlign: VerticalAlignment::Center, sizePt: 9)],
borderColor: '#888888',
variables: ['party' => 'Контрагент'],
),
alignment: Alignment::End,
),
new SignatureField(
'seal',
widthPt: 220,
heightPt: 80,
appearance: new SignatureAppearance([
new ImageBlock(PdfImage::fromPath($root.'/tests/fixtures/sample.png'), new Box(0, 0, 0.6, 1)),
new QrBlock('https://example.com/verify?s=5A17C0DE2026', new Box(0.65, 0, 0.35, 1)),
], borderColor: '#1F3A93'),
),
]);
$failures += $emit('signature-stamp', new Document($stampSection, lang: 'ru', signature: $signature)) ? 0 : 1;
$failures += $emit('pdfa-2b-signed', new Document(
$stampSection,
lang: 'ru',
pdfA: new PdfAConfig(
$iccPath,
iccProfileName: 'sRGB2014',
lang: 'ru',
title: 'php-pdf conformance reference — PDFA-2B signed',
author: 'dskripchenko/php-pdf',
part: PdfAConfig::PART_2,
conformance: PdfAConfig::CONFORMANCE_B,
),
signature: $signature,
)) ? 0 : 1;
exit($failures > 0 ? 1 : 0);