From d8b60d82f33b64b2c3223ee670713fe4f7d7517f Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 7 Aug 2019 22:12:14 +0100 Subject: [PATCH 1/2] Turn Packer static --- demo/1-basic.ts | 4 +-- demo/10-my-cv.ts | 4 +-- demo/11-declaritive-styles-2.ts | 4 +-- demo/12-scaling-images.ts | 4 +-- demo/13-xml-styles.ts | 4 +-- demo/14-page-numbers.ts | 4 +-- demo/15-page-break-before.ts | 4 +-- demo/16-multiple-sections.ts | 4 +-- demo/17-footnotes.ts | 4 +-- demo/18-image-from-buffer.ts | 4 +-- demo/19-export-to-base64.ts | 4 +-- demo/2-declaritive-styles.ts | 4 +-- demo/20-table-cell-borders.ts | 4 +-- demo/21-bookmarks.ts | 4 +-- demo/22-right-to-left-text.ts | 4 +-- demo/23-base64-images.ts | 4 +-- demo/24-images-to-table-cell.ts | 4 +-- demo/26-paragraph-borders.ts | 4 +-- demo/27-declaritive-styles-3.ts | 4 +-- demo/28-table-of-contents.ts | 5 +-- demo/29-numbered-lists.ts | 4 +-- demo/3-numbering-and-bullet-points.ts | 4 +-- demo/30-template-document.ts | 3 +- demo/31-tables.ts | 4 +-- demo/32-merge-table-cells.ts | 4 +-- demo/33-sequential-captions.ts | 4 +-- demo/34-floating-tables.ts | 4 +-- demo/35-hyperlinks.ts | 4 +-- demo/36-image-to-table-cell.ts | 4 +-- demo/37-images-to-header-and-footer.ts | 4 +-- demo/38-text-wrapping.ts | 4 +-- demo/39-page-numbers.ts | 4 +-- demo/4-basic-table.ts | 4 +-- demo/40-line-numbers.ts | 4 +-- demo/41-merge-table-cells-2.ts | 4 +-- demo/43-images-to-table-cell-2.ts | 4 +-- demo/44-multiple-columns.ts | 4 +-- demo/45-highlighting-text.ts | 4 +-- demo/46-shading-text.ts | 4 +-- demo/5-images.ts | 4 +-- demo/6-page-borders.ts | 4 +-- demo/7-landscape.ts | 4 +-- demo/8-header-footer.ts | 4 +-- demo/9-images-in-header-and-footer.ts | 4 +-- demo/browser-demo.html | 4 +-- docs/README.md | 4 +-- docs/usage/packers.md | 37 ++++++++++++++++++++++- docs/usage/styling-with-js.md | 3 +- src/export/packer/next-compiler.ts | 42 ++++++++++++-------------- src/export/packer/packer.spec.ts | 15 ++++----- src/export/packer/packer.ts | 20 +++++------- 51 files changed, 118 insertions(+), 183 deletions(-) diff --git a/demo/1-basic.ts b/demo/1-basic.ts index 2787c869d7..7db8870259 100644 --- a/demo/1-basic.ts +++ b/demo/1-basic.ts @@ -24,8 +24,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/10-my-cv.ts b/demo/10-my-cv.ts index f7a89a0512..6c60be4f48 100644 --- a/demo/10-my-cv.ts +++ b/demo/10-my-cv.ts @@ -336,8 +336,6 @@ const documentCreator = new DocumentCreator(); const doc = documentCreator.create([experiences, education, skills, achievements]); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/11-declaritive-styles-2.ts b/demo/11-declaritive-styles-2.ts index e286ed7556..bb6428d518 100644 --- a/demo/11-declaritive-styles-2.ts +++ b/demo/11-declaritive-styles-2.ts @@ -163,8 +163,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/12-scaling-images.ts b/demo/12-scaling-images.ts index 2de556a262..6de5f49b92 100644 --- a/demo/12-scaling-images.ts +++ b/demo/12-scaling-images.ts @@ -14,8 +14,6 @@ doc.addSection({ children: [new Paragraph("Hello World"), new Paragraph(image), new Paragraph(image2), new Paragraph(image3), new Paragraph(image4)], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/13-xml-styles.ts b/demo/13-xml-styles.ts index 9d84777f68..1a4f60c24a 100644 --- a/demo/13-xml-styles.ts +++ b/demo/13-xml-styles.ts @@ -27,8 +27,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/14-page-numbers.ts b/demo/14-page-numbers.ts index f8e024547c..fc9a20169d 100644 --- a/demo/14-page-numbers.ts +++ b/demo/14-page-numbers.ts @@ -32,8 +32,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/15-page-break-before.ts b/demo/15-page-break-before.ts index 73ab48680e..bfb56fd7dd 100644 --- a/demo/15-page-break-before.ts +++ b/demo/15-page-break-before.ts @@ -15,8 +15,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/16-multiple-sections.ts b/demo/16-multiple-sections.ts index ede5e4058e..10e2098704 100644 --- a/demo/16-multiple-sections.ts +++ b/demo/16-multiple-sections.ts @@ -107,8 +107,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/17-footnotes.ts b/demo/17-footnotes.ts index e316ea4edb..3920d0dce8 100644 --- a/demo/17-footnotes.ts +++ b/demo/17-footnotes.ts @@ -12,8 +12,6 @@ doc.addSection({ doc.createFootnote(new Paragraph("Test")); doc.createFootnote(new Paragraph("My amazing reference")); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/18-image-from-buffer.ts b/demo/18-image-from-buffer.ts index 7eefdc4328..146b1b421f 100644 --- a/demo/18-image-from-buffer.ts +++ b/demo/18-image-from-buffer.ts @@ -17,8 +17,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/19-export-to-base64.ts b/demo/19-export-to-base64.ts index 84991cc929..676d844c8d 100644 --- a/demo/19-export-to-base64.ts +++ b/demo/19-export-to-base64.ts @@ -23,8 +23,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBase64String(doc).then((str) => { +Packer.toBase64String(doc).then((str) => { fs.writeFileSync("My Document.docx", str); }); diff --git a/demo/2-declaritive-styles.ts b/demo/2-declaritive-styles.ts index 3397f41ee8..a854b04546 100644 --- a/demo/2-declaritive-styles.ts +++ b/demo/2-declaritive-styles.ts @@ -117,8 +117,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/20-table-cell-borders.ts b/demo/20-table-cell-borders.ts index ee2b7918e2..13809a6164 100644 --- a/demo/20-table-cell-borders.ts +++ b/demo/20-table-cell-borders.ts @@ -19,8 +19,6 @@ table .addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green") .addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000"); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/21-bookmarks.ts b/demo/21-bookmarks.ts index 1193ee01c0..7a8e7b523b 100644 --- a/demo/21-bookmarks.ts +++ b/demo/21-bookmarks.ts @@ -30,8 +30,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/22-right-to-left-text.ts b/demo/22-right-to-left-text.ts index 3a70c01d21..2c3984fb46 100644 --- a/demo/22-right-to-left-text.ts +++ b/demo/22-right-to-left-text.ts @@ -39,8 +39,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/23-base64-images.ts b/demo/23-base64-images.ts index 5333d91e9b..c927ba45ef 100644 --- a/demo/23-base64-images.ts +++ b/demo/23-base64-images.ts @@ -27,8 +27,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/24-images-to-table-cell.ts b/demo/24-images-to-table-cell.ts index 2ea6f664c3..114be5491e 100644 --- a/demo/24-images-to-table-cell.ts +++ b/demo/24-images-to-table-cell.ts @@ -19,8 +19,6 @@ table.getCell(2, 2).add(new Paragraph("Hello")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); table.getCell(1, 1).add(new Paragraph(image)); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/26-paragraph-borders.ts b/demo/26-paragraph-borders.ts index 421d8477e5..40057d756a 100644 --- a/demo/26-paragraph-borders.ts +++ b/demo/26-paragraph-borders.ts @@ -28,8 +28,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/27-declaritive-styles-3.ts b/demo/27-declaritive-styles-3.ts index a31ce0b44b..31994dfe4a 100644 --- a/demo/27-declaritive-styles-3.ts +++ b/demo/27-declaritive-styles-3.ts @@ -37,8 +37,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/28-table-of-contents.ts b/demo/28-table-of-contents.ts index 414c2ad97d..83160b0177 100644 --- a/demo/28-table-of-contents.ts +++ b/demo/28-table-of-contents.ts @@ -48,12 +48,9 @@ doc.addSection({ style: "MySpectacularStyle", pageBreakBefore: true, }), - ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/29-numbered-lists.ts b/demo/29-numbered-lists.ts index 31dcd6d28f..740320e5f8 100644 --- a/demo/29-numbered-lists.ts +++ b/demo/29-numbered-lists.ts @@ -61,8 +61,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/3-numbering-and-bullet-points.ts b/demo/3-numbering-and-bullet-points.ts index 79545c75ec..94d5665568 100644 --- a/demo/3-numbering-and-bullet-points.ts +++ b/demo/3-numbering-and-bullet-points.ts @@ -71,8 +71,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/30-template-document.ts b/demo/30-template-document.ts index e9ebfd5c2b..3b3c724b5c 100644 --- a/demo/30-template-document.ts +++ b/demo/30-template-document.ts @@ -23,8 +23,7 @@ fs.readFile(filePath, (err, data) => { children: [new Paragraph("Hello World")], }); - const packer = new Packer(); - packer.toBuffer(doc).then((buffer) => { + Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); }); diff --git a/demo/31-tables.ts b/demo/31-tables.ts index d3e4cd3a8a..050403bb99 100644 --- a/demo/31-tables.ts +++ b/demo/31-tables.ts @@ -27,8 +27,6 @@ doc.addSection({ children: [table], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/32-merge-table-cells.ts b/demo/32-merge-table-cells.ts index 8526dc4916..cd850067e5 100644 --- a/demo/32-merge-table-cells.ts +++ b/demo/32-merge-table-cells.ts @@ -108,8 +108,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/33-sequential-captions.ts b/demo/33-sequential-captions.ts index bf0913d6f1..c06c3aa1cc 100644 --- a/demo/33-sequential-captions.ts +++ b/demo/33-sequential-captions.ts @@ -26,8 +26,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/34-floating-tables.ts b/demo/34-floating-tables.ts index c24246b709..c5b8a18cf8 100644 --- a/demo/34-floating-tables.ts +++ b/demo/34-floating-tables.ts @@ -36,8 +36,6 @@ doc.addSection({ children: [table], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/35-hyperlinks.ts b/demo/35-hyperlinks.ts index 41117a6a51..c547d93b36 100644 --- a/demo/35-hyperlinks.ts +++ b/demo/35-hyperlinks.ts @@ -12,8 +12,6 @@ doc.addSection({ children: [paragraph], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/36-image-to-table-cell.ts b/demo/36-image-to-table-cell.ts index 689a07a997..1c7897c558 100644 --- a/demo/36-image-to-table-cell.ts +++ b/demo/36-image-to-table-cell.ts @@ -22,8 +22,6 @@ doc.addSection({ children: [table], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/37-images-to-header-and-footer.ts b/demo/37-images-to-header-and-footer.ts index 08c4e547f5..86a8a20218 100644 --- a/demo/37-images-to-header-and-footer.ts +++ b/demo/37-images-to-header-and-footer.ts @@ -17,8 +17,6 @@ doc.addSection({ children: [new Paragraph("Hello World")], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/38-text-wrapping.ts b/demo/38-text-wrapping.ts index 84eb162fa5..9dac048e52 100644 --- a/demo/38-text-wrapping.ts +++ b/demo/38-text-wrapping.ts @@ -40,8 +40,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/39-page-numbers.ts b/demo/39-page-numbers.ts index 811115b876..edc9c411f9 100644 --- a/demo/39-page-numbers.ts +++ b/demo/39-page-numbers.ts @@ -40,8 +40,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/4-basic-table.ts b/demo/4-basic-table.ts index 48bcca1c0a..78367caab9 100644 --- a/demo/4-basic-table.ts +++ b/demo/4-basic-table.ts @@ -16,8 +16,6 @@ doc.addSection({ children: [table], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/40-line-numbers.ts b/demo/40-line-numbers.ts index f3bef349ec..d8da6f3074 100644 --- a/demo/40-line-numbers.ts +++ b/demo/40-line-numbers.ts @@ -24,8 +24,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/41-merge-table-cells-2.ts b/demo/41-merge-table-cells-2.ts index 2d3609fe5d..1801eb4a6e 100644 --- a/demo/41-merge-table-cells-2.ts +++ b/demo/41-merge-table-cells-2.ts @@ -49,8 +49,6 @@ doc.addSection({ children: [table], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/43-images-to-table-cell-2.ts b/demo/43-images-to-table-cell-2.ts index 06806d5780..90d6322bb7 100644 --- a/demo/43-images-to-table-cell-2.ts +++ b/demo/43-images-to-table-cell-2.ts @@ -17,8 +17,6 @@ doc.addSection({ children: [table], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/44-multiple-columns.ts b/demo/44-multiple-columns.ts index 83fbcf9efe..3fdf328461 100644 --- a/demo/44-multiple-columns.ts +++ b/demo/44-multiple-columns.ts @@ -35,8 +35,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/45-highlighting-text.ts b/demo/45-highlighting-text.ts index 0e2b416f33..d33a99fb94 100644 --- a/demo/45-highlighting-text.ts +++ b/demo/45-highlighting-text.ts @@ -30,8 +30,6 @@ doc.addSection({ children: [], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/46-shading-text.ts b/demo/46-shading-text.ts index 5e0b816a63..61f3f3d984 100644 --- a/demo/46-shading-text.ts +++ b/demo/46-shading-text.ts @@ -34,8 +34,6 @@ doc.addSection({ children: [], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/5-images.ts b/demo/5-images.ts index 0102998570..260fd25f15 100644 --- a/demo/5-images.ts +++ b/demo/5-images.ts @@ -57,8 +57,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/6-page-borders.ts b/demo/6-page-borders.ts index 47cca9c5f6..d54aac9a94 100644 --- a/demo/6-page-borders.ts +++ b/demo/6-page-borders.ts @@ -35,8 +35,6 @@ doc.addSection({ ], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/7-landscape.ts b/demo/7-landscape.ts index 2b171dea2f..fa3e551728 100644 --- a/demo/7-landscape.ts +++ b/demo/7-landscape.ts @@ -12,8 +12,6 @@ doc.addSection({ children: [new Paragraph("Hello World")], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/8-header-footer.ts b/demo/8-header-footer.ts index 012a4a7bf5..6455d94390 100644 --- a/demo/8-header-footer.ts +++ b/demo/8-header-footer.ts @@ -19,8 +19,6 @@ doc.addSection({ children: [new Paragraph("Hello World")], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/9-images-in-header-and-footer.ts b/demo/9-images-in-header-and-footer.ts index eb81879223..75acd4f07f 100644 --- a/demo/9-images-in-header-and-footer.ts +++ b/demo/9-images-in-header-and-footer.ts @@ -30,8 +30,6 @@ doc.addSection({ children: [new Paragraph("Hello World")], }); -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/browser-demo.html b/demo/browser-demo.html index a199c1b4e8..22c7c821a5 100644 --- a/demo/browser-demo.html +++ b/demo/browser-demo.html @@ -32,9 +32,9 @@ ], }); - const packer = new Packer(); - packer.toBlob(doc).then((blob) => { + + Packer.toBlob(doc).then((blob) => { console.log(blob); saveAs(blob, "example.docx"); console.log("Document created successfully"); diff --git a/docs/README.md b/docs/README.md index e8da69f267..089c133ac2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -59,9 +59,7 @@ doc.addSection({ }); // Used to export the file into a .docx file -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/docs/usage/packers.md b/docs/usage/packers.md index 274942b715..e9b3626015 100644 --- a/docs/usage/packers.md +++ b/docs/usage/packers.md @@ -2,9 +2,44 @@ > Packers are the way in which `docx` turns your code into `.docx` format. It is completely decoupled from the `docx.Document`. +Packers in `version 4` and above are now one single `Packer`. It works in both a node and browser environment (Angular etc). Now, the packer returns a `Buffer`, `Blob` or `base64 string`. It is up to you to take that and persist it with node's `fs`, send it down as a downloadable file, or anything else you wish. As of version 4, this library will not have options to export to PDF. + +## Version 5 + +Packers in `version 5` and above are now static methods on `Packer`. + +### Export as Buffer + +This will return a NodeJS `Buffer`. If this is used in the browser, it will return a `UInt8Array` instead. + +```ts +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); +``` + +### Export as a `base64` string + +```ts +Packer.toBase64String(doc).then((string) => { + console.log(string); +}); +``` + +### Export as Blob + +This is useful if you want to send it as an downloadable in a browser environment. + +```ts +Packer.toBlob(doc).then((blob) => { + // saveAs from FileSaver will download the file + saveAs(blob, "example.docx"); +}); +``` + ## Version 4 -Packers in `version 4` and above are now one single `Packer`. It works in both a node and browser environment (Angular etc). Now, the packer returns a `Buffer`, `Blob` or `base64 string`. It is up to you to take that and persist it with node's `fs`, send it down as a downloadable file, or anything else you wish. As of version 4, this library will not have options to export to PDF. +The `Packer` in `version 4` requires an instance of `Packer`, so be sure to `new` it. ### Export as Buffer diff --git a/docs/usage/styling-with-js.md b/docs/usage/styling-with-js.md index 74fcdffb93..c39e7a7f40 100644 --- a/docs/usage/styling-with-js.md +++ b/docs/usage/styling-with-js.md @@ -107,8 +107,7 @@ There are three parts to using custom styles with `docx`: 3. When you generate your document, make sure to pass the `styles` container to the `Packer`: ```ts - const packer = new Packer(doc, myStyles); - packer.pack(myOutStream); + Packer.pack(myOutStream); ``` **Note**: If you are using the `.headingX` or `.title` methods of paragraphs, you must make sure to define `HeadingX` or `Title` styles for these. Otherwise they'll show up unstyled :(. If you are using the `.bullet` or `.setNumbering` methods, you need to define a `ListParagraph` style or the numbers may not show up. diff --git a/src/export/packer/next-compiler.ts b/src/export/packer/next-compiler.ts index a2a09c3a8b..426d32174f 100644 --- a/src/export/packer/next-compiler.ts +++ b/src/export/packer/next-compiler.ts @@ -30,17 +30,15 @@ interface IXmlifyedFileMapping { export class Compiler { private readonly formatter: Formatter; private readonly imageReplacer: ImageReplacer; - private readonly prettifyXml?: boolean; - constructor(prettifyXml?: boolean) { + constructor() { this.formatter = new Formatter(); this.imageReplacer = new ImageReplacer(); - this.prettifyXml = prettifyXml; } - public compile(file: File): JSZip { + public compile(file: File, prettifyXml?: boolean): JSZip { const zip = new JSZip(); - const xmlifiedFileMapping = this.xmlifyFile(file); + const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml); for (const key in xmlifiedFileMapping) { if (!xmlifiedFileMapping[key]) { @@ -66,14 +64,14 @@ export class Compiler { return zip; } - private xmlifyFile(file: File): IXmlifyedFileMapping { + private xmlifyFile(file: File, prettify?: boolean): IXmlifyedFileMapping { file.verifyUpdateFields(); const documentRelationshipCount = file.DocumentRelationships.RelationshipCount + 1; return { Relationships: { data: (() => { - const xmlData = xml(this.formatter.format(file.Document), this.prettifyXml); + const xmlData = xml(this.formatter.format(file.Document), prettify); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); mediaDatas.forEach((mediaData, i) => { @@ -84,13 +82,13 @@ export class Compiler { ); }); - return xml(this.formatter.format(file.DocumentRelationships), this.prettifyXml); + return xml(this.formatter.format(file.DocumentRelationships), prettify); })(), path: "word/_rels/document.xml.rels", }, Document: { data: (() => { - const tempXmlData = xml(this.formatter.format(file.Document), this.prettifyXml); + const tempXmlData = xml(this.formatter.format(file.Document), prettify); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, documentRelationshipCount); @@ -99,7 +97,7 @@ export class Compiler { path: "word/document.xml", }, Styles: { - data: xml(this.formatter.format(file.Styles), this.prettifyXml), + data: xml(this.formatter.format(file.Styles), prettify), path: "word/styles.xml", }, Properties: { @@ -112,15 +110,15 @@ export class Compiler { path: "docProps/core.xml", }, Numbering: { - data: xml(this.formatter.format(file.Numbering), this.prettifyXml), + data: xml(this.formatter.format(file.Numbering), prettify), path: "word/numbering.xml", }, FileRelationships: { - data: xml(this.formatter.format(file.FileRelationships), this.prettifyXml), + data: xml(this.formatter.format(file.FileRelationships), prettify), path: "_rels/.rels", }, HeaderRelationships: file.Headers.map((headerWrapper, index) => { - const xmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml); + const xmlData = xml(this.formatter.format(headerWrapper.Header), prettify); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); mediaDatas.forEach((mediaData, i) => { @@ -132,12 +130,12 @@ export class Compiler { }); return { - data: xml(this.formatter.format(headerWrapper.Relationships), this.prettifyXml), + data: xml(this.formatter.format(headerWrapper.Relationships), prettify), path: `word/_rels/header${index + 1}.xml.rels`, }; }), FooterRelationships: file.Footers.map((footerWrapper, index) => { - const xmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml); + const xmlData = xml(this.formatter.format(footerWrapper.Footer), prettify); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); mediaDatas.forEach((mediaData, i) => { @@ -149,12 +147,12 @@ export class Compiler { }); return { - data: xml(this.formatter.format(footerWrapper.Relationships), this.prettifyXml), + data: xml(this.formatter.format(footerWrapper.Relationships), prettify), path: `word/_rels/footer${index + 1}.xml.rels`, }; }), Headers: file.Headers.map((headerWrapper, index) => { - const tempXmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml); + const tempXmlData = xml(this.formatter.format(headerWrapper.Header), prettify); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); // TODO: 0 needs to be changed when headers get relationships of their own const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0); @@ -165,7 +163,7 @@ export class Compiler { }; }), Footers: file.Footers.map((footerWrapper, index) => { - const tempXmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml); + const tempXmlData = xml(this.formatter.format(footerWrapper.Footer), prettify); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); // TODO: 0 needs to be changed when headers get relationships of their own const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0); @@ -176,19 +174,19 @@ export class Compiler { }; }), ContentTypes: { - data: xml(this.formatter.format(file.ContentTypes), this.prettifyXml), + data: xml(this.formatter.format(file.ContentTypes), prettify), path: "[Content_Types].xml", }, AppProperties: { - data: xml(this.formatter.format(file.AppProperties), this.prettifyXml), + data: xml(this.formatter.format(file.AppProperties), prettify), path: "docProps/app.xml", }, FootNotes: { - data: xml(this.formatter.format(file.FootNotes), this.prettifyXml), + data: xml(this.formatter.format(file.FootNotes), prettify), path: "word/footnotes.xml", }, Settings: { - data: xml(this.formatter.format(file.Settings), this.prettifyXml), + data: xml(this.formatter.format(file.Settings), prettify), path: "word/settings.xml", }, }; diff --git a/src/export/packer/packer.spec.ts b/src/export/packer/packer.spec.ts index e2be5aca44..2bf8537bed 100644 --- a/src/export/packer/packer.spec.ts +++ b/src/export/packer/packer.spec.ts @@ -7,7 +7,6 @@ import { File, HeadingLevel, Paragraph } from "file"; import { Packer } from "./packer"; describe("Packer", () => { - let packer: Packer; let file: File; beforeEach(() => { @@ -34,14 +33,12 @@ describe("Packer", () => { new Paragraph("test text"), ], }); - - packer = new Packer(); }); describe("#toBuffer()", () => { it("should create a standard docx file", async function() { this.timeout(99999999); - const buffer = await packer.toBuffer(file); + const buffer = await Packer.toBuffer(file); assert.isDefined(buffer); assert.isTrue(buffer.byteLength > 0); @@ -49,10 +46,10 @@ describe("Packer", () => { it("should handle exception if it throws any", () => { // tslint:disable-next-line:no-any - const compiler = stub((packer as any).compiler, "compile"); + const compiler = stub((Packer as any).compiler, "compile"); compiler.throwsException(); - return packer.toBuffer(file).catch((error) => { + return Packer.toBuffer(file).catch((error) => { assert.isDefined(error); }); }); @@ -61,7 +58,7 @@ describe("Packer", () => { describe("#toBase64String()", () => { it("should create a standard docx file", async function() { this.timeout(99999999); - const str = await packer.toBase64String(file); + const str = await Packer.toBase64String(file); assert.isDefined(str); assert.isTrue(str.length > 0); @@ -69,10 +66,10 @@ describe("Packer", () => { it("should handle exception if it throws any", () => { // tslint:disable-next-line:no-any - const compiler = stub((packer as any).compiler, "compile"); + const compiler = stub((Packer as any).compiler, "compile"); compiler.throwsException(); - return packer.toBase64String(file).catch((error) => { + return Packer.toBase64String(file).catch((error) => { assert.isDefined(error); }); }); diff --git a/src/export/packer/packer.ts b/src/export/packer/packer.ts index a6c0f33cc4..c064836ed2 100644 --- a/src/export/packer/packer.ts +++ b/src/export/packer/packer.ts @@ -2,14 +2,8 @@ import { File } from "file"; import { Compiler } from "./next-compiler"; export class Packer { - private readonly compiler: Compiler; - - constructor(prettifyXml?: boolean) { - this.compiler = new Compiler(prettifyXml); - } - - public async toBuffer(file: File): Promise { - const zip = this.compiler.compile(file); + public static async toBuffer(file: File, prettify?: boolean): Promise { + const zip = this.compiler.compile(file, prettify); const zipData = (await zip.generateAsync({ type: "nodebuffer", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", @@ -18,8 +12,8 @@ export class Packer { return zipData; } - public async toBase64String(file: File): Promise { - const zip = this.compiler.compile(file); + public static async toBase64String(file: File, prettify?: boolean): Promise { + const zip = this.compiler.compile(file, prettify); const zipData = (await zip.generateAsync({ type: "base64", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", @@ -28,8 +22,8 @@ export class Packer { return zipData; } - public async toBlob(file: File): Promise { - const zip = this.compiler.compile(file); + public static async toBlob(file: File, prettify?: boolean): Promise { + const zip = this.compiler.compile(file, prettify); const zipData = (await zip.generateAsync({ type: "blob", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", @@ -37,4 +31,6 @@ export class Packer { return zipData; } + + private static readonly compiler = new Compiler(); } From 9fdd07e7fe0da01d2f6ff442c0533100e8eb5aac Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 7 Aug 2019 22:27:44 +0100 Subject: [PATCH 2/2] Fix tests --- src/export/packer/packer.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/export/packer/packer.spec.ts b/src/export/packer/packer.spec.ts index 2bf8537bed..8235649f15 100644 --- a/src/export/packer/packer.spec.ts +++ b/src/export/packer/packer.spec.ts @@ -53,6 +53,11 @@ describe("Packer", () => { assert.isDefined(error); }); }); + + after(() => { + // tslint:disable-next-line:no-any + (Packer as any).compiler.compile.restore(); + }); }); describe("#toBase64String()", () => { @@ -73,5 +78,10 @@ describe("Packer", () => { assert.isDefined(error); }); }); + + after(() => { + // tslint:disable-next-line:no-any + (Packer as any).compiler.compile.restore(); + }); }); });