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
74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { helpers } from '@vuelidate/validators'
|
|
import type { PdfPageSetup } from '@/scripts/api/services/pdf.service'
|
|
|
|
/**
|
|
* Shared bits of the page-setup form, so the dompdf and Gotenberg components
|
|
* validate and seed the same fields the same way rather than drifting apart.
|
|
*/
|
|
|
|
export const PAGE_SETUP_KEYS = [
|
|
'pdf_paper_width',
|
|
'pdf_paper_height',
|
|
'pdf_orientation',
|
|
'pdf_margin_top',
|
|
'pdf_margin_right',
|
|
'pdf_margin_bottom',
|
|
'pdf_margin_left',
|
|
] as const
|
|
|
|
/** Mirrors App\Rules\CssLength, so a bad value is caught before the round trip. */
|
|
const CSS_LENGTH = /^\d+(\.\d+)?(pt|px|pc|mm|cm|in)$/
|
|
|
|
export function cssLength(t: (key: string) => string) {
|
|
return {
|
|
cssLength: helpers.withMessage(t('validation.invalid_length'), (value: string) =>
|
|
!helpers.req(value) ? true : CSS_LENGTH.test(String(value).trim())
|
|
),
|
|
}
|
|
}
|
|
|
|
/** Matches the defaults in config/pdf.php, including dompdf's own 1.2cm margin. */
|
|
export function pageSetupDefaults(): PdfPageSetup {
|
|
return {
|
|
pdf_paper_width: '210mm',
|
|
pdf_paper_height: '297mm',
|
|
pdf_orientation: 'portrait',
|
|
pdf_margin_top: '1.2cm',
|
|
pdf_margin_right: '1.2cm',
|
|
pdf_margin_bottom: '1.2cm',
|
|
pdf_margin_left: '1.2cm',
|
|
pdf_page_numbers: false,
|
|
}
|
|
}
|
|
|
|
/** Pulls the page-setup keys out of the API payload, skipping anything absent. */
|
|
export function pageSetupFrom(configData: Record<string, unknown>): Partial<PdfPageSetup> {
|
|
const setup: Record<string, string | boolean> = {}
|
|
|
|
for (const key of PAGE_SETUP_KEYS) {
|
|
if (typeof configData[key] === 'string' && configData[key]) {
|
|
setup[key] = configData[key] as string
|
|
}
|
|
}
|
|
|
|
if (typeof configData.pdf_page_numbers === 'boolean') {
|
|
setup.pdf_page_numbers = configData.pdf_page_numbers
|
|
}
|
|
|
|
return setup as Partial<PdfPageSetup>
|
|
}
|
|
|
|
/** Flattens Vuelidate state into the shape AdminPdfPageSetup renders. */
|
|
export function pageSetupErrors(
|
|
v$: Record<string, { $error?: boolean; $errors?: { $message: unknown }[] }>
|
|
): Record<string, string | false> {
|
|
const errors: Record<string, string | false> = {}
|
|
|
|
for (const key of PAGE_SETUP_KEYS) {
|
|
const field = v$[key]
|
|
errors[key] = field?.$error ? String(field.$errors?.[0]?.$message ?? '') : false
|
|
}
|
|
|
|
return errors
|
|
}
|