Repository navigation
Expand file tree
/
Copy pathgenerate.php
More file actions
217 lines (195 loc) · 7.71 KB
/
Copy pathgenerate.php
File metadata and controls
217 lines (195 loc) · 7.71 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
<?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),
* built with embedded Liberation fonts (run scripts/fetch-fonts.sh first)
* and the vendored sRGB ICC profile. These files are the input for
* scripts/conformance/verapdf.sh (veraPDF validation in CI).
*/
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\Table;
use Dskripchenko\PhpPdf\Font\Ttf\TtfFile;
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\Section;
use Dskripchenko\PhpPdf\Style\RunStyle;
$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);
}
$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;
}
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;
exit($failures > 0 ? 1 : 0);