Skip to content

Commit 104ddb0

Browse files
authored
Add option to override page size/margin (#137)
1 parent 69d5d8f commit 104ddb0

1 file changed

Lines changed: 60 additions & 5 deletions

File tree

src/mdast-util-to-docx.ts

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
type IRunOptions,
2121
type IParagraphOptions,
2222
PageBreak,
23+
type ISectionPropertiesOptions,
2324
} from "docx";
2425
import type * as mdast from "mdast";
2526
import { warnOnce } from "./utils";
@@ -171,6 +172,16 @@ export interface DocxOptions extends Pick<
171172
| "styles"
172173
| "background"
173174
> {
175+
/**
176+
* Page size defined in twip (1 twip == 1/1440 inch).
177+
* @default A4 ({@link sectionPageSizeDefaults})
178+
*/
179+
size?: { width?: number; height?: number };
180+
/**
181+
* Page margin defined in twip (1 twip == 1/1440 inch).
182+
* @default 1 inch ({@link sectionMarginDefaults})
183+
*/
184+
margin?: { top?: number; left?: number; bottom?: number; right?: number };
174185
/**
175186
* An option to override the text format of ordered list.
176187
* See https://docx.js.org/#/usage/numbering?id=level-options for more details.
@@ -198,6 +209,8 @@ export const mdastToDocx = async (
198209
keywords,
199210
description,
200211
styles,
212+
size,
213+
margin,
201214
background,
202215
thematicBreak = "page",
203216
orderedListFormat = defaultOrderedList,
@@ -262,6 +275,36 @@ export const mdastToDocx = async (
262275
return null;
263276
};
264277

278+
let { WIDTH: pageWidth, HEIGHT: pageHeight } = sectionPageSizeDefaults;
279+
if (size) {
280+
if (size.width != null) {
281+
pageWidth = size.width;
282+
}
283+
if (size.height != null) {
284+
pageHeight = size.height;
285+
}
286+
}
287+
let {
288+
TOP: marginTop,
289+
LEFT: marginLeft,
290+
BOTTOM: marginBottom,
291+
RIGHT: marginRight,
292+
} = sectionMarginDefaults;
293+
if (margin) {
294+
if (margin.top != null) {
295+
marginTop = margin.top;
296+
}
297+
if (margin.left != null) {
298+
marginLeft = margin.left;
299+
}
300+
if (margin.bottom != null) {
301+
marginBottom = margin.bottom;
302+
}
303+
if (margin.right != null) {
304+
marginRight = margin.right;
305+
}
306+
}
307+
265308
const ctx: Context = {
266309
render(nodes, c) {
267310
const results: DocxContent[] = [];
@@ -273,10 +316,7 @@ export const mdastToDocx = async (
273316
}
274317
return results;
275318
},
276-
width:
277-
sectionPageSizeDefaults.WIDTH -
278-
sectionMarginDefaults.LEFT -
279-
sectionMarginDefaults.RIGHT,
319+
width: pageWidth - marginLeft - marginRight,
280320
deco: {},
281321
indent: 0,
282322
thematicBreak,
@@ -301,6 +341,18 @@ export const mdastToDocx = async (
301341

302342
const orderedLevels = buildLevels(orderedListFormat);
303343

344+
const sectionProperties: ISectionPropertiesOptions = {
345+
page: {
346+
size: { width: pageWidth, height: pageHeight },
347+
margin: {
348+
top: marginTop,
349+
left: marginLeft,
350+
bottom: marginBottom,
351+
right: marginRight,
352+
},
353+
},
354+
};
355+
304356
const doc = new Document({
305357
title,
306358
subject,
@@ -322,7 +374,10 @@ export const mdastToDocx = async (
322374
background,
323375
sections: sections
324376
.filter((s) => s.length)
325-
.map((s) => ({ children: s as DocxChild[] })),
377+
.map((s) => ({
378+
properties: sectionProperties,
379+
children: s as DocxChild[],
380+
})),
326381
footnotes: footnote.toConfig(),
327382
numbering: {
328383
config: [

0 commit comments

Comments
 (0)