Skip to content

Commit 47b0a16

Browse files
committed
Correctly set margin when pageSize is set
1 parent e8b718e commit 47b0a16

File tree

5 files changed

+60
-38
lines changed

5 files changed

+60
-38
lines changed

Diff for: dist/index-cjs.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -57198,7 +57198,7 @@ class DocxDocument {
5719857198
} else {
5719957199
throw new Error("Document XML must be created before importing");
5720057200
}
57201-
const sectPr = import_xmlbuilder23.fragment({ namespaceAlias: { w: namespaces_default.w } }).ele("@w", "sectPr").ele("@w", "pgSz").att("@w", "w", String(this.width)).att("@w", "h", String(this.height)).att("@w", "orient", this.orientation).up().ele("@w", "pgMar").att("@w", "top", String(this.margins.top)).att("@w", "right", String(this.margins.right)).att("@w", "bottom", String(this.margins.bottom)).att("@w", "left", String(this.margins.left)).att("@w", "header", String(this.margins.header)).att("@w", "footer", String(this.margins.footer)).att("@w", "gutter", String(this.margins.gutter)).up().up();
57201+
const sectPr = import_xmlbuilder23.fragment({ namespaceAlias: { w: namespaces_default.w } }).ele("@w", "sectPr").ele("@w", "pgSz").att("@w", "w", String(this.width)).att("@w", "h", String(this.height)).att("@w", "orient", this.orientation).up().ele("@w", "pgMar").att("@w", "top", String(this.margins?.top || 0)).att("@w", "right", String(this.margins?.right || 0)).att("@w", "bottom", String(this.margins?.bottom || 0)).att("@w", "left", String(this.margins?.left || 0)).att("@w", "header", String(this.margins?.header || 0)).att("@w", "footer", String(this.margins?.footer || 0)).att("@w", "gutter", String(this.margins?.gutter || 0)).up().up();
5720257202
body.import(sectPr);
5720357203
generateSectionReferenceXML(documentXML, "header", this.headerObjects, this.header);
5720457204
generateSectionReferenceXML(documentXML, "footer", this.footerObjects, this.footer);
@@ -57462,15 +57462,6 @@ async function addFilesToContainer(zip, htmlString, suppliedDocumentOptions, hea
5746257462
zip,
5746357463
htmlString,
5746457464
orientation: documentOptions.orientation || "portrait",
57465-
margins: documentOptions.margins || {
57466-
top: 0,
57467-
right: 0,
57468-
bottom: 0,
57469-
left: 0,
57470-
header: 0,
57471-
footer: 0,
57472-
gutter: 0
57473-
},
5747457465
table: documentOptions.table || { row: { cantSplit: false } },
5747557466
numbering: documentOptions.numbering || { defaultOrderedListStyleType: "decimal" }
5747657467
});

Diff for: dist/index-esm.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -57161,7 +57161,7 @@ class DocxDocument {
5716157161
} else {
5716257162
throw new Error("Document XML must be created before importing");
5716357163
}
57164-
const sectPr = import_xmlbuilder23.fragment({ namespaceAlias: { w: namespaces_default.w } }).ele("@w", "sectPr").ele("@w", "pgSz").att("@w", "w", String(this.width)).att("@w", "h", String(this.height)).att("@w", "orient", this.orientation).up().ele("@w", "pgMar").att("@w", "top", String(this.margins.top)).att("@w", "right", String(this.margins.right)).att("@w", "bottom", String(this.margins.bottom)).att("@w", "left", String(this.margins.left)).att("@w", "header", String(this.margins.header)).att("@w", "footer", String(this.margins.footer)).att("@w", "gutter", String(this.margins.gutter)).up().up();
57164+
const sectPr = import_xmlbuilder23.fragment({ namespaceAlias: { w: namespaces_default.w } }).ele("@w", "sectPr").ele("@w", "pgSz").att("@w", "w", String(this.width)).att("@w", "h", String(this.height)).att("@w", "orient", this.orientation).up().ele("@w", "pgMar").att("@w", "top", String(this.margins?.top || 0)).att("@w", "right", String(this.margins?.right || 0)).att("@w", "bottom", String(this.margins?.bottom || 0)).att("@w", "left", String(this.margins?.left || 0)).att("@w", "header", String(this.margins?.header || 0)).att("@w", "footer", String(this.margins?.footer || 0)).att("@w", "gutter", String(this.margins?.gutter || 0)).up().up();
5716557165
body.import(sectPr);
5716657166
generateSectionReferenceXML(documentXML, "header", this.headerObjects, this.header);
5716757167
generateSectionReferenceXML(documentXML, "footer", this.footerObjects, this.footer);
@@ -57425,15 +57425,6 @@ async function addFilesToContainer(zip, htmlString, suppliedDocumentOptions, hea
5742557425
zip,
5742657426
htmlString,
5742757427
orientation: documentOptions.orientation || "portrait",
57428-
margins: documentOptions.margins || {
57429-
top: 0,
57430-
right: 0,
57431-
bottom: 0,
57432-
left: 0,
57433-
header: 0,
57434-
footer: 0,
57435-
gutter: 0
57436-
},
5743757428
table: documentOptions.table || { row: { cantSplit: false } },
5743857429
numbering: documentOptions.numbering || { defaultOrderedListStyleType: "decimal" }
5743957430
});

Diff for: src/docx-document.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export default class DocxDocument {
174174
pageSize?: { height: number; width: number }
175175
width: number
176176
height: number
177-
margins: Margins
177+
margins?: Margins
178178
availableDocumentSpace: number
179179
title: string
180180
subject: string
@@ -253,7 +253,7 @@ export default class DocxDocument {
253253
htmlString: string
254254
orientation: "portrait" | "landscape"
255255
pageSize?: { height: number; width: number }
256-
margins: Margins
256+
margins?: Margins
257257
title?: string
258258
subject?: string
259259
creator?: string
@@ -427,13 +427,13 @@ export default class DocxDocument {
427427
.att("@w", "orient", this.orientation)
428428
.up()
429429
.ele("@w", "pgMar")
430-
.att("@w", "top", String(this.margins.top))
431-
.att("@w", "right", String(this.margins.right))
432-
.att("@w", "bottom", String(this.margins.bottom))
433-
.att("@w", "left", String(this.margins.left))
434-
.att("@w", "header", String(this.margins.header))
435-
.att("@w", "footer", String(this.margins.footer))
436-
.att("@w", "gutter", String(this.margins.gutter))
430+
.att("@w", "top", String(this.margins?.top || 0))
431+
.att("@w", "right", String(this.margins?.right || 0))
432+
.att("@w", "bottom", String(this.margins?.bottom || 0))
433+
.att("@w", "left", String(this.margins?.left || 0))
434+
.att("@w", "header", String(this.margins?.header || 0))
435+
.att("@w", "footer", String(this.margins?.footer || 0))
436+
.att("@w", "gutter", String(this.margins?.gutter || 0))
437437
.up()
438438
.up()
439439
body.import(sectPr)

Diff for: src/html-to-docx.ts

-9
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,6 @@ export default async function addFilesToContainer(
170170
zip,
171171
htmlString,
172172
orientation: documentOptions.orientation || "portrait",
173-
margins: documentOptions.margins || {
174-
top: 0,
175-
right: 0,
176-
bottom: 0,
177-
left: 0,
178-
header: 0,
179-
footer: 0,
180-
gutter: 0,
181-
},
182173
table: documentOptions.table || { row: { cantSplit: false } },
183174
numbering: documentOptions.numbering ||
184175
{ defaultOrderedListStyleType: "decimal" },

Diff for: tests/document-width.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import assert from "assert"
2+
import JSZip from "jszip"
3+
import { test } from "node:test"
4+
5+
import htmlToDocx from "../index.ts"
6+
import writeFile from "./write-file.ts"
7+
8+
const createdAt = new Date("2025-01-01")
9+
10+
test("correctly set margin if pageSize is set", async () => {
11+
const simpleHtml = `
12+
<html>
13+
<title>Document Width/Margin Test</title>
14+
<body>
15+
<p>Some text</p>
16+
</body>
17+
</html>
18+
`
19+
const docxContent = await htmlToDocx(
20+
simpleHtml,
21+
null,
22+
{
23+
createdAt,
24+
modifiedAt: createdAt,
25+
pageSize: { // Din A4 in TWIP
26+
width: 11918,
27+
height: 16850,
28+
},
29+
},
30+
null,
31+
)
32+
await writeFile(docxContent, "tests/_tmp_document-width.docx")
33+
34+
const zip = new JSZip()
35+
const zipContent = await zip.loadAsync(docxContent)
36+
37+
assert.ok("word/document.xml" in zipContent.files)
38+
39+
const docXml = await zipContent.file("word/document.xml")
40+
?.async("string") || ""
41+
42+
const sizeMatches = docXml.match(/w:w="(\d+)" w:h="(\d+)"/)
43+
assert.strictEqual(sizeMatches?.[1], "11918")
44+
assert.strictEqual(sizeMatches?.[2], "16850")
45+
46+
const marginMatches = docXml.match(/<w:pgMar w:top="(\d+)" w:right="(\d+)"/)
47+
assert.strictEqual(marginMatches?.[1], "1440")
48+
assert.strictEqual(marginMatches?.[2], "1800")
49+
})

0 commit comments

Comments
 (0)