Files
InvoiceShelf/tests/Unit/GotenbergPdfDriverTest.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

68 lines
2.4 KiB
PHP

<?php
use App\Support\Pdf\GotenbergPdfDriver;
/**
* These assert against buildRequest(), which assembles the Chromium multipart
* body without sending it. Nothing here needs a running Gotenberg.
*/
beforeEach(function () {
config([
'pdf.connections.gotenberg.host' => 'http://gotenberg.example.com:3000',
'pdf.page.paper_width' => '210mm',
'pdf.page.paper_height' => '297mm',
]);
});
// A real view that renders without any shared document data, so these stay
// unit tests rather than needing a seeded invoice.
function gotenbergRequestBody(string $template = 'app.pdf.partials.fonts'): string
{
return (string) (new GotenbergPdfDriver)->buildRequest($template)->getBody();
}
/**
* printBackground governs the root (body/html) background only — Chromium paints
* element backgrounds regardless, checked against gotenberg:8. dompdf paints the
* body background, so setting this keeps a custom template that styles `body`
* looking the same on either driver. No stock template sets one.
*/
test('the chromium request asks for the page background to be printed', function () {
expect(gotenbergRequestBody())->toContain('printBackground');
});
/**
* config/dompdf.php renders as `screen`; Chromium's own default is `print`. The
* two drivers should not disagree about which media type a template is styled for.
*/
test('the chromium request emulates the same media type dompdf uses', function () {
expect(gotenbergRequestBody())->toContain('emulatedMediaType');
});
test('the configured paper size reaches the request', function () {
config(['pdf.page.paper_width' => '8.5in', 'pdf.page.paper_height' => '11in']);
expect(gotenbergRequestBody())
->toContain('paperWidth')
->toContain('8.5in')
->toContain('11in');
});
test('the rendered document is sent as the index file', function () {
expect(gotenbergRequestBody())->toContain('index.html');
});
test('it throws when a page length has an unexpected format', function () {
config(['pdf.page.paper_width' => 'invalid']);
expect(fn () => gotenbergRequestBody())
->toThrow(InvalidArgumentException::class, 'Invalid PDF page length');
});
test('it throws when the configured host targets a private network address', function () {
config(['pdf.connections.gotenberg.host' => 'http://10.0.0.1:3000']);
expect(fn () => gotenbergRequestBody())
->toThrow(InvalidArgumentException::class, 'Invalid Gotenberg host');
});