89 lines
3.7 KiB
TypeScript
89 lines
3.7 KiB
TypeScript
import { PdfDefinition } from './pdf-engine';
|
|
|
|
export const PDF_CONFIDENTIAL = 'Confidencial · uso interno';
|
|
export const LOGO_URL = 'assets/branding/isssteleon-logo.png';
|
|
// The logo is 1125 x 358 pixels.
|
|
const LOGO_WIDTH = 110;
|
|
|
|
export const COLORS = { ink: '#081839', accent: '#a8c243', muted: '#7a7a7a', rule: '#e3e7df', band: '#f0f3e8' } as const;
|
|
|
|
export interface PdfTemplateOptions {
|
|
/** Title in the page header and in the document properties. */
|
|
title: string;
|
|
/** Data URL of the institutional logo; without it the header shows the name as text. */
|
|
logo?: string;
|
|
now: Date;
|
|
/** Page size: reports are landscape letter, individual records portrait. */
|
|
orientation: 'landscape' | 'portrait';
|
|
}
|
|
|
|
/** The logo as a data URL, or `undefined` when it cannot be read (the header then falls back to text). */
|
|
export async function loadLogo(url = LOGO_URL): Promise<string | undefined> {
|
|
try {
|
|
const response = await fetch(url);
|
|
if (!response.ok) return undefined;
|
|
const blob = await response.blob();
|
|
return await new Promise<string>((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.onload = () => resolve(String(reader.result));
|
|
reader.onerror = () => reject(reader.error);
|
|
reader.readAsDataURL(blob);
|
|
});
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function formatGenerated(now: Date): string {
|
|
return new Intl.DateTimeFormat('es-MX', { dateStyle: 'medium', timeStyle: 'short', timeZone: 'America/Mexico_City' }).format(now);
|
|
}
|
|
|
|
/**
|
|
* The institutional frame shared by every PDF: header with the logo, and the title on every
|
|
* page, footer with the generation time, the confidentiality line and "Página X de Y", and generic document properties
|
|
* (no author, no user). `content` is what goes between them.
|
|
*/
|
|
export function institutionalDocument(options: PdfTemplateOptions, content: unknown[]): PdfDefinition {
|
|
const margin = 36;
|
|
// Letter is 792 x 612 points; the header rule spans the page between its margins.
|
|
const usableWidth = (options.orientation === 'landscape' ? 792 : 612) - 2 * margin;
|
|
return {
|
|
pageSize: 'LETTER',
|
|
pageOrientation: options.orientation,
|
|
pageMargins: [margin, 84, margin, 52],
|
|
// Named, so the logo is embedded once and not again on every page.
|
|
images: options.logo ? { logo: options.logo } : {},
|
|
info: { title: options.title, author: 'ISSSTELEON', creator: 'ISSSTELEON', producer: 'ISSSTELEON' },
|
|
defaultStyle: { font: 'Roboto', fontSize: 9, color: COLORS.ink },
|
|
styles: {
|
|
h1: { fontSize: 15, bold: true, margin: [0, 0, 0, 6] },
|
|
h2: { fontSize: 11, bold: true, margin: [0, 10, 0, 4] },
|
|
muted: { color: COLORS.muted },
|
|
th: { bold: true, fillColor: COLORS.accent, color: COLORS.ink },
|
|
small: { fontSize: 8, color: COLORS.muted },
|
|
},
|
|
header: () => ({
|
|
margin: [margin, 20, margin, 0],
|
|
stack: [
|
|
{
|
|
columns: [
|
|
options.logo ? { image: 'logo', width: LOGO_WIDTH } : { text: 'ISSSTELEON', bold: true, fontSize: 14 },
|
|
{ text: options.title, alignment: 'center', width: '*', bold: true, fontSize: 12, margin: [0, 8, 0, 0] },
|
|
{ text: '', width: LOGO_WIDTH },
|
|
],
|
|
},
|
|
{ canvas: [{ type: 'line', x1: 0, y1: 8, x2: usableWidth, y2: 8, lineWidth: 1, lineColor: COLORS.accent }] },
|
|
],
|
|
}),
|
|
footer: (currentPage: number, pageCount: number) => ({
|
|
margin: [margin, 14, margin, 0],
|
|
columns: [
|
|
{ text: `Generado: ${formatGenerated(options.now)} (hora de la Ciudad de México)`, style: 'small' },
|
|
{ text: PDF_CONFIDENTIAL, style: 'small', alignment: 'center' },
|
|
{ text: `Página ${currentPage} de ${pageCount}`, style: 'small', alignment: 'right' },
|
|
],
|
|
}),
|
|
content,
|
|
};
|
|
}
|