Request utilities
Pure functions for validating and normalizing PDF API input.
Module: @sumx/ssr-pdf/html-to-pdf-request (also re-exported from barrel).
Types
type HtmlToPdfRequestBody = {
html?: string;
filename?: string;
orientation?: 'portrait' | 'landscape';
disposition?: 'attachment' | 'inline';
pdfOptions?: {
format?: PaperFormat; // e.g. 'A4', 'Letter'
landscape?: boolean;
printBackground?: boolean;
preferCSSPageSize?: boolean;
margin?: PdfMarginInput;
};
};
type PdfMarginInput = {
top?: string | number;
right?: string | number;
bottom?: string | number;
left?: string | number;
};Constants
| Constant | Value |
|---|---|
MAX_HTML_TO_PDF_LENGTH | 2_000_000 characters |
DEFAULT_PDF_FILENAME | document.pdf |
Functions
| Function | Description |
|---|---|
normalizePdfMargin(margin?) | CSS lengths for Puppeteer margins |
resolvePdfLandscape(orientation, pdfOptions?) | Final landscape flag |
sanitizePdfFilename(value?) | Strip unsafe chars; ensure .pdf suffix |
isValidPdfOrientation(value) | Type guard for orientation |
Example body
{
"html": "<html><body><h1>Invoice</h1></body></html>",
"filename": "invoice-1042.pdf",
"orientation": "portrait",
"disposition": "attachment",
"pdfOptions": {
"format": "A4",
"printBackground": true,
"margin": { top: "20mm", bottom: "20mm" }
}
}Client call (portal)
const res = await fetch('/api/html-to-pdf', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ html, filename: 'report.pdf' }),
});
const blob = await res.blob();Keep HTML generation server-side or sanitize any user HTML before POST.
Filename rules
sanitizePdfFilename:
- Replaces
/ \ ? % * : | " < >with_ - Appends
.pdfif missing - Falls back to
document.pdf