API route handler
createHtmlToPdfHandler — POST handler that renders HTML with Puppeteer and returns application/pdf.
Default export
import htmlToPdfPostHandler from '@sumx/ssr-pdf/next/html-to-pdf-handler';
export default htmlToPdfPostHandler; // env-based defaultsPrefer createHtmlToPdfHandler when you need custom inline-disposition rules.
Factory
import {
createHtmlToPdfHandler,
htmlToPdfApiRouteConfig,
} from '@sumx/ssr-pdf';
const handler = createHtmlToPdfHandler({
isInlineDispositionAllowed: (req) => {
const host = req.headers.host ?? '';
return host.endsWith('.sumx.app');
},
});
export default handler;
export const config = htmlToPdfApiRouteConfig;HtmlToPdfApiConfig
| Option | Description |
|---|---|
isInlineDispositionAllowed | Required for disposition: 'inline'; default allows SumX dev hosts |
Route config
export const htmlToPdfApiRouteConfig = {
api: {
bodyParser: { sizeLimit: '5mb' },
responseLimit: false,
},
} as const;Portal pattern (Next 16):
export { default } from '@sumx/ssr-pdf/next/html-to-pdf-handler';
export const config = {
api: {
bodyParser: { sizeLimit: '5mb' },
responseLimit: false,
},
};Request flow
- Validate method
POSTand JSON body (request utilities). - Acquire render slot (concurrency cap).
- Launch/reuse Puppeteer,
setContent(html),page.pdf(...). - Release slot; return PDF buffer with
Content-Disposition.
Response headers
Content-Type: application/pdfContent-Disposition: attachmentorinline+ sanitized filename
Errors
| Status | Typical cause |
|---|---|
400 | Invalid body, HTML too large, bad orientation |
403 | inline not allowed for Host |
405 | Non-POST |
500 | Puppeteer/Chromium failure (logged server-side) |
Debugging
Set HTML_TO_PDF_DEBUG=true for structured request logs (see Configuration).