core.medplumclientoptions.createpdf
Home > @medplum/core > MedplumClientOptions > createPdf
MedplumClientOptions.createPdf property
Create PDF implementation.
Default is none, and PDF generation is disabled.
Signature:
createPdf?: CreatePdfFunction;
Example 1
In browser environments, import the client-side pdfmake library.
<script src="pdfmake.min.js"></script>
<script>
async function createPdf(docDefinition, tableLayouts, fonts) {
return new Promise((resolve) => {
pdfMake.createPdf(docDefinition, tableLayouts, fonts).getBlob(resolve);
});
}
</script>
Example 2
In Node.js applications:
import type { CustomTableLayout, TDocumentDefinitions, TFontDictionary } from 'pdfmake/interfaces';
function createPdf(
docDefinition: TDocumentDefinitions,
tableLayouts?: { [name: string]: CustomTableLayout },
fonts?: TFontDictionary
): Promise<Buffer> {
return new Promise((resolve, reject) => {
const printer = new PdfPrinter(fonts ?? {});
const pdfDoc = printer.createPdfKitDocument(docDefinition, { tableLayouts });
const chunks: Uint8Array[] = [];
pdfDoc.on('data', (chunk: Uint8Array) => chunks.push(chunk));
pdfDoc.on('end', () => resolve(Buffer.concat(chunks)));
pdfDoc.on('error', reject);
pdfDoc.end();
});
}