mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-08-05 07:32:14 +00:00
Takes over #690 by csoscd. The companion-view idea is theirs; this reworks it onto the shared page setup and fills in the gaps that stopped it landing. A `{template}_header` or `{template}_footer` view next to a template is rendered alongside it and repeated by Chromium on every page. The suffix resolves through the pdf_templates:: namespace too, so custom templates get it with no extra wiring. Two things had to change for that to be useful. Companion views are now hidden from the template picker. getFormattedTemplates() lists every .blade.php it finds, so an invoice1_footer would otherwise appear as a separately selectable template with no preview image -- the feature would have introduced that the moment anyone used it. And it does something out of the box. #690 shipped no companion views, so both of its margin settings were visible no-ops until someone hand-wrote a Blade file. Instead there is a pdf_page_numbers setting, off by default, that supplies a footer when a template has none. A template's own companion still wins, so turning page numbers on cannot overwrite a designed footer. The setting sits under Gotenberg because only Chromium can repeat a footer; dompdf has no equivalent. Its value is still carried by the dompdf form so saving from there cannot clear the choice -- the field is absent from that payload, and the controller only writes it when present. Margins come from the page setup rather than #690's separate header_margin and footer_margin. Chromium draws header and footer inside the page margin, so the existing margins are the space they occupy; two more settings for the same distance would have been a second way to say the same thing. Verified against a live gotenberg:8 on a two-page document: off produces no footer, on produces "1/2" and "2/2" on the respective pages, and a companion footer replaces both. #690's own test is not carried over. It asserted nothing: a bare View::shouldReceive('exists') is an allowance rather than an expectation, andReturn(false) never entered the companion branch, and the call sat inside try { } catch (Throwable) { }, so it passed with the feature deleted. Claude-Session: https://claude.ai/code/session_01QmECndmNZwzN65Zz9P87dF
151 lines
4.4 KiB
PHP
151 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Settings;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\PDFConfigurationRequest;
|
|
use App\Models\Setting;
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class PDFConfigurationController extends Controller
|
|
{
|
|
/**
|
|
* Driver-neutral page geometry. Each maps onto `pdf.page.*` in config, and is
|
|
* read and written for every driver rather than being nested under one.
|
|
*/
|
|
private const PAGE_SETTINGS = [
|
|
'pdf_paper_width',
|
|
'pdf_paper_height',
|
|
'pdf_orientation',
|
|
'pdf_margin_top',
|
|
'pdf_margin_right',
|
|
'pdf_margin_bottom',
|
|
'pdf_margin_left',
|
|
];
|
|
|
|
/**
|
|
* Stored in the same string-valued settings table, so kept apart from the
|
|
* lengths above: it needs an explicit cast in both directions rather than
|
|
* being passed through.
|
|
*/
|
|
private const PAGE_BOOLEANS = [
|
|
'pdf_page_numbers',
|
|
];
|
|
|
|
/**
|
|
* Returns the available drivers
|
|
*
|
|
* @throws AuthorizationException
|
|
*/
|
|
public function getDrivers(): JsonResponse
|
|
{
|
|
$this->authorize('manage pdf config');
|
|
|
|
$drivers = [
|
|
'dompdf',
|
|
'gotenberg',
|
|
];
|
|
|
|
return response()->json($drivers);
|
|
}
|
|
|
|
/**
|
|
* Return the PDF settings
|
|
*
|
|
* @throws AuthorizationException
|
|
*/
|
|
public function getEnvironment(): JsonResponse
|
|
{
|
|
$this->authorize('manage pdf config');
|
|
|
|
$pdfSettings = Setting::getSettings(array_merge(
|
|
['pdf_driver', 'gotenberg_host'],
|
|
self::PAGE_SETTINGS,
|
|
self::PAGE_BOOLEANS,
|
|
));
|
|
|
|
$config = [
|
|
'pdf_driver' => $pdfSettings['pdf_driver'] ?? config('pdf.driver'),
|
|
'gotenberg_host' => $pdfSettings['gotenberg_host'] ?? config('pdf.connections.gotenberg.host'),
|
|
];
|
|
|
|
// Page geometry applies to whichever driver is selected, so it is always
|
|
// returned rather than nested under a driver branch.
|
|
foreach (self::PAGE_SETTINGS as $setting) {
|
|
$config[$setting] = $pdfSettings[$setting] ?? config(self::configKeyFor($setting));
|
|
}
|
|
|
|
foreach (self::PAGE_BOOLEANS as $setting) {
|
|
$config[$setting] = filter_var(
|
|
$pdfSettings[$setting] ?? config(self::configKeyFor($setting)),
|
|
FILTER_VALIDATE_BOOLEAN
|
|
);
|
|
}
|
|
|
|
return response()->json($config);
|
|
}
|
|
|
|
/**
|
|
* Saves the settings
|
|
*
|
|
* @throws AuthorizationException
|
|
*/
|
|
public function saveEnvironment(PDFConfigurationRequest $request): JsonResponse
|
|
{
|
|
$this->authorize('manage pdf config');
|
|
|
|
// Prepare PDF settings for database storage
|
|
$pdfSettings = $this->preparePDFSettingsForDatabase($request);
|
|
|
|
// Save PDF settings to database
|
|
Setting::setSettings($pdfSettings);
|
|
|
|
return response()->json([
|
|
'success' => 'pdf_variables_save_successfully',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Prepare PDF settings for database storage
|
|
*/
|
|
private function preparePDFSettingsForDatabase(PDFConfigurationRequest $request): array
|
|
{
|
|
$driver = $request->get('pdf_driver');
|
|
|
|
$settings = ['pdf_driver' => $driver];
|
|
|
|
// Page geometry is saved for every driver: switching between them should
|
|
// not lose the paper size, which is what happened while it was a
|
|
// Gotenberg-only setting.
|
|
foreach (self::PAGE_SETTINGS as $setting) {
|
|
$settings[$setting] = $request->get($setting);
|
|
}
|
|
|
|
// Only written when the form actually submitted it. Page numbers are a
|
|
// Gotenberg capability and the dompdf form does not render the control,
|
|
// so an unconditional write would clear the operator's choice every time
|
|
// they saved from the other driver.
|
|
foreach (self::PAGE_BOOLEANS as $setting) {
|
|
if ($request->has($setting)) {
|
|
$settings[$setting] = $request->boolean($setting) ? '1' : '0';
|
|
}
|
|
}
|
|
|
|
if ($driver === 'gotenberg') {
|
|
$settings['gotenberg_host'] = $request->get('gotenberg_host');
|
|
}
|
|
|
|
return $settings;
|
|
}
|
|
|
|
/**
|
|
* Maps a settings key onto its config counterpart, e.g.
|
|
* `pdf_margin_top` -> `pdf.page.margin_top`.
|
|
*/
|
|
private static function configKeyFor(string $setting): string
|
|
{
|
|
return 'pdf.page.'.substr($setting, strlen('pdf_'));
|
|
}
|
|
}
|