-
Notifications
You must be signed in to change notification settings - Fork 2
/
make-pdf.ts
25 lines (24 loc) · 1.08 KB
/
make-pdf.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { FontLoader, FontStore } from '../font-loader.ts';
import { ImageLoader, ImageStore } from '../image-loader.ts';
import { layoutPages } from '../layout/layout.ts';
import { readDocumentDefinition } from '../read-document.ts';
import { renderDocument } from '../render/render-document.ts';
import { readAs } from '../types.ts';
import type { DocumentDefinition } from './document.ts';
/**
* Generates a PDF from the given document definition.
*
* @param definition The definition of the document to generate.
* @returns The generated PDF document.
*/
export async function makePdf(definition: DocumentDefinition): Promise<Uint8Array> {
const def = readAs(definition, 'definition', readDocumentDefinition);
const fontLoader = new FontLoader(def.fonts ?? []);
const imageLoader = new ImageLoader(def.images ?? []);
const fontStore = new FontStore(fontLoader);
const imageStore = new ImageStore(imageLoader);
const guides = !!def.dev?.guides;
const ctx = { fontStore, imageStore, guides };
const pages = await layoutPages(def, ctx);
return await renderDocument(def, pages);
}