feat(issued-orders-pdf): true per-page footer (Vystavil + Strana X z Y), sections flow inline
The PDF now carries a real repeating footer on EVERY page via Puppeteer's footerTemplate (Chromium has no CSS margin-box support, so this is the only true footer): 'Vystavil: <logged-in user>' on the left, 'Strana X z Y' / 'Page X of Y' centered, localized by the document language. htmlToPdf gained an optional footerTemplate option (empty header suppresses Chromium's default; bottom margin grows to 18mm). The old body footer pinned by flex min-height is gone. The 'Obsah' sections no longer start a new page with a repeated header - they flow inline right after the items/totals block and paginate naturally (break-inside: avoid per section). Applied at all three render sites (PDF route, ?save=1 archive, /file fallback). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -212,13 +212,37 @@ function stripTags(html: string | null | undefined): string {
|
||||
return (html || "").replace(/<[^>]*>/g, "").trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeating per-page footer for the issued-order PDF, rendered by Puppeteer
|
||||
* in the bottom page margin (the only true repeating footer Chromium
|
||||
* supports): "Vystavil: <name>" left, "Strana X z Y" / "Page X of Y"
|
||||
* (by document language) centered. Styles must stay inline and the
|
||||
* font-size explicit — Chromium defaults footer text to 0.
|
||||
*/
|
||||
export function buildIssuedOrderPdfFooter(
|
||||
lang: Lang,
|
||||
issuerName: string,
|
||||
): string {
|
||||
const t = translations[lang];
|
||||
const pageWord = lang === "cs" ? "Strana" : "Page";
|
||||
const ofWord = lang === "cs" ? "z" : "of";
|
||||
return `<div style="width:100%; font-size:8pt; font-family:Tahoma, sans-serif; color:#646464; padding:0 10mm; box-sizing:border-box; display:flex; align-items:center;">
|
||||
<div style="flex:1; text-align:left;"><span style="font-weight:600;">${escapeHtml(t.issued_by)}</span> ${escapeHtml(issuerName)}</div>
|
||||
<div style="flex:1; text-align:center;">${pageWord} <span class="pageNumber"></span> ${ofWord} <span class="totalPages"></span></div>
|
||||
<div style="flex:1;"></div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
export function renderIssuedOrderHtml(
|
||||
order: IssuedOrderPdfData,
|
||||
items: IssuedOrderPdfItem[],
|
||||
supplier: Record<string, unknown> | null,
|
||||
settings: Record<string, unknown> | null,
|
||||
lang: Lang,
|
||||
issuer: { name: string },
|
||||
// Kept for signature stability across the many call sites: the issuer no
|
||||
// longer renders in the BODY — it prints in the Puppeteer footer template
|
||||
// (buildIssuedOrderPdfFooter) on every page.
|
||||
_issuer: { name: string },
|
||||
sections: IssuedOrderPdfSection[] = [],
|
||||
): string {
|
||||
const t = translations[lang];
|
||||
@@ -283,19 +307,20 @@ export function renderIssuedOrderHtml(
|
||||
// Quill indent CSS
|
||||
const indentCSS = buildQuillIndentCss();
|
||||
|
||||
// ── Sections (scope) page — mirrors offers-pdf's .scope-page, but repeats
|
||||
// THIS template's red PO header (logo + heading + dates), not the offers'
|
||||
// monochrome one. The whole page is suppressed when every section is empty
|
||||
// (tag-stripped check, so an empty-Quill "<p><br></p>" doesn't count).
|
||||
// cs prefers the Czech title when present; en (or a missing Czech title)
|
||||
// falls back to the EN title (same rule as offers-pdf).
|
||||
// ── Sections ("Obsah") — flow INLINE after the items/totals block (user
|
||||
// decision: no separate page, no repeated header). Each section keeps
|
||||
// break-inside: avoid; long content paginates naturally and the Puppeteer
|
||||
// footer template stamps every page. Suppressed entirely when every
|
||||
// section is empty (tag-stripped check, so an empty-Quill "<p><br></p>"
|
||||
// doesn't count). cs prefers the Czech title when present; en (or a
|
||||
// missing Czech title) falls back to the EN title (same rule as offers).
|
||||
const resolveSectionTitle = (s: IssuedOrderPdfSection): string =>
|
||||
lang === "cs" && (s.title_cz || "").trim()
|
||||
? s.title_cz || ""
|
||||
: s.title || "";
|
||||
// Visibility must use the SAME per-language title rule as the render loop —
|
||||
// otherwise a section with only the other language's title would count as
|
||||
// content and produce a scope page holding nothing but the header.
|
||||
// content and produce an empty sections block.
|
||||
const visibleSections = sections.filter(
|
||||
(s) => resolveSectionTitle(s).trim() || stripTags(s.content),
|
||||
);
|
||||
@@ -303,20 +328,7 @@ export function renderIssuedOrderHtml(
|
||||
|
||||
let scopeHtml = "";
|
||||
if (hasSectionContent) {
|
||||
const scopeDates = `<div class="scope-dates"><span class="lbl">${escapeHtml(t.issue_date)}</span> <span class="val">${escapeHtml(issueDateStr) || "—"}</span>${
|
||||
deliveryDateStr
|
||||
? ` <span class="sep">·</span> <span class="lbl">${escapeHtml(t.delivery_date)}</span> <span class="val">${escapeHtml(deliveryDateStr)}</span>`
|
||||
: ""
|
||||
}</div>`;
|
||||
scopeHtml += '<div class="scope-page">';
|
||||
scopeHtml += `
|
||||
<div class="invoice-header">
|
||||
<div class="left">
|
||||
${logoImg ? `<div class="logo-header">${logoImg}</div>` : ""}
|
||||
</div>
|
||||
<div class="invoice-title">${escapeHtml(t.heading)} ${poNumber}</div>
|
||||
</div>
|
||||
${scopeDates}`;
|
||||
scopeHtml += '<div class="scope-sections">';
|
||||
for (const section of visibleSections) {
|
||||
const title = resolveSectionTitle(section);
|
||||
const content = (section.content || "").trim();
|
||||
@@ -348,15 +360,9 @@ export function renderIssuedOrderHtml(
|
||||
width: 186mm;
|
||||
}
|
||||
|
||||
.invoice-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: calc(297mm - 27mm);
|
||||
}
|
||||
.invoice-content { flex: 1 1 auto; }
|
||||
.invoice-footer {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
/* The repeating "Vystavil / Strana X z Y" footer lives in Puppeteer's
|
||||
footerTemplate (bottom page margin), NOT in the body — the old flex
|
||||
min-height pinning is gone with it. */
|
||||
|
||||
.accent { color: #de3a3a; }
|
||||
|
||||
@@ -561,14 +567,6 @@ export function renderIssuedOrderHtml(
|
||||
border-bottom: 2.5pt solid #de3a3a;
|
||||
padding-bottom: 1mm;
|
||||
}
|
||||
/* Vystavil */
|
||||
.issued-by {
|
||||
font-size: 9pt;
|
||||
margin: 2mm 0;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.issued-by .lbl { font-weight: 600; }
|
||||
|
||||
/* Upozorneni */
|
||||
.notice {
|
||||
font-size: 8pt;
|
||||
@@ -577,18 +575,12 @@ export function renderIssuedOrderHtml(
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
/* Sekce (scope) na vlastni strance — zrcadli offers-pdf .scope-page,
|
||||
hlavicka se opakuje v cervenem stylu teto sablony */
|
||||
.scope-page {
|
||||
page-break-before: always;
|
||||
/* Sekce ("Obsah") — tecou inline za tabulkou polozek; zadna nova stranka,
|
||||
dlouhy obsah strankuje prirozene a paticku tiskne Puppeteer na kazdou
|
||||
stranu. */
|
||||
.scope-sections {
|
||||
margin-top: 6mm;
|
||||
}
|
||||
.scope-dates {
|
||||
font-size: 9pt;
|
||||
color: #666;
|
||||
margin: 1mm 0 4mm 0;
|
||||
}
|
||||
.scope-dates .val { font-weight: 600; color: #1a1a1a; }
|
||||
.scope-dates .sep { color: #d0d0d0; }
|
||||
.scope-section {
|
||||
width: 100%;
|
||||
max-width: 100%;
|
||||
@@ -644,7 +636,7 @@ ${indentCSS}
|
||||
min-height: 100vh;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
.invoice-page, .scope-page {
|
||||
.invoice-page {
|
||||
width: 210mm;
|
||||
min-height: 297mm;
|
||||
padding: 15mm;
|
||||
@@ -720,17 +712,11 @@ ${indentCSS}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
${scopeHtml}
|
||||
|
||||
</div><!-- /.invoice-content -->
|
||||
<div class="invoice-footer">
|
||||
|
||||
<!-- Vystavil (jmeno prihlaseneho uzivatele) -->
|
||||
<div class="issued-by"><span class="lbl">${escapeHtml(t.issued_by)}</span> ${escapeHtml(issuer.name)}</div>
|
||||
|
||||
</div><!-- /.invoice-footer -->
|
||||
</div><!-- /.invoice-page -->
|
||||
|
||||
${scopeHtml}
|
||||
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
@@ -808,7 +794,9 @@ export default async function issuedOrdersPdfRoutes(fastify: FastifyInstance) {
|
||||
? new Date(order.order_date)
|
||||
: new Date();
|
||||
nasOrdersManager.cleanIssued(order.po_number);
|
||||
const pdfBuffer = await htmlToPdf(html);
|
||||
const pdfBuffer = await htmlToPdf(html, {
|
||||
footerTemplate: buildIssuedOrderPdfFooter(lang, issuer.name),
|
||||
});
|
||||
nasOrdersManager.saveIssuedPdf(
|
||||
order.po_number,
|
||||
baseDate.getFullYear(),
|
||||
@@ -818,7 +806,9 @@ export default async function issuedOrdersPdfRoutes(fastify: FastifyInstance) {
|
||||
return success(reply, null, 200, "PDF uloženo");
|
||||
}
|
||||
|
||||
const pdf = await htmlToPdf(html);
|
||||
const pdf = await htmlToPdf(html, {
|
||||
footerTemplate: buildIssuedOrderPdfFooter(lang, issuer.name),
|
||||
});
|
||||
const filename = `Objednavka-${order.po_number || String(id)}.pdf`;
|
||||
return reply
|
||||
.type("application/pdf")
|
||||
|
||||
@@ -18,7 +18,10 @@ import {
|
||||
deleteIssuedOrder,
|
||||
getNextIssuedOrderNumberPreview,
|
||||
} from "../../services/issued-orders.service";
|
||||
import { renderIssuedOrderHtml } from "./issued-orders-pdf";
|
||||
import {
|
||||
renderIssuedOrderHtml,
|
||||
buildIssuedOrderPdfFooter,
|
||||
} from "./issued-orders-pdf";
|
||||
import { htmlToPdf } from "../../utils/html-to-pdf";
|
||||
import { nasOrdersManager } from "../../services/nas-financials-manager";
|
||||
import { contentDisposition } from "../../utils/content-disposition";
|
||||
@@ -306,7 +309,9 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
||||
issuer,
|
||||
sections,
|
||||
);
|
||||
const pdf = await htmlToPdf(html);
|
||||
const pdf = await htmlToPdf(html, {
|
||||
footerTemplate: buildIssuedOrderPdfFooter(lang, issuer.name),
|
||||
});
|
||||
|
||||
if (nasOrdersManager.isConfigured()) {
|
||||
const baseDate = order.order_date
|
||||
|
||||
Reference in New Issue
Block a user