mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-08-04 23:22:12 +00:00
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
107 lines
3.6 KiB
PHP
107 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\FileDisk;
|
|
use App\Models\Setting;
|
|
use App\Services\Mail\MailConfigurationService;
|
|
use App\Services\Storage\FileDiskService;
|
|
use App\Support\Setup\InstallUtils;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AppConfigProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Bootstrap services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
// Check if database is available
|
|
if (! InstallUtils::isDbCreated()) {
|
|
return;
|
|
}
|
|
|
|
$this->configureMailFromDatabase();
|
|
$this->configurePDFFromDatabase();
|
|
$this->configureFileSystemFromDatabase();
|
|
}
|
|
|
|
/**
|
|
* Configure mail settings from database
|
|
*/
|
|
protected function configureMailFromDatabase(): void
|
|
{
|
|
try {
|
|
app(MailConfigurationService::class)->applyGlobalConfig();
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
// This prevents the application from breaking during setup
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure PDF settings from database
|
|
*/
|
|
protected function configurePDFFromDatabase(): void
|
|
{
|
|
try {
|
|
$pageSettings = [
|
|
'pdf_paper_width' => 'pdf.page.paper_width',
|
|
'pdf_paper_height' => 'pdf.page.paper_height',
|
|
'pdf_orientation' => 'pdf.page.orientation',
|
|
'pdf_margin_top' => 'pdf.page.margin_top',
|
|
'pdf_margin_right' => 'pdf.page.margin_right',
|
|
'pdf_margin_bottom' => 'pdf.page.margin_bottom',
|
|
'pdf_margin_left' => 'pdf.page.margin_left',
|
|
];
|
|
|
|
$pdfSettings = Setting::getSettings(array_merge(
|
|
['pdf_driver', 'gotenberg_host'],
|
|
array_keys($pageSettings),
|
|
));
|
|
|
|
if (! empty($pdfSettings['pdf_driver'])) {
|
|
Config::set('pdf.driver', $pdfSettings['pdf_driver']);
|
|
|
|
if ($pdfSettings['pdf_driver'] === 'gotenberg' && ! empty($pdfSettings['gotenberg_host'])) {
|
|
Config::set('pdf.connections.gotenberg.host', $pdfSettings['gotenberg_host']);
|
|
}
|
|
}
|
|
|
|
// Page geometry is applied regardless of driver. Note the isset guard
|
|
// rather than !empty: a saved margin of "0mm" is a deliberate choice,
|
|
// and !empty() would discard it and silently fall back to the default.
|
|
foreach ($pageSettings as $setting => $configKey) {
|
|
if (isset($pdfSettings[$setting]) && trim((string) $pdfSettings[$setting]) !== '') {
|
|
Config::set($configKey, $pdfSettings[$setting]);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
// This prevents the application from breaking during setup
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure file system settings from database
|
|
*/
|
|
protected function configureFileSystemFromDatabase(): void
|
|
{
|
|
try {
|
|
$fileDisk = FileDisk::whereSetAsDefault(true)->first();
|
|
|
|
if (! $fileDisk) {
|
|
return;
|
|
}
|
|
|
|
$diskName = app(FileDiskService::class)->registerDisk($fileDisk);
|
|
|
|
// Point Spatie Media Library at the resolved disk
|
|
config(['media-library.disk_name' => $diskName]);
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
}
|
|
}
|
|
}
|