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
This commit is contained in:
Darko Gjorgjijoski
2026-08-01 12:48:51 +02:00
committed by GitHub
parent 6cb754da60
commit a54a5ee007
23 changed files with 1104 additions and 251 deletions

View File

@@ -0,0 +1,140 @@
<?php
namespace App\Support\Pdf;
/**
* The page geometry both drivers render to, resolved once and translated per driver.
*
* Paper size used to be a Gotenberg-only setting stored as "210mm 297mm", while
* dompdf was pinned to whatever `config/dompdf.php` said and had no admin control
* at all. The two also disagreed about margins: dompdf falls back to its own
* stylesheet default of 1.2cm, Gotenberg was hardcoded to zero, so the same
* template came out differently depending on the driver.
*
* Dimensions are stored as CSS lengths because that is the only representation
* both drivers take without loss. Gotenberg has no notion of named sizes, only
* dimensions; dompdf accepts either a name from its own 66-entry table or a
* points array, and the points array is the branch that can express anything.
* Named presets are a convenience in the UI that resolve to a pair of lengths.
*/
final class PdfPageSetup
{
/** Points per unit. CSS px is 1/96in, PDF points are 1/72in. */
private const POINTS_PER_UNIT = [
'pt' => 1.0,
'px' => 0.75,
'pc' => 12.0,
'mm' => 72 / 25.4,
'cm' => 720 / 25.4,
'in' => 72.0,
];
private function __construct(
public readonly string $width,
public readonly string $height,
public readonly string $orientation,
public readonly string $marginTop,
public readonly string $marginRight,
public readonly string $marginBottom,
public readonly string $marginLeft,
) {}
public static function fromConfig(): self
{
return new self(
width: self::length('pdf.page.paper_width', '210mm'),
height: self::length('pdf.page.paper_height', '297mm'),
orientation: config('pdf.page.orientation') === 'landscape' ? 'landscape' : 'portrait',
marginTop: self::length('pdf.page.margin_top', '1.2cm'),
marginRight: self::length('pdf.page.margin_right', '1.2cm'),
marginBottom: self::length('pdf.page.margin_bottom', '1.2cm'),
marginLeft: self::length('pdf.page.margin_left', '1.2cm'),
);
}
public function isLandscape(): bool
{
return $this->orientation === 'landscape';
}
/**
* Portrait dimensions for Gotenberg's paperSize(). Orientation is applied
* separately via landscape(), which does the swap itself.
*
* @return array{0: string, 1: string}
*/
public function gotenbergPaper(): array
{
return [$this->width, $this->height];
}
/**
* Gotenberg's margins() takes top, bottom, left, right — note the order,
* which is not the CSS one.
*
* @return array{0: string, 1: string, 2: string, 3: string}
*/
public function gotenbergMargins(): array
{
return [$this->marginTop, $this->marginBottom, $this->marginLeft, $this->marginRight];
}
/**
* Points array for dompdf's setPaper(). Always portrait: Dompdf::getPaperSize()
* swaps the axes itself when the orientation argument says landscape, so
* pre-swapping here would cancel out.
*
* @return array{0: float, 1: float, 2: float, 3: float}
*/
public function dompdfPaper(): array
{
return [0.0, 0.0, self::toPoints($this->width), self::toPoints($this->height)];
}
/**
* dompdf has no margin API at all — margins come from the `@page` box, so
* the only lever is CSS. See DompdfDriver, which injects this.
*/
public function marginCss(): string
{
return "{$this->marginTop} {$this->marginRight} {$this->marginBottom} {$this->marginLeft}";
}
public static function toPoints(string $length): float
{
if (! preg_match('/^(\d+(?:\.\d+)?)(pt|px|pc|mm|cm|in)$/', trim($length), $m)) {
throw new \InvalidArgumentException("Invalid PDF page length: {$length}");
}
return (float) $m[1] * self::POINTS_PER_UNIT[$m[2]];
}
/**
* Unset or blank falls back to the default; anything set but malformed
* throws.
*
* Values are validated on save, but config can also come from the
* environment, and the drivers would fail differently otherwise: dompdf
* throws while converting to points, whereas Gotenberg would forward the
* garbage and render at some other size. Failing here keeps them consistent
* and names the offending key.
*/
private static function length(string $key, string $fallback): string
{
$value = config($key);
if (! is_string($value) || trim($value) === '') {
return $fallback;
}
$value = trim($value);
if (! preg_match('/^\d+(\.\d+)?(pt|px|pc|mm|cm|in)$/', $value)) {
throw new \InvalidArgumentException(
"Invalid PDF page length for {$key}: \"{$value}\". Expected a number and a unit, e.g. \"210mm\"."
);
}
return $value;
}
}