Skip to content

Commit 2eb3dc5

Browse files
committed
⚗️ Support render-document hook
To enable experimentation with custom rendering logic, this commit adds support for an undocumented `onRenderDocument` hook in the document definition.
1 parent 0e95464 commit 2eb3dc5

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/read-document.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ describe('readDocumentDefinition', () => {
113113
]);
114114
});
115115

116+
it(`includes onRenderDocument hook`, () => {
117+
const onRenderDocument = () => {};
118+
const def = readDocumentDefinition({ ...input, onRenderDocument });
119+
120+
expect(def.onRenderDocument).toBe(onRenderDocument);
121+
});
122+
116123
(['header', 'footer'] as const).forEach((name) => {
117124
it(`supports dynamic ${name}`, () => {
118125
const defaultStyle = { fontSize: 23 };

src/read-document.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { PDFDocument } from 'pdf-lib';
2+
13
import type { BoxEdges, Size } from './box.ts';
24
import { parseEdges } from './box.ts';
35
import type { FontDef } from './fonts.ts';
@@ -31,6 +33,7 @@ export type DocumentDefinition = {
3133
creationDate?: Date;
3234
modificationDate?: Date;
3335
}[];
36+
onRenderDocument?: (pdfDoc: PDFDocument) => void;
3437
};
3538

3639
export type Metadata = {
@@ -61,6 +64,7 @@ export function readDocumentDefinition(input: unknown): DocumentDefinition {
6164
dev: optional(types.object({ guides: optional(types.boolean()) })),
6265
customData: optional(readCustomData),
6366
embeddedFiles: optional(types.array(readEmbeddedFiles)),
67+
onRenderDocument: optional(),
6468
});
6569
const tBlock = (block: unknown) => readBlock(block, def1.defaultStyle);
6670
const def2 = readObject(input, {

src/render/render-document.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,20 @@ describe('renderDocument', () => {
6868
expect(lookup('XXFoo').getContentsString()).toBe('Foo');
6969
expect(lookup('XXBar').getContents()).toEqual(Uint8Array.of(1, 2, 3));
7070
});
71+
72+
it('calls custom render hook', async () => {
73+
const def = {
74+
content: [],
75+
onRenderDocument: (pdfDoc: PDFDocument) => {
76+
pdfDoc.setTitle('Test Title');
77+
},
78+
};
79+
80+
const pdfData = await renderDocument(def, []);
81+
82+
const pdfDoc = await PDFDocument.load(pdfData, { updateMetadata: false });
83+
const infoDict = pdfDoc.context.lookup(pdfDoc.context.trailerInfo.Info) as PDFDict;
84+
const getInfo = (name: string) => infoDict.get(PDFName.of(name));
85+
expect(getInfo('Title')).toEqual(PDFHexString.fromText('Test Title'));
86+
});
7187
});

src/render/render-document.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export async function renderDocument(def: DocumentDefinition, pages: Page[]): Pr
2222
});
2323
}
2424

25+
def.onRenderDocument?.(pdfDoc);
2526
const idInfo = {
2627
creator: 'pdfmkr',
2728
time: new Date().toISOString(),

0 commit comments

Comments
 (0)