Files
InvoiceShelf/tests/Unit/PdfDriverPageParityTest.php
Darko Gjorgjijoski a54a5ee007 feat(pdf): one page setup, honoured by both drivers (#728)
Paper size was a Gotenberg-only setting stored as a single "210mm 297mm"
string. dompdf had no page settings at all: size was pinned to config/dompdf.php's
fixed 'a4', its top-level `orientation` key was read by nothing (the installed
barryvdh v3 builds options only from `defines`), and margins were whatever
dompdf's own stylesheet said. So the two drivers disagreed about margins by
default -- dompdf 1.2cm, Gotenberg hardcoded to zero -- and selecting dompdf
silently discarded the paper size.

Replaces gotenberg_papersize with pdf_paper_width / pdf_paper_height /
pdf_orientation / pdf_margin_{top,right,bottom,left}, saved and applied for
either driver. Width and height are separate CSS lengths because that is the
only lossless shared notation: Gotenberg has no named sizes, and dompdf's named
table cannot express everything Gotenberg accepts. Named presets (A3/A4/A5/
Letter/Legal) are a convenience in the UI that resolve to a pair of lengths.

PdfPageSetup resolves it once and translates: a points array plus an orientation
argument for dompdf, CSS lengths plus landscape() for Gotenberg. Both are handed
the portrait pair, since each swaps the axes itself. Gotenberg's margins() takes
top, bottom, left, right, which is not the CSS order.

dompdf exposes no margin API, so DompdfDriver injects an @page rule -- at the
top of <head>, so a template declaring its own still wins. Doing it in the driver
rather than a Blade partial means custom templates get it without including
anything.

Margins default to 1.2cm, dompdf's existing default, so Gotenberg starts
matching it rather than rendering edge-to-edge. Verified against a live
gotenberg:8: A4 portrait, A4 landscape and Letter at zero margins all come out
with the same page box and the same ink offsets on both drivers.

A malformed length now throws rather than being ignored. Blank still falls back,
but a value that is set and wrong is an operator mistake, and the drivers would
otherwise fail differently: dompdf throws converting to points, Gotenberg would
forward the string and render at some other size.

Also here:
- Migration splits an existing gotenberg_papersize into the new pair. It earns
  its place because that key ships in 2.x, not just a 3.x alpha, so a stable
  install that chose Letter would otherwise come back up on A4. Drops
  gotenberg_margins, which 2.x also stores and neither driver ever read.
- Removes EnvironmentManager::savePDFVariables/getPDFConfiguration, which had no
  caller anywhere, and the unused EnvironmentManager injection in the controller.
- config/dompdf.php: drops the dead `orientation` key and defaults enable_remote
  to false, matching .env.example, which sets it explicitly and explains why.
  Installs predating that line were falling back to true.
- Retires the settings.pdf.footer_text and pdf_layout strings, which no component
  referenced.

Claude-Session: https://claude.ai/code/session_01QmECndmNZwzN65Zz9P87dF
2026-08-01 12:48:51 +02:00

110 lines
4.5 KiB
PHP

<?php
use App\Support\Pdf\DompdfDriver;
use App\Support\Pdf\GotenbergPdfDriver;
use App\Support\Pdf\PdfPageSetup;
/**
* The page setup is only worth anything if both drivers land on the same page.
*
* dompdf gets a points array plus an orientation argument; Gotenberg gets CSS
* lengths plus landscape(), and its margins() takes a different argument order
* than CSS. Those are three separate chances to get it subtly wrong, and the
* failure mode is a slightly shifted document rather than an error.
*
* The dompdf side is checked against real rendered output. Gotenberg's is
* checked on the request it would send, so this needs no running service --
* the rendered comparison against a live gotenberg:8 is in the PR description.
*/
beforeEach(function () {
config([
'pdf.connections.gotenberg.host' => 'http://gotenberg.example.com:3000',
'pdf.page.paper_width' => '210mm',
'pdf.page.paper_height' => '297mm',
'pdf.page.orientation' => 'portrait',
'pdf.page.margin_top' => '1.2cm',
'pdf.page.margin_right' => '1.2cm',
'pdf.page.margin_bottom' => '1.2cm',
'pdf.page.margin_left' => '1.2cm',
]);
});
test('the gotenberg request carries the configured page geometry', function () {
$body = (string) (new GotenbergPdfDriver)->buildRequest('app.pdf.partials.fonts')->getBody();
expect($body)->toContain('210mm')
->and($body)->toContain('297mm')
->and($body)->toContain('marginTop')
->and($body)->toContain('1.2cm');
});
test('landscape is requested of gotenberg rather than pre-swapping the paper', function () {
config(['pdf.page.orientation' => 'landscape']);
$body = (string) (new GotenbergPdfDriver)->buildRequest('app.pdf.partials.fonts')->getBody();
// Still the portrait pair: landscape() does the swap on Gotenberg's side,
// exactly as setPaper()'s orientation argument does on dompdf's.
expect($body)->toContain('landscape')
->and($body)->toContain('210mm')
->and($body)->toContain('297mm');
});
test('dompdf renders at the configured paper size', function () {
$pdf = (new DompdfDriver)->loadView('app.pdf.partials.fonts');
// A4 in points, per dompdf's own table.
expect($pdf->output())->toStartWith('%PDF-');
$page = PdfPageSetup::fromConfig();
expect($page->dompdfPaper())->toEqualWithDelta([0.0, 0.0, 595.28, 841.89], 0.01);
});
/**
* dompdf has no margin API, so the only way margins reach it is an injected
* page rule. If that injection stops happening, output silently reverts to
* dompdf's built-in 1.2cm and nothing else notices -- so assert on real
* rendered bytes, not just on the string helper.
*/
test('changing the margins changes what dompdf actually renders', function () {
config(['pdf.page.margin_top' => '0mm', 'pdf.page.margin_left' => '0mm']);
$tight = (new DompdfDriver)->loadView('app.pdf.partials.fonts')->output();
config(['pdf.page.margin_top' => '40mm', 'pdf.page.margin_left' => '40mm']);
$roomy = (new DompdfDriver)->loadView('app.pdf.partials.fonts')->output();
expect($tight)->toStartWith('%PDF-')
->and($roomy)->toStartWith('%PDF-')
->and($tight)->not->toBe($roomy);
});
test('the page size reaches dompdf rather than config/dompdf.php\'s fixed a4', function () {
config(['pdf.page.paper_width' => '8.5in', 'pdf.page.paper_height' => '14in']);
$legal = (new DompdfDriver)->loadView('app.pdf.partials.fonts')->output();
config(['pdf.page.paper_width' => '210mm', 'pdf.page.paper_height' => '297mm']);
$a4 = (new DompdfDriver)->loadView('app.pdf.partials.fonts')->output();
expect($legal)->not->toBe($a4);
});
/**
* Injected at the top of the head element so a template declaring its own page
* rule still wins -- later rules of equal specificity take precedence in CSS.
*/
test('the injected rule sits at the start of head so a template can override it', function () {
$html = '<html><head><style>@page { margin: 0; }</style></head><body></body></html>';
$method = new ReflectionMethod(DompdfDriver::class, 'withPageMargins');
$result = $method->invoke(new DompdfDriver, $html, PdfPageSetup::fromConfig());
expect(strpos($result, '1.2cm'))->toBeLessThan(strpos($result, 'margin: 0'));
});
test('markup with no head still receives the rule', function () {
$method = new ReflectionMethod(DompdfDriver::class, 'withPageMargins');
$result = $method->invoke(new DompdfDriver, '<p>bare</p>', PdfPageSetup::fromConfig());
expect($result)->toStartWith('<style>@page');
});