Integrate PDF conversion
Return PDF responses, configure Node.js deployments, and convert batches.
The converter runs on Node.js and returns PDF bytes, so browser applications convert on a server. The package uses the EigenPal Pro License. For installation, see DOCX to PDF.
Return a PDF response
The following route accepts raw DOCX bytes, not multipart form data, and maps expected errors to HTTP status codes:
import {
exportPdf,
ExportResourceError,
PdfDocumentOpenError,
PdfFidelityError,
PdfOutputLimitError,
PdfPageLimitError,
} from '@docx-editor.dev/docx-to-pdf';
export async function POST(request: Request): Promise<Response> {
try {
const source = new Uint8Array(await request.arrayBuffer());
const result = await exportPdf(source, {
signal: request.signal,
timeoutMs: 30_000,
maxPages: 100,
maxOutputBytes: 16 * 1024 * 1024,
useSystemFonts: false,
});
return new Response(new Uint8Array(result.bytes), {
headers: {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename="document.pdf"',
'Cache-Control': 'no-store',
},
});
} catch (error) {
if (error instanceof PdfDocumentOpenError) {
return Response.json({ error: error.code }, { status: 422 });
}
if (error instanceof PdfFidelityError) {
return Response.json({ error: error.code }, { status: 422 });
}
if (error instanceof PdfPageLimitError || error instanceof PdfOutputLimitError) {
return Response.json({ error: error.code }, { status: 413 });
}
if (error instanceof ExportResourceError && error.code === 'timedOut') {
return Response.json({ error: error.code }, { status: 504 });
}
throw error;
}
}Deploy with Next.js
Place the handler in app/api/convert/route.ts and declare the Node.js runtime:
export const runtime = 'nodejs';Keep the packages external so Node.js resolves their font and WebAssembly assets:
// next.config.mjs
export default {
serverExternalPackages: [
'@docx-editor.dev/docx-to-pdf',
'@docx-editor.dev/core',
'@docx-editor.dev/fonts',
'@docx-editor.dev/fonts-cjk',
],
};- For a standalone deployment, include the
assets/directories of the converter and@docx-editor.dev/fonts, and of@docx-editor.dev/fonts-cjkwhen installed. - Keep the package directory structure intact.
- Test the deployment artifact with a real conversion before release.
Convert a batch
The following loop converts files in sequence, which keeps memory use low:
import { readFile, writeFile } from 'node:fs/promises';
import { exportPdf } from '@docx-editor.dev/docx-to-pdf';
for (const name of ['first', 'second']) {
const source = await readFile(`${name}.docx`);
const result = await exportPdf(source, { useSystemFonts: false });
await writeFile(`${name}.pdf`, result.bytes);
}The exporter disposes its session after each call, including failed calls.
Limits
- Set the server request size limit before
request.arrayBuffer()reads the body. maxOutputByteslimits output, not uploaded bytes or process memory.- Limit concurrent conversions to fit available memory.
- For deadlines and memory isolation, see PDF export API limits.