Skip to content

Commit b69cf1d

Browse files
jakejackson1claude
andcommitted
Round every patch mesh percentage, and check for veraPDF once
The patch mesh opacity bytes truncated 100% to 254 as its colour bytes did; one helper now rounds both, and the shadow fixture's blurred edges come out fully opaque where they should. The conformance tests ask whether veraPDF runs once, and skip before building documents where it does not. Their helpers, and those of the PDF/A-2 and ToUnicode tests, build on PageStreams and pdfaConformance() instead of repeating them. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent b1ae009 commit b69cf1d

8 files changed

Lines changed: 94 additions & 52 deletions

File tree

‎.github/workflows/conformance.yml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
runs-on: ubuntu-latest
2121

2222
env:
23-
# veraPDF validates against PDF/A, PDF/UA and WTPDF; tests in the conformance group choose the flavour
23+
# The veraPDF release to install; the cache is keyed on it
2424
VERAPDF_VERSION: "1.30.2"
2525

2626
steps:
@@ -50,9 +50,7 @@ jobs:
5050
rm -r verapdf-installer.zip verapdf.properties "verapdf-greenfield-${VERAPDF_VERSION}"
5151
5252
- name: "Put veraPDF on the PATH"
53-
run: |
54-
echo "$HOME/verapdf" >> "$GITHUB_PATH"
55-
"$HOME/verapdf/verapdf" --version
53+
run: echo "$HOME/verapdf" >> "$GITHUB_PATH"
5654

5755
- name: "Install PHP"
5856
uses: "shivammathur/setup-php@v2"

‎src/Gradient.php‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ public function CoonsPatchMesh($x, $y, $w, $h, $patch_array = [], $x_min = 0, $x
8585
$trans = true;
8686
}
8787
} elseif ($colspace === 'CMYK') {
88-
// Percentages to bytes, rounded: 100 * 2.55 is 254.99999999999997
8988
for ($k = 1; $k <= 4; $k++) {
90-
$this->mpdf->gradients[$n]['stream'] .= chr((int) round(ord($patch_array[$i]['colors'][$j][$k]) * 2.55));
89+
$this->mpdf->gradients[$n]['stream'] .= $this->percentToByte($patch_array[$i]['colors'][$j][$k]);
9190
}
9291
if (isset($patch_array[$i]['colors'][$j][5]) && ord($patch_array[$i]['colors'][$j][5]) < 100) {
9392
$trans = true;
@@ -118,11 +117,11 @@ public function CoonsPatchMesh($x, $y, $w, $h, $patch_array = [], $x_min = 0, $x
118117
for ($j = 0; $j < count($patch_array[$i]['colors']); $j++) {
119118
// each color component as 8 bit // OPACITY
120119
if ($colspace === 'RGB') {
121-
$this->mpdf->gradients[$n]['stream_trans'] .= chr((int) (ord($patch_array[$i]['colors'][$j][4]) * 2.55));
120+
$this->mpdf->gradients[$n]['stream_trans'] .= $this->percentToByte($patch_array[$i]['colors'][$j][4]);
122121
} elseif ($colspace === 'CMYK') {
123-
$this->mpdf->gradients[$n]['stream_trans'] .= chr((int) (ord($patch_array[$i]['colors'][$j][5]) * 2.55));
122+
$this->mpdf->gradients[$n]['stream_trans'] .= $this->percentToByte($patch_array[$i]['colors'][$j][5]);
124123
} elseif ($colspace === 'Gray') {
125-
$this->mpdf->gradients[$n]['stream_trans'] .= chr((int) (ord($patch_array[$i]['colors'][$j][3]) * 2.55));
124+
$this->mpdf->gradients[$n]['stream_trans'] .= $this->percentToByte($patch_array[$i]['colors'][$j][3]);
126125
}
127126
}
128127
}
@@ -144,6 +143,20 @@ public function CoonsPatchMesh($x, $y, $w, $h, $patch_array = [], $x_min = 0, $x
144143
$this->writer->write($s);
145144
}
146145

146+
/**
147+
* A colour component mPDF holds as a percentage, packed in a byte, as the 8-bit sample a patch mesh draws with
148+
*
149+
* Rounded, as 100 * 2.55 is 254.99999999999997.
150+
*
151+
* @param string $percent
152+
*
153+
* @return string
154+
*/
155+
private function percentToByte($percent)
156+
{
157+
return chr((int) round(ord($percent) * 2.55));
158+
}
159+
147160
// type = linear:2; radial: 3;
148161
// Linear: $coords - array of the form (x1, y1, x2, y2) which defines the gradient vector (see linear_gradient_coords.jpg).
149162
// The default value is from left to right (x1=0, y1=0, x2=1, y2=0).

‎tests/Mpdf/PDFA2Test.php‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ public function testPdfa2AutoLeavesOutFileItCannotEmbed()
236236

237237
$this->assertSame(1, substr_count($pdf, '/Type /EmbeddedFile'));
238238
$this->assertSame(1, substr_count($pdf, '/Subtype /FileAttachment'));
239-
$this->assertStringContainsString('/F (mpdfpdfa2attachment.pdf) /UF (mpdfpdfa2attachment.pdf)', $pdf);
239+
$name = str_replace('-', '', basename($pdfa)); // mPDF keeps letters, digits, dots and underscores
240+
$this->assertStringContainsString('/F (' . $name . ') /UF (' . $name . ')', $pdf);
240241
}
241242

242243
/**
@@ -257,7 +258,7 @@ private function translucentContent()
257258
*/
258259
private function pdfaFile()
259260
{
260-
$file = sys_get_temp_dir() . '/mpdf-pdfa2-attachment.pdf';
261+
$file = sys_get_temp_dir() . '/mpdf-pdfa2-' . uniqid() . '.pdf';
261262
$this->pdfa('2-B')->Output($file, 'F');
262263

263264
return $file;
@@ -273,10 +274,7 @@ private function pdfaFile()
273274
*/
274275
private function pdfa($version, $config = [])
275276
{
276-
$mpdf = new Mpdf($config + ['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => $version]);
277-
$mpdf->compress = false;
278-
279-
return $mpdf;
277+
return $this->mpdf($config + $this->pdfaConfig($version));
280278
}
281279

282280
/**
@@ -289,10 +287,20 @@ private function pdfa($version, $config = [])
289287
*/
290288
private function renderPdfa($version, $html)
291289
{
292-
$mpdf = $this->pdfa($version);
293-
$mpdf->WriteHTML($html);
290+
return $this->render($html, $this->pdfaConfig($version));
291+
}
294292

295-
return $mpdf->Output('', 'S');
293+
/**
294+
* The configuration of a PDF/A document that fixes what it can, in the embedded fonts PDF/A needs rather than the
295+
* core fonts PageStreams defaults to
296+
*
297+
* @param string $version
298+
*
299+
* @return mixed[]
300+
*/
301+
private function pdfaConfig($version)
302+
{
303+
return ['mode' => '', 'PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => $version];
296304
}
297305

298306
}

‎tests/Mpdf/PDFATest.php‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,6 @@ public function testPDFA_3B_SetsCatalogVersion17()
6969
$this->assertStringContainsString('/Version /1.7', $output);
7070
}
7171

72-
public function testPDFA_Version_Fail()
73-
{
74-
$this->mpdf->PDFAversion = '11';
75-
try {
76-
$this->mpdf->Output(null, 'S');
77-
} catch (\Exception $e) {
78-
$this->assertSame('PDFA version (11) is not valid. (Use: 1-B, 2-B, 2-U, 3-B or 3-U)', $e->getMessage());
79-
}
80-
}
81-
8272
/**
8373
* PDF/A-2 at levels B and U declares its part and level in the XMP metadata, without the PDF/A-1 amendment
8474
*
@@ -130,7 +120,7 @@ public function testPDFA_UnsupportedVersionIsRefused($version)
130120
*/
131121
public function unsupportedVersions()
132122
{
133-
return [['1-A'], ['1-U'], ['2-A'], ['3-A'], ['4'], ['2B']];
123+
return [['1-A'], ['1-U'], ['2-A'], ['3-A'], ['4'], ['11'], ['2B']];
134124
}
135125

136126
public function testOriginalPDFA_3B()

‎tests/Mpdf/PdfaConformanceTest.php‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ class PdfaConformanceTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
1717
*/
1818
private $files = [];
1919

20+
/**
21+
* Skip before building a document veraPDF is not there to check
22+
*/
23+
protected function set_up()
24+
{
25+
$this->skipWithoutVeraPdf();
26+
}
27+
2028
/**
2129
* Remove the documents written
2230
*/
2331
protected function tear_down()
2432
{
25-
foreach ($this->files as $file) {
26-
if (file_exists($file)) {
27-
unlink($file);
28-
}
29-
}
33+
array_map('unlink', $this->files);
3034
}
3135

3236
/**
@@ -39,7 +43,7 @@ public function testDocumentConforms($version, $config)
3943
{
4044
$img = __DIR__ . '/../data/img/';
4145

42-
$mpdf = new Mpdf($config + ['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => $version]);
46+
$mpdf = $this->pdfa($version, $config);
4347
$mpdf->SetWatermarkText('DRAFT');
4448
$mpdf->showWatermarkText = true;
4549
$mpdf->WriteHTML(
@@ -53,9 +57,8 @@ public function testDocumentConforms($version, $config)
5357
. '<img src="' . $img . 'truecolour-trns.png" /> <img style="opacity: 0.5" src="' . $img . 'tiger.jpg" width="20" />'
5458
. '<img src="' . $img . 'demo.svg" width="40" />'
5559
);
56-
$file = $this->write($mpdf);
5760

58-
$this->assertConforms($file, $this->flavour($version));
61+
$this->assertConforms($this->write($mpdf), $this->flavour($mpdf));
5962
}
6063

6164
/**
@@ -65,17 +68,17 @@ public function testDocumentConforms($version, $config)
6568
*/
6669
public function testAttachmentsConform($version)
6770
{
68-
$attachment = $this->write(new Mpdf(['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B']));
71+
$attachment = $this->write($this->pdfa('2-B'));
6972

70-
$mpdf = new Mpdf(['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => $version, 'allowAnnotationFiles' => true]);
73+
$mpdf = $this->pdfa($version, ['allowAnnotationFiles' => true]);
7174
$mpdf->WriteHTML(
7275
'<p>PDF/A <annotation content="PDF/A" file="' . $attachment . '" /></p>'
7376
. '<p>Plain <annotation content="Plain" file="' . __DIR__ . '/../data/pdfs/2-Page-PDF_1_4.pdf" /></p>'
7477
);
7578
$file = $this->write($mpdf);
7679

7780
$this->assertSame(1, substr_count(file_get_contents($file), '/Type /EmbeddedFile'));
78-
$this->assertConforms($file, $this->flavour($version));
81+
$this->assertConforms($file, $this->flavour($mpdf));
7982
}
8083

8184
/**
@@ -106,6 +109,19 @@ public function pdfa2Versions()
106109
return [['2-B'], ['2-U']];
107110
}
108111

112+
/**
113+
* A PDF/A document of the given version that fixes what it can
114+
*
115+
* @param string $version
116+
* @param mixed[] $config
117+
*
118+
* @return \Mpdf\Mpdf
119+
*/
120+
private function pdfa($version, $config = [])
121+
{
122+
return new Mpdf($config + ['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => $version]);
123+
}
124+
109125
/**
110126
* Writes a document to a temporary file, removed after the test
111127
*
@@ -123,15 +139,15 @@ private function write(Mpdf $mpdf)
123139
}
124140

125141
/**
126-
* The veraPDF flavour of a PDFAversion: '2-B' is '2b'
142+
* The veraPDF flavour a PDF/A document claims: PDF/A-2b is '2b'
127143
*
128-
* @param string $version
144+
* @param \Mpdf\Mpdf $mpdf
129145
*
130146
* @return string
131147
*/
132-
private function flavour($version)
148+
private function flavour(Mpdf $mpdf)
133149
{
134-
return strtolower(str_replace('-', '', $version));
150+
return strtolower(implode('', $mpdf->pdfaConformance()));
135151
}
136152

137153
}

‎tests/Mpdf/ToUnicodeTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
class ToUnicodeTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
99
{
1010

11+
use PageStreams;
12+
1113
/**
1214
* A subset maps each run of 256 codes it draws from, as a range of its own
1315
*/
@@ -39,11 +41,9 @@ public function testWholeFontMapsEveryRow()
3941
*/
4042
private function cmap($config)
4143
{
42-
$mpdf = new Mpdf($config + ['default_font' => 'dejavusans']);
43-
$mpdf->compress = false;
44-
$mpdf->WriteHTML('<p>Ελληνικά</p>');
44+
$pdf = $this->render('<p>Ελληνικά</p>', $config + ['mode' => '', 'default_font' => 'dejavusans']);
4545

46-
$this->assertSame(1, preg_match('/\/CMapName \/Adobe-Identity-UCS def\n(.*?)endcmap/s', $mpdf->Output('', 'S'), $cmap));
46+
$this->assertSame(1, preg_match('/\/CMapName \/Adobe-Identity-UCS def\n(.*?)endcmap/s', $pdf, $cmap));
4747

4848
return $cmap[1];
4949
}

‎tests/Mpdf/VeraPdf.php‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@
1111
trait VeraPdf
1212
{
1313

14+
/**
15+
* @var bool|null whether veraPDF runs, asked once as each start is a JVM's
16+
*/
17+
private static $veraPdfRuns;
18+
19+
/**
20+
* Skips the test unless veraPDF can be run, to call before the test spends time building a document
21+
*/
22+
private function skipWithoutVeraPdf()
23+
{
24+
if (self::$veraPdfRuns === null) {
25+
exec('verapdf --version 2>&1', $version, $status);
26+
self::$veraPdfRuns = $status === 0;
27+
}
28+
29+
if (!self::$veraPdfRuns) {
30+
$this->markTestSkipped('veraPDF is not on the PATH');
31+
}
32+
}
33+
1434
/**
1535
* Asserts veraPDF finds the document conforms to a flavour, as its --flavour option names it: '2b' for
1636
* PDF/A-2b, 'ua1' for PDF/UA-1 and so on
@@ -20,10 +40,7 @@ trait VeraPdf
2040
*/
2141
private function assertConforms($file, $flavour)
2242
{
23-
exec('verapdf --version 2>&1', $version, $status);
24-
if ($status !== 0) {
25-
$this->markTestSkipped('veraPDF is not on the PATH');
26-
}
43+
$this->skipWithoutVeraPdf();
2744

2845
exec('verapdf --flavour ' . escapeshellarg($flavour) . ' --format xml ' . escapeshellarg($file) . ' 2>&1', $report);
2946
$report = implode("\n", $report);

‎tests/data/snapshots/shadow.pdf‎

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)