## Demo
@@ -57,18 +53,12 @@ Please refer to [https://docx.js.org/](https://docx.js.org/) for details on how
## Examples
-Check the examples section in the [documentation](https://docx.js.org/#/examples) and the [demo folder](https://github.com/dolanmiu/docx/tree/master/demo) for examples.
+Check the `examples` section in the [documentation](https://docx.js.org/#/examples) and the [demo folder](https://github.com/dolanmiu/docx/tree/master/demo) for examples.
-# Contributing
+## Contributing
Read the contribution guidelines [here](https://docx.js.org/#/contribution-guidelines).
-# Honoured Mentions
-
-[@felipeochoa](https://github.com/felipeochoa)
-
-[@h4buli](https://github.com/h4buli)
-
---
Made with 💖
diff --git a/docs/README.md b/docs/README.md
index 2ea6ac25e3..557bcbad65 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -19,11 +19,11 @@ npm install --save docx
Then you can `require` or `import` as usual:
```js
-let docx = require('docx');
+let docx = require("docx");
```
```js
-import * as docx from 'docx'
+import * as docx from "docx";
```
## Basic Usage
@@ -48,6 +48,12 @@ exporter.pack("My First Document");
// Done! A file called 'My First Document.docx' will be in your file system if you used LocalPacker
```
+## Honoured Mentions
+
+[@felipeochoa](https://github.com/felipeochoa)
+
+[@h4buli](https://github.com/h4buli)
+
---
Made with 💖
From f6996a1d989ebc37a377b9521afc7babbebb62a8 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 03:20:46 +0100
Subject: [PATCH 27/63] Update README
---
README.md | 8 --------
docs/examples.md | 8 ++++++++
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/README.md b/README.md
index f102c85f7e..ad198e1d03 100644
--- a/README.md
+++ b/README.md
@@ -39,14 +39,6 @@ Press `endpoint` on the `RunKit` website:
* https://runkit.com/dolanmiu/docx-demo8/1.0.1 - Header and Footer
* https://runkit.com/dolanmiu/docx-demo10 - **My CV generated with docx**
-#### Run demos locally:
-
-```sh
-$ npm run demo
-```
-
-This command will run the demo selector app in the `demo` folder. It will prompt you to select a demo number, which will run a demo from that folder.
-
## How to
Please refer to [https://docx.js.org/](https://docx.js.org/) for details on how to use this library, examples and much more!
diff --git a/docs/examples.md b/docs/examples.md
index c2450496ca..cd811ded43 100644
--- a/docs/examples.md
+++ b/docs/examples.md
@@ -2,6 +2,14 @@
> All examples can run independently and can be found in the `/demo` folder of the project
+All the examples below can be ran locally, to do so, run the following command:
+
+```sh
+npm run demo
+```
+
+This command will run the `demo selector app` in the `/demo` folder. It will prompt you to select a demo number, which will run a demo from that folder.
+
## Simple
A simple hello world of the `docx` library:
From 5e7c5aa620509b75865c9a4a9058855139b2e1c9 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 03:23:07 +0100
Subject: [PATCH 28/63] Fix typo
---
docs/contribution-guidelines.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/docs/contribution-guidelines.md b/docs/contribution-guidelines.md
index e50ec54f54..5d1442fe0c 100644
--- a/docs/contribution-guidelines.md
+++ b/docs/contribution-guidelines.md
@@ -28,7 +28,7 @@ This is just a guideline, and the rules can sometimes be broken.
* Use `add` if you add the element into the method as a parameter:
```js
- public createParagraph(paragraph: Paragraph) {
+ public addParagraph(paragraph: Paragraph) {
this.root.push(paragraph);
}
```
From 0a698514631caaba091fd09ee8230bde0508c90f Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 11:51:27 +0100
Subject: [PATCH 29/63] Update README.md
---
README.md | 15 +++++----------
1 file changed, 5 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
index ad198e1d03..a22b9d04f3 100644
--- a/README.md
+++ b/README.md
@@ -14,16 +14,13 @@
[![Dependency Status][daviddm-image]][daviddm-url]
[![Known Vulnerabilities][snky-image]][snky-url]
[![Chat on Gitter][gitter-image]][gitter-url]
-[![code style: prettier][prettier-image]][prettier-url]
[![PRs Welcome][pr-image]][pr-url]
-# docx
-
-## Demo
+# Demo
Press `endpoint` on the `RunKit` website:
@@ -39,15 +36,15 @@ Press `endpoint` on the `RunKit` website:
* https://runkit.com/dolanmiu/docx-demo8/1.0.1 - Header and Footer
* https://runkit.com/dolanmiu/docx-demo10 - **My CV generated with docx**
-## How to
+# How to use & Documentation
-Please refer to [https://docx.js.org/](https://docx.js.org/) for details on how to use this library, examples and much more!
+Please refer to the [https://docx.js.org/](documentation - https://docx.js.org/) for details on how to use this library, examples and much more!
-## Examples
+# Examples
Check the `examples` section in the [documentation](https://docx.js.org/#/examples) and the [demo folder](https://github.com/dolanmiu/docx/tree/master/demo) for examples.
-## Contributing
+# Contributing
Read the contribution guidelines [here](https://docx.js.org/#/contribution-guidelines).
@@ -67,7 +64,5 @@ Made with 💖
[snky-url]: https://snyk.io/test/github/dolanmiu/docx
[gitter-image]: https://badges.gitter.im/dolanmiu/docx.svg
[gitter-url]: https://gitter.im/docx-lib/Lobby
-[prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg
-[prettier-url]: https://github.com/prettier/prettier
[pr-image]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg
[pr-url]: http://makeapullrequest.com
From e9bd27bc554012b64bac67391039d81ebf6f50df Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 11:52:17 +0100
Subject: [PATCH 30/63] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index a22b9d04f3..09b6c8d2e3 100644
--- a/README.md
+++ b/README.md
@@ -38,7 +38,7 @@ Press `endpoint` on the `RunKit` website:
# How to use & Documentation
-Please refer to the [https://docx.js.org/](documentation - https://docx.js.org/) for details on how to use this library, examples and much more!
+Please refer to the [documentation - https://docx.js.org/](https://docx.js.org/) for details on how to use this library, examples and much more!
# Examples
From d9716a1cf766c45466c9715c5c2314e41d247e1a Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 11:52:47 +0100
Subject: [PATCH 31/63] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 09b6c8d2e3..84b4a75e0b 100644
--- a/README.md
+++ b/README.md
@@ -38,7 +38,7 @@ Press `endpoint` on the `RunKit` website:
# How to use & Documentation
-Please refer to the [documentation - https://docx.js.org/](https://docx.js.org/) for details on how to use this library, examples and much more!
+Please refer to the [documentation at https://docx.js.org/](https://docx.js.org/) for details on how to use this library, examples and much more!
# Examples
From d8252141fed81a57b434f988018b3adc1dc15c89 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 11:54:05 +0100
Subject: [PATCH 32/63] Update tsconfig.json
---
tsconfig.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tsconfig.json b/tsconfig.json
index 8e716e322f..4ef656d049 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -25,7 +25,7 @@
"**/_*"
],
"typedocOptions": {
- "mode": "modules",
+ "mode": "file",
"out": "docs/api",
"exclude": "test",
"theme": "default",
From f264e4d0f7e053f56a890ba4c611194e4d2b82bd Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 21:58:50 +0100
Subject: [PATCH 33/63] Add scale method to image
---
src/file/media/image.ts | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/file/media/image.ts b/src/file/media/image.ts
index 8255fe7398..b596f77d9d 100644
--- a/src/file/media/image.ts
+++ b/src/file/media/image.ts
@@ -10,4 +10,8 @@ export class Image {
public get Run(): PictureRun {
return this.paragraph.Run;
}
+
+ public scale(factorX: number, factorY?: number): void {
+ this.paragraph.Run.scale(factorX, factorY);
+ }
}
From 89df3c48e00f664369321606bb7907b7741c0161 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 23:22:03 +0100
Subject: [PATCH 34/63] Fix linting errors
---
src/file/document/body/body.ts | 3 +-
.../page-border/page-borders.spec.ts | 2 +-
.../page-border/page-borders.ts | 42 ++++++++++++-------
.../section-properties/section-properties.ts | 7 +---
src/file/file.ts | 3 +-
src/file/media/media.ts | 10 ++---
src/file/numbering/level.ts | 2 +-
src/file/styles/external-styles-factory.ts | 4 +-
8 files changed, 37 insertions(+), 36 deletions(-)
diff --git a/src/file/document/body/body.ts b/src/file/document/body/body.ts
index 7fc0e98f38..e274fa359f 100644
--- a/src/file/document/body/body.ts
+++ b/src/file/document/body/body.ts
@@ -1,7 +1,6 @@
import { IXmlableObject, XmlComponent } from "file/xml-components";
-import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties";
import { Paragraph, ParagraphProperties } from "../..";
-import { SectionProperties, SectionPropertiesOptions } from "./section-properties";
+import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties";
export class Body extends XmlComponent {
private readonly defaultSection: SectionProperties;
diff --git a/src/file/document/body/section-properties/page-border/page-borders.spec.ts b/src/file/document/body/section-properties/page-border/page-borders.spec.ts
index 782280f292..66abb9cc66 100644
--- a/src/file/document/body/section-properties/page-border/page-borders.spec.ts
+++ b/src/file/document/body/section-properties/page-border/page-borders.spec.ts
@@ -1,8 +1,8 @@
import { expect } from "chai";
import { Formatter } from "../../../../../export/formatter";
-import { PageBorders, PageBorderDisplay, PageBorderZOrder } from "./page-borders";
import { BorderStyle } from "../../../../styles";
+import { PageBorderDisplay, PageBorders, PageBorderZOrder } from "./page-borders";
describe("PageBorders", () => {
describe("#constructor()", () => {
diff --git a/src/file/document/body/section-properties/page-border/page-borders.ts b/src/file/document/body/section-properties/page-border/page-borders.ts
index 88b6ce98dd..0af96cfd41 100644
--- a/src/file/document/body/section-properties/page-border/page-borders.ts
+++ b/src/file/document/body/section-properties/page-border/page-borders.ts
@@ -1,5 +1,5 @@
// http://officeopenxml.com/WPsectionBorders.php
-import { XmlComponent, XmlAttributeComponent, IXmlableObject } from "file/xml-components";
+import { IXmlableObject, XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { BorderStyle } from "../../../../styles";
export enum PageBorderDisplay {
@@ -24,22 +24,22 @@ export interface IPageBorderAttributes {
zOrder?: PageBorderZOrder;
}
-export interface PageBorderConfiguration {
+export interface IPageBorderConfiguration {
style?: BorderStyle;
size?: number;
color?: string;
space?: number;
}
-export type PageBordersOptions = {
+export interface IPageBordersOptions {
pageBorders?: IPageBorderAttributes;
- pageBorderTop?: PageBorderConfiguration;
- pageBorderRight?: PageBorderConfiguration;
- pageBorderBottom?: PageBorderConfiguration;
- pageBorderLeft?: PageBorderConfiguration;
-};
+ pageBorderTop?: IPageBorderConfiguration;
+ pageBorderRight?: IPageBorderConfiguration;
+ pageBorderBottom?: IPageBorderConfiguration;
+ pageBorderLeft?: IPageBorderConfiguration;
+}
-class PageBordeAttributes extends XmlAttributeComponent {
+class PageBordeAttributes extends XmlAttributeComponent {
protected xmlKeys = {
style: "w:val",
size: "w:size",
@@ -49,7 +49,7 @@ class PageBordeAttributes extends XmlAttributeComponent
}
class PageBorder extends XmlComponent {
- constructor(key: string, options: PageBorderConfiguration) {
+ constructor(key: string, options: IPageBorderConfiguration) {
super(key);
this.root.push(new PageBordeAttributes(options));
@@ -65,10 +65,12 @@ class PageBordersAttributes extends XmlAttributeComponent
}
export class PageBorders extends XmlComponent {
- constructor(options?: PageBordersOptions) {
+ constructor(options?: IPageBordersOptions) {
super("w:pgBorders");
- if (!options) return;
+ if (!options) {
+ return;
+ }
let pageBordersAttributes = {};
@@ -82,10 +84,18 @@ export class PageBorders extends XmlComponent {
this.root.push(new PageBordersAttributes(pageBordersAttributes));
- if (options.pageBorderTop) this.root.push(new PageBorder("w:top", options.pageBorderTop));
- if (options.pageBorderRight) this.root.push(new PageBorder("w:right", options.pageBorderRight));
- if (options.pageBorderBottom) this.root.push(new PageBorder("w:bottom", options.pageBorderBottom));
- if (options.pageBorderLeft) this.root.push(new PageBorder("w:left", options.pageBorderLeft));
+ if (options.pageBorderTop) {
+ this.root.push(new PageBorder("w:top", options.pageBorderTop));
+ }
+ if (options.pageBorderRight) {
+ this.root.push(new PageBorder("w:right", options.pageBorderRight));
+ }
+ if (options.pageBorderBottom) {
+ this.root.push(new PageBorder("w:bottom", options.pageBorderBottom));
+ }
+ if (options.pageBorderLeft) {
+ this.root.push(new PageBorder("w:left", options.pageBorderLeft));
+ }
}
public prepForXml(): IXmlableObject {
diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts
index 33d0a7a0ae..c23964cd14 100644
--- a/src/file/document/body/section-properties/section-properties.ts
+++ b/src/file/document/body/section-properties/section-properties.ts
@@ -1,6 +1,6 @@
// http://officeopenxml.com/WPsection.php
import { XmlComponent } from "file/xml-components";
-import { FooterReferenceType, IPageNumberTypeAttributes, PageNumberFormat, PageNumberType } from "./";
+import { FooterReferenceType, IPageBordersOptions, IPageNumberTypeAttributes, PageBorders, PageNumberFormat, PageNumberType } from "./";
import { Columns } from "./columns/columns";
import { IColumnsAttributes } from "./columns/columns-attributes";
import { DocumentGrid } from "./doc-grid/doc-grid";
@@ -12,9 +12,6 @@ import { PageMargin } from "./page-margin/page-margin";
import { IPageMarginAttributes } from "./page-margin/page-margin-attributes";
import { PageSize } from "./page-size/page-size";
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
-import { FooterReferenceType, IPageNumberTypeAttributes, PageNumberType, PageNumberFormat } from ".";
-// import { TitlePage } from "./title-page/title-page";
-import { FooterReferenceType, IPageNumberTypeAttributes, PageNumberType, PageNumberFormat, PageBordersOptions, PageBorders } from ".";
export type SectionPropertiesOptions = IPageSizeAttributes &
IPageMarginAttributes &
@@ -23,7 +20,7 @@ export type SectionPropertiesOptions = IPageSizeAttributes &
IHeaderOptions &
IFooterOptions &
IPageNumberTypeAttributes &
- PageBordersOptions;
+ IPageBordersOptions;
export class SectionProperties extends XmlComponent {
private readonly options: SectionPropertiesOptions;
diff --git a/src/file/file.ts b/src/file/file.ts
index 83d0c8c53b..2482d9b39c 100644
--- a/src/file/file.ts
+++ b/src/file/file.ts
@@ -2,7 +2,7 @@ import { AppProperties } from "./app-properties/app-properties";
import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties";
import { Document } from "./document";
-import { FooterReferenceType, HeaderReference, HeaderReferenceType } from "./document/body/section-properties";
+import { FooterReferenceType, HeaderReference, HeaderReferenceType, SectionPropertiesOptions } from "./document/body/section-properties";
import { FooterWrapper } from "./footer-wrapper";
import { FootNotes } from "./footnotes";
import { HeaderWrapper } from "./header-wrapper";
@@ -14,7 +14,6 @@ import { Styles } from "./styles";
import { ExternalStylesFactory } from "./styles/external-styles-factory";
import { DefaultStylesFactory } from "./styles/factory";
import { Table } from "./table";
-import { FooterReferenceType, HeaderReferenceType, SectionPropertiesOptions } from "./document/body/section-properties";
export class File {
private readonly document: Document;
diff --git a/src/file/media/media.ts b/src/file/media/media.ts
index e8e6044569..aea65777f0 100644
--- a/src/file/media/media.ts
+++ b/src/file/media/media.ts
@@ -61,8 +61,6 @@ export class Media {
this.map = new Map();
}
- x: Math.round(dimensions.width * 9525),
- y: Math.round(dimensions.height * 9525),
public getMedia(key: string): IMediaData {
const data = this.map.get(key);
@@ -108,12 +106,12 @@ export class Media {
fileName: key,
dimensions: {
pixels: {
- x: dimensions.width,
- y: dimensions.height,
+ x: Math.round(dimensions.width),
+ y: Math.round(dimensions.height),
},
emus: {
- x: dimensions.width * 9525,
- y: dimensions.height * 9525,
+ x: Math.round(dimensions.width * 9525),
+ y: Math.round(dimensions.height * 9525),
},
},
};
diff --git a/src/file/numbering/level.ts b/src/file/numbering/level.ts
index 0df90758fb..8bdc7ff8d7 100644
--- a/src/file/numbering/level.ts
+++ b/src/file/numbering/level.ts
@@ -110,7 +110,7 @@ export class LevelBase extends XmlComponent {
this.root.push(this.runProperties);
}
- public setSuffix(value: LevelSuffix) {
+ public setSuffix(value: LevelSuffix): LevelBase {
this.root.push(new Suffix(value));
return this;
}
diff --git a/src/file/styles/external-styles-factory.ts b/src/file/styles/external-styles-factory.ts
index 303d563907..301211bdc7 100644
--- a/src/file/styles/external-styles-factory.ts
+++ b/src/file/styles/external-styles-factory.ts
@@ -1,7 +1,5 @@
import * as fastXmlParser from "fast-xml-parser";
-import { ImportedXmlComponent, ImportedRootElementAttributes, parseOptions, convertToXmlComponent } from "./../../file/xml-components";
-import { convertToXmlComponent, ImportedRootElementAttributes, ImportedXmlComponent, parseOptions } from "file/xml-components";
-import { ImportedRootElementAttributes, parseOptions, convertToXmlComponent } from "./../../file/xml-components";
+import { convertToXmlComponent, ImportedRootElementAttributes, parseOptions } from "file/xml-components";
import { Styles } from "./";
export class ExternalStylesFactory {
From 5f594a822fc2b4a89e2c82c5ca255f9ac05eefa7 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 23:29:40 +0100
Subject: [PATCH 35/63] Fix and enable linting on tests
---
src/file/content-types/content-types.spec.ts | 3 +++
.../body/section-properties/section-properties.spec.ts | 2 +-
src/file/drawing/floating/simple-pos.spec.ts | 2 +-
src/file/styles/external-styles-factory.spec.ts | 5 ++++-
src/file/table/table-cell.spec.ts | 2 +-
src/file/xml-components/imported-xml-component.spec.ts | 6 ++++--
tsconfig.json | 1 -
7 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/src/file/content-types/content-types.spec.ts b/src/file/content-types/content-types.spec.ts
index 14665df4ee..7e9b62c88f 100644
--- a/src/file/content-types/content-types.spec.ts
+++ b/src/file/content-types/content-types.spec.ts
@@ -1,6 +1,9 @@
+// tslint:disable:no-string-literal
+
import { expect } from "chai";
import { Formatter } from "../../export/formatter";
import { ContentTypes } from "./content-types";
+
describe("ContentTypes", () => {
let contentTypes: ContentTypes;
diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts
index b2499bbd63..e80facb635 100644
--- a/src/file/document/body/section-properties/section-properties.spec.ts
+++ b/src/file/document/body/section-properties/section-properties.spec.ts
@@ -1,8 +1,8 @@
import { expect } from "chai";
import { Formatter } from "../../../../export/formatter";
+import { FooterReferenceType, PageBorderOffsetFrom, PageNumberFormat } from "./";
import { SectionProperties } from "./section-properties";
-import { FooterReferenceType, PageNumberFormat, PageBorderOffsetFrom } from ".";
describe("SectionProperties", () => {
describe("#constructor()", () => {
diff --git a/src/file/drawing/floating/simple-pos.spec.ts b/src/file/drawing/floating/simple-pos.spec.ts
index a86739b7b0..5eee49f4c1 100644
--- a/src/file/drawing/floating/simple-pos.spec.ts
+++ b/src/file/drawing/floating/simple-pos.spec.ts
@@ -1,7 +1,7 @@
import { assert } from "chai";
-import { SimplePos } from "./simple-pos";
import { Utility } from "../../../tests/utility";
+import { SimplePos } from "./simple-pos";
describe("SimplePos", () => {
describe("#constructor()", () => {
diff --git a/src/file/styles/external-styles-factory.spec.ts b/src/file/styles/external-styles-factory.spec.ts
index e34bfbaa8d..295c9644f6 100644
--- a/src/file/styles/external-styles-factory.spec.ts
+++ b/src/file/styles/external-styles-factory.spec.ts
@@ -22,7 +22,7 @@ describe("External styles factory", () => {
-
+
@@ -48,6 +48,7 @@ describe("External styles factory", () => {
describe("#parse", () => {
it("should parse w:styles attributes", () => {
+ // tslint:disable-next-line:no-any
const importedStyle = new ExternalStylesFactory().newInstance(externalStyles) as any;
expect(importedStyle.rootKey).to.equal("w:styles");
@@ -58,6 +59,7 @@ describe("External styles factory", () => {
});
it("should parse other child elements of w:styles", () => {
+ // tslint:disable-next-line:no-any
const importedStyle = new ExternalStylesFactory().newInstance(externalStyles) as any;
expect(importedStyle.root.length).to.equal(5);
@@ -134,6 +136,7 @@ describe("External styles factory", () => {
});
it("should parse styles elements", () => {
+ // tslint:disable-next-line:no-any
const importedStyle = new ExternalStylesFactory().newInstance(externalStyles) as any;
expect(importedStyle.root.length).to.equal(5);
diff --git a/src/file/table/table-cell.spec.ts b/src/file/table/table-cell.spec.ts
index 4e69e958a8..7a3072f186 100644
--- a/src/file/table/table-cell.spec.ts
+++ b/src/file/table/table-cell.spec.ts
@@ -1,8 +1,8 @@
import { expect } from "chai";
-import { TableCellBorders, TableCellWidth, WidthType } from "./table-cell";
import { Formatter } from "../../export/formatter";
import { BorderStyle } from "../styles";
+import { TableCellBorders, TableCellWidth, WidthType } from "./table-cell";
describe("TableCellBorders", () => {
describe("#prepForXml", () => {
diff --git a/src/file/xml-components/imported-xml-component.spec.ts b/src/file/xml-components/imported-xml-component.spec.ts
index e941d3f0ec..beed8cf09a 100644
--- a/src/file/xml-components/imported-xml-component.spec.ts
+++ b/src/file/xml-components/imported-xml-component.spec.ts
@@ -1,5 +1,5 @@
import { expect } from "chai";
-import { ImportedXmlComponent, convertToXmlComponent } from "./";
+import { convertToXmlComponent, ImportedXmlComponent } from "./";
const xmlString = `
@@ -11,10 +11,11 @@ const xmlString = `
Text 2
-
+
`;
+// tslint:disable:object-literal-key-quotes
const importedXmlElement = {
"w:p": {
_attr: { "w:one": "value 1", "w:two": "value 2" },
@@ -22,6 +23,7 @@ const importedXmlElement = {
"w:r": [{ _attr: { active: "true" }, "w:t": "Text 1" }, { _attr: { active: "true" }, "w:t": "Text 2" }],
},
};
+// tslint:enable:object-literal-key-quotes
const convertedXmlElement = {
deleted: false,
diff --git a/tsconfig.json b/tsconfig.json
index 4ef656d049..53730ebdfd 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -21,7 +21,6 @@
"exclude": [
"node_modules",
"tests",
- "**/*.spec.ts",
"**/_*"
],
"typedocOptions": {
From ad356d27596c57d2d0e5a2b8d15c5430a96da3a8 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Thu, 9 Aug 2018 23:40:11 +0100
Subject: [PATCH 36/63] Add ignores so it doesnt get generated in documentation
---
src/export/packer/packer.ts | 3 +++
src/file/media/data.ts | 3 +++
src/file/xml-components/xmlable-object.ts | 5 ++++-
3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/src/export/packer/packer.ts b/src/export/packer/packer.ts
index 64fa05a9aa..848924f457 100644
--- a/src/export/packer/packer.ts
+++ b/src/export/packer/packer.ts
@@ -3,4 +3,7 @@ export interface IPacker {
}
// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432
+/**
+ * @ignore
+ */
export const WORKAROUND = "";
diff --git a/src/file/media/data.ts b/src/file/media/data.ts
index cbc7d8c5bf..dfc7a664a0 100644
--- a/src/file/media/data.ts
+++ b/src/file/media/data.ts
@@ -20,4 +20,7 @@ export interface IMediaData {
}
// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432
+/**
+ * @ignore
+ */
export const WORKAROUND2 = "";
diff --git a/src/file/xml-components/xmlable-object.ts b/src/file/xml-components/xmlable-object.ts
index 9fa0d5b600..255df14fc2 100644
--- a/src/file/xml-components/xmlable-object.ts
+++ b/src/file/xml-components/xmlable-object.ts
@@ -3,4 +3,7 @@ export interface IXmlableObject extends Object {
}
// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432
-export const WORKAROUND3 = "workaround";
+/**
+ * @ignore
+ */
+export const WORKAROUND3 = "";
From d850f379047222a3cb177163a4a236c1a9e3db38 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Fri, 10 Aug 2018 01:49:37 +0100
Subject: [PATCH 37/63] Remove unused import
---
src/file/drawing/drawing.spec.ts | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts
index 70d36f2110..208be2991a 100644
--- a/src/file/drawing/drawing.spec.ts
+++ b/src/file/drawing/drawing.spec.ts
@@ -1,5 +1,4 @@
import { assert } from "chai";
-import * as fs from "fs";
import { Utility } from "../../tests/utility";
import { Drawing, IDrawingOptions, PlacementPosition } from "./";
From bc1132146f6911e2d630d06623d3e923105ff6ed Mon Sep 17 00:00:00 2001
From: Dolan
Date: Fri, 10 Aug 2018 02:46:15 +0100
Subject: [PATCH 38/63] Revert test changes
---
src/file/drawing/drawing.spec.ts | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts
index 208be2991a..cf3f926315 100644
--- a/src/file/drawing/drawing.spec.ts
+++ b/src/file/drawing/drawing.spec.ts
@@ -1,4 +1,5 @@
import { assert } from "chai";
+import * as fs from "fs";
import { Utility } from "../../tests/utility";
import { Drawing, IDrawingOptions, PlacementPosition } from "./";
@@ -9,6 +10,7 @@ function createDrawing(drawingOptions?: IDrawingOptions): Drawing {
{
fileName: "test.jpg",
referenceId: 1,
+ stream: fs.createReadStream(path),
path: path,
dimensions: {
pixels: {
From 675192b86fc5cad868442a18c33f1725f597dd59 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Sun, 12 Aug 2018 23:07:31 +0100
Subject: [PATCH 39/63] Refactor image to accept Buffer only
---
demo/demo11.js | 5 +++--
demo/demo12.js | 9 ++++----
demo/demo18.js | 4 ++--
demo/demo5.js | 11 +++++-----
demo/demo9.js | 5 +++--
docs/usage/headers-and-footers.md | 4 ++--
docs/usage/images.md | 4 ++--
src/file/file.ts | 11 ++--------
src/file/footer-wrapper.ts | 4 ++--
src/file/header-wrapper.ts | 4 ++--
src/file/media/data.ts | 4 +---
src/file/media/media.ts | 36 +++++--------------------------
12 files changed, 35 insertions(+), 66 deletions(-)
diff --git a/demo/demo11.js b/demo/demo11.js
index 2c6305106e..f0597e0da4 100644
--- a/demo/demo11.js
+++ b/demo/demo11.js
@@ -1,4 +1,5 @@
const docx = require("../build");
+const fs = require('fs');
const doc = new docx.Document(undefined, {
top: 700,
@@ -80,7 +81,7 @@ doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph")
.quickFormat()
.basedOn("Normal");
-doc.createImage("./demo/images/pizza.gif");
+doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
doc
.createParagraph("HEADING")
.heading1()
@@ -122,7 +123,7 @@ var arrboth = [{
arrboth.forEach(function(item) {
const para = doc.createParagraph();
- para.createTextRun(doc.createImage(item.image));
+ para.createTextRun(doc.createImage(fs.readFileSync(item.image)));
para.properties.width = 60;
para.properties.height = 90;
doc.createParagraph(item.comment).style("normalPara2");
diff --git a/demo/demo12.js b/demo/demo12.js
index 7fd5eb5ffc..87c7a405e0 100644
--- a/demo/demo12.js
+++ b/demo/demo12.js
@@ -1,14 +1,15 @@
const docx = require("../build");
+const fs = require('fs');
var doc = new docx.Document();
var paragraph = new docx.Paragraph("Hello World");
doc.addParagraph(paragraph);
-const image = doc.createImage("./demo/images/pizza.gif");
-const image2 = doc.createImage("./demo/images/pizza.gif");
-const image3 = doc.createImage("./demo/images/pizza.gif");
-const image4 = doc.createImage("./demo/images/pizza.gif");
+const image = doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
+const image2 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
+const image3 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
+const image4 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
image.scale(0.5);
image2.scale(1)
diff --git a/demo/demo18.js b/demo/demo18.js
index 9035d19e7a..73a778a6c3 100644
--- a/demo/demo18.js
+++ b/demo/demo18.js
@@ -6,8 +6,8 @@ var doc = new docx.Document();
const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHiAjHh4hHx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z8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`
-// doc.createImageFromBuffer(Buffer.from(imageBase64Data, 'base64'));
-doc.createImageFromBuffer(Buffer.from(imageBase64Data, 'base64'), 100, 100);
+// doc.createImage(Buffer.from(imageBase64Data, 'base64'));
+doc.createImage(Buffer.from(imageBase64Data, 'base64'), 100, 100);
var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document');
diff --git a/demo/demo5.js b/demo/demo5.js
index 89638ad49b..fbb2cd0eaf 100644
--- a/demo/demo5.js
+++ b/demo/demo5.js
@@ -1,15 +1,16 @@
const docx = require("../build");
+const fs = require('fs');
var doc = new docx.Document();
var paragraph = new docx.Paragraph("Hello World");
doc.addParagraph(paragraph);
-doc.createImage("./demo/images/image1.jpeg");
-doc.createImage("./demo/images/dog.png");
-doc.createImage("./demo/images/cat.jpg");
-doc.createImage("./demo/images/parrots.bmp");
-doc.createImage("./demo/images/pizza.gif");
+doc.createImage(fs.readFileSync("./demo/images/image1.jpeg"));
+doc.createImage(fs.readFileSync("./demo/images/dog.png"));
+doc.createImage(fs.readFileSync("./demo/images/cat.jpg"));
+doc.createImage(fs.readFileSync("./demo/images/parrots.bmp"));
+doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
var exporter = new docx.LocalPacker(doc);
exporter.pack("My Document");
diff --git a/demo/demo9.js b/demo/demo9.js
index c93d8fa63c..c0a57cf8f6 100644
--- a/demo/demo9.js
+++ b/demo/demo9.js
@@ -1,11 +1,12 @@
const docx = require('../build');
+const fs = require('fs');
var doc = new docx.Document();
doc.createParagraph("Hello World");
-doc.Header.createImage("./demo/images/pizza.gif");
-doc.Footer.createImage("./demo/images/pizza.gif");
+doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif"));
+doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif"));
var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document');
diff --git a/docs/usage/headers-and-footers.md b/docs/usage/headers-and-footers.md
index bfe626a04c..1489576dda 100644
--- a/docs/usage/headers-and-footers.md
+++ b/docs/usage/headers-and-footers.md
@@ -19,8 +19,8 @@ doc.Footer.createParagraph("Footer text");
Even add images:
```js
-doc.Header.createImage([PATH_TO_YOUR_IMAGE]);
-doc.Footer.createImage([PATH_TO_YOUR_IMAGE]);
+doc.Header.createImage([BUFFER_OF_YOUR_IMAGE]);
+doc.Footer.createImage([BUFFER_OF_YOUR_IMAGE]);
```
Refer to `demo8.js` for more information
diff --git a/docs/usage/images.md b/docs/usage/images.md
index 2cc0339507..a89fa893cd 100644
--- a/docs/usage/images.md
+++ b/docs/usage/images.md
@@ -7,7 +7,7 @@ Adding images is very simple
Simply call the `createImage` method:
```js
-const image = doc.createImage([PATH_TO_YOUR_IMAGE]);
+const image = doc.createImage([BUFFER_OF_YOUR_IMAGE]);
```
`docx` supports `jpeg`, `jpg`, `bmp`, `gif` and `png`
@@ -43,7 +43,7 @@ interface DrawingOptions {
can be passed when creating `PictureRun()` for example:
```js
-const imageData = document.createImageData(filename, buffer, 903, 1149);
+const imageData = document.createImage(buffer, 903, 1149);
new docx.PictureRun(imageData, {
position: docx.PlacementPosition.FLOATING,
diff --git a/src/file/file.ts b/src/file/file.ts
index d7ef02b520..becc713176 100644
--- a/src/file/file.ts
+++ b/src/file/file.ts
@@ -124,20 +124,13 @@ export class File {
return this.document.createTable(rows, cols);
}
- public createImage(filePath: string): Image {
- const image = Media.addImage(this, filePath);
- this.document.addParagraph(image.Paragraph);
-
- return image;
- }
-
public addImage(image: Image): File {
this.document.addParagraph(image.Paragraph);
return this;
}
- public createImageFromBuffer(buffer: Buffer, width?: number, height?: number): Image {
- const image = Media.addImageFromBuffer(this, buffer, width, height);
+ public createImage(buffer: Buffer, width?: number, height?: number): Image {
+ const image = Media.addImage(this, buffer, width, height);
this.document.addParagraph(image.Paragraph);
return image;
diff --git a/src/file/footer-wrapper.ts b/src/file/footer-wrapper.ts
index c1080bbd4e..afd673c2b2 100644
--- a/src/file/footer-wrapper.ts
+++ b/src/file/footer-wrapper.ts
@@ -36,8 +36,8 @@ export class FooterWrapper {
this.footer.addChildElement(childElement);
}
- public createImage(image: string): void {
- const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount);
+ public createImage(image: Buffer, width?: number, height?: number): void {
+ const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount, width, height);
this.relationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
diff --git a/src/file/header-wrapper.ts b/src/file/header-wrapper.ts
index 1053bc2096..79a216610a 100644
--- a/src/file/header-wrapper.ts
+++ b/src/file/header-wrapper.ts
@@ -36,8 +36,8 @@ export class HeaderWrapper {
this.header.addChildElement(childElement);
}
- public createImage(image: string): void {
- const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount);
+ public createImage(image: Buffer, width?: number, height?: number): void {
+ const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount, width, height);
this.relationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
diff --git a/src/file/media/data.ts b/src/file/media/data.ts
index dfc7a664a0..9ac7ab5eee 100644
--- a/src/file/media/data.ts
+++ b/src/file/media/data.ts
@@ -1,5 +1,3 @@
-import * as fs from "fs";
-
export interface IMediaDataDimensions {
pixels: {
x: number;
@@ -13,7 +11,7 @@ export interface IMediaDataDimensions {
export interface IMediaData {
referenceId: number;
- stream: fs.ReadStream | Buffer;
+ stream: Buffer;
path?: string;
fileName: string;
dimensions: IMediaDataDimensions;
diff --git a/src/file/media/media.ts b/src/file/media/media.ts
index b0fb4019d8..f2cb71aa2f 100644
--- a/src/file/media/media.ts
+++ b/src/file/media/media.ts
@@ -1,6 +1,4 @@
-import * as fs from "fs";
import * as sizeOf from "image-size";
-import * as path from "path";
import { File } from "../file";
import { ImageParagraph } from "../paragraph";
@@ -12,28 +10,10 @@ interface IHackedFile {
}
export class Media {
- public static addImage(file: File, filePath: string): Image {
+ public static addImage(file: File, buffer: Buffer, width?: number, height?: number): Image {
// Workaround to expose id without exposing to API
const exposedFile = (file as {}) as IHackedFile;
- const mediaData = file.Media.addMedia(filePath, exposedFile.currentRelationshipId++);
- file.DocumentRelationships.createRelationship(
- mediaData.referenceId,
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- `media/${mediaData.fileName}`,
- );
- return new Image(new ImageParagraph(mediaData));
- }
-
- public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): Image {
- // Workaround to expose id without exposing to API
- const exposedFile = (file as {}) as IHackedFile;
- const mediaData = file.Media.addMediaFromBuffer(
- `${Media.generateId()}.png`,
- buffer,
- exposedFile.currentRelationshipId++,
- width,
- height,
- );
+ const mediaData = file.Media.addMedia(buffer, exposedFile.currentRelationshipId++, width, height);
file.DocumentRelationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
@@ -71,14 +51,8 @@ export class Media {
return data;
}
- public addMedia(filePath: string, referenceId: number): IMediaData {
- const key = path.basename(filePath);
- const dimensions = sizeOf(filePath);
- return this.createMedia(key, referenceId, dimensions, fs.createReadStream(filePath), filePath);
- }
-
- public addMediaFromBuffer(fileName: string, buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData {
- const key = fileName;
+ public addMedia(buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData {
+ const key = `${Media.generateId()}.png`;
let dimensions;
if (width && height) {
dimensions = {
@@ -96,7 +70,7 @@ export class Media {
key: string,
relationshipsCount: number,
dimensions: { width: number; height: number },
- data: fs.ReadStream | Buffer,
+ data: Buffer,
filePath?: string,
): IMediaData {
const imageData = {
From a38abeb4c2c018e69def58ff49d79ec30421be5e Mon Sep 17 00:00:00 2001
From: Dolan
Date: Tue, 14 Aug 2018 01:46:48 +0100
Subject: [PATCH 40/63] Huge refactoring to use new compiler and deprecate all
other Packers
Add new PdfPacker
---
My Document.pdf | 519 ++++++++++++++++++
demo/demo1.js | 8 +-
demo/demo26.js | 21 +
src/export/index.ts | 5 +-
src/export/packer/browser.ts | 17 -
src/export/packer/buffer-stream.ts | 28 -
src/export/packer/buffer.ts | 20 -
src/export/packer/express.spec.ts | 43 --
src/export/packer/express.ts | 32 --
src/export/packer/local.spec.ts | 66 ---
src/export/packer/local.ts | 57 --
src/export/packer/next-compiler.spec.ts | 66 +++
src/export/packer/next-compiler.ts | 63 ++-
.../packer/{buffer.spec.ts => packer.spec.ts} | 22 +-
src/export/packer/packer.ts | 38 +-
src/export/packer/pdf-packer.spec.ts | 48 ++
.../{pdf-convert-wrapper.ts => pdf-packer.ts} | 25 +-
src/export/packer/stream.ts | 25 -
src/file/drawing/drawing.spec.ts | 2 +-
19 files changed, 760 insertions(+), 345 deletions(-)
create mode 100644 My Document.pdf
create mode 100644 demo/demo26.js
delete mode 100644 src/export/packer/browser.ts
delete mode 100644 src/export/packer/buffer-stream.ts
delete mode 100644 src/export/packer/buffer.ts
delete mode 100644 src/export/packer/express.spec.ts
delete mode 100644 src/export/packer/express.ts
delete mode 100644 src/export/packer/local.spec.ts
delete mode 100644 src/export/packer/local.ts
create mode 100644 src/export/packer/next-compiler.spec.ts
rename src/export/packer/{buffer.spec.ts => packer.spec.ts} (72%)
create mode 100644 src/export/packer/pdf-packer.spec.ts
rename src/export/packer/{pdf-convert-wrapper.ts => pdf-packer.ts} (66%)
delete mode 100644 src/export/packer/stream.ts
diff --git a/My Document.pdf b/My Document.pdf
new file mode 100644
index 0000000000..0a2b5bf7bf
--- /dev/null
+++ b/My Document.pdf
@@ -0,0 +1,519 @@
+
+
+
+
+
+ Convert DOC to PDF Online | Convert DOCX to PDF Online | Convert Word to PDF Online | Free online converter
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/demo/demo1.js b/demo/demo1.js
index 53e0e58834..fc8d4c31a1 100644
--- a/demo/demo1.js
+++ b/demo/demo1.js
@@ -1,4 +1,5 @@
const docx = require('../build');
+const fs = require('fs');
var doc = new docx.Document();
@@ -10,7 +11,10 @@ paragraph.addRun(dateText);
doc.addParagraph(paragraph);
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
+var packer = new docx.Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync('My Document.docx', buffer);
+});
console.log('Document created successfully at project root!');
diff --git a/demo/demo26.js b/demo/demo26.js
new file mode 100644
index 0000000000..c88fdaea8e
--- /dev/null
+++ b/demo/demo26.js
@@ -0,0 +1,21 @@
+const docx = require('../build');
+const fs = require('fs');
+
+var doc = new docx.Document();
+
+var paragraph = new docx.Paragraph("Hello World");
+var institutionText = new docx.TextRun("University College London").bold();
+var dateText = new docx.TextRun("5th Dec 2015").tab().bold();
+paragraph.addRun(institutionText);
+paragraph.addRun(dateText);
+
+doc.addParagraph(paragraph);
+
+var packer = new docx.PdfPacker();
+
+packer.toBuffer(doc).then((buffer) => {
+ console.log(buffer);
+ fs.writeFileSync('My Document.pdf', buffer);
+});
+
+console.log('Document created successfully at project root!');
diff --git a/src/export/index.ts b/src/export/index.ts
index f6a47e4826..1a661debc6 100644
--- a/src/export/index.ts
+++ b/src/export/index.ts
@@ -1,5 +1,2 @@
-export * from "./packer/local";
-export * from "./packer/express";
export * from "./packer/packer";
-export * from "./packer/stream";
-export * from "./packer/buffer";
+export * from "./packer/pdf-packer";
diff --git a/src/export/packer/browser.ts b/src/export/packer/browser.ts
deleted file mode 100644
index ad88a5552c..0000000000
--- a/src/export/packer/browser.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { Compiler } from "./next-compiler";
-import { IPacker } from "./packer";
-
-declare var saveAs;
-
-export class BrowserPacker implements IPacker {
- private readonly packer: Compiler;
-
- public async pack(filePath: string): Promise {
- filePath = filePath.replace(/.docx$/, "");
-
- const zip = await this.packer.compile();
- const zipBlob = await zip.generateAsync({ type: "blob" });
-
- saveAs(zipBlob, `${filePath}.docx`);
- }
-}
diff --git a/src/export/packer/buffer-stream.ts b/src/export/packer/buffer-stream.ts
deleted file mode 100644
index e0d2f8ed73..0000000000
--- a/src/export/packer/buffer-stream.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import { Writable } from "stream";
-
-export class BufferStream extends Writable {
- private readonly data: Buffer[];
-
- constructor() {
- super();
-
- this.data = [];
- }
-
- // tslint:disable-next-line:no-any
- public _write(chunk: any, _: string, next: (err?: Error) => void): void {
- this.data.push(Buffer.from(chunk));
- next();
- }
-
- // tslint:disable-next-line:ban-types
- public end(cb?: Function): void {
- super.end(cb);
-
- this.emit("close");
- }
-
- public get Buffer(): Buffer {
- return Buffer.concat(this.data);
- }
-}
diff --git a/src/export/packer/buffer.ts b/src/export/packer/buffer.ts
deleted file mode 100644
index 9c46c4e166..0000000000
--- a/src/export/packer/buffer.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { File } from "../../file";
-import { BufferStream } from "./buffer-stream";
-import { Compiler } from "./compiler";
-import { IPacker } from "./packer";
-
-export class BufferPacker implements IPacker {
- private readonly packer: Compiler;
-
- constructor(file: File) {
- this.packer = new Compiler(file);
- }
-
- public async pack(): Promise {
- const stream = new BufferStream();
-
- await this.packer.compile(stream);
-
- return stream.Buffer;
- }
-}
diff --git a/src/export/packer/express.spec.ts b/src/export/packer/express.spec.ts
deleted file mode 100644
index 416d206815..0000000000
--- a/src/export/packer/express.spec.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-// tslint:disable:typedef space-before-function-paren
-// tslint:disable:no-empty
-// tslint:disable:no-any
-import { assert } from "chai";
-import { stub } from "sinon";
-
-import { ExpressPacker } from "../../export/packer/express";
-import { File, Paragraph } from "../../file";
-
-describe("LocalPacker", () => {
- let packer: ExpressPacker;
-
- beforeEach(() => {
- const file = new File({
- creator: "Dolan Miu",
- revision: "1",
- lastModifiedBy: "Dolan Miu",
- });
- const paragraph = new Paragraph("test text");
- const heading = new Paragraph("Hello world").heading1();
- file.addParagraph(new Paragraph("title").title());
- file.addParagraph(heading);
- file.addParagraph(new Paragraph("heading 2").heading2());
- file.addParagraph(paragraph);
-
- const expressResMock = {
- on: () => {},
- attachment: () => {},
- };
-
- packer = new ExpressPacker(file, expressResMock as any);
- });
-
- describe("#pack()", () => {
- it("should handle exception if it throws any", () => {
- const compiler = stub((packer as any).packer, "compile");
- compiler.throwsException();
- return packer.pack("build/tests/test").catch((error) => {
- assert.isDefined(error);
- });
- });
- });
-});
diff --git a/src/export/packer/express.ts b/src/export/packer/express.ts
deleted file mode 100644
index 3a4fa4b3ef..0000000000
--- a/src/export/packer/express.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import * as express from "express";
-
-import { File } from "file";
-import { Compiler } from "./compiler";
-import { IPacker } from "./packer";
-
-/**
- * @deprecated ExpressPacker is now deprecated. Please use the StreamPacker instead and pipe that to `express`' `res` object
- */
-export class ExpressPacker implements IPacker {
- private readonly packer: Compiler;
-
- constructor(file: File, private readonly res: express.Response) {
- this.packer = new Compiler(file);
-
- this.res = res;
-
- this.res.on("close", () => {
- return res
- .status(200)
- .send("OK")
- .end();
- });
- }
-
- public async pack(name: string): Promise {
- name = name.replace(/.docx$/, "");
-
- this.res.attachment(`${name}.docx`);
- await this.packer.compile(this.res);
- }
-}
diff --git a/src/export/packer/local.spec.ts b/src/export/packer/local.spec.ts
deleted file mode 100644
index e942fb70a9..0000000000
--- a/src/export/packer/local.spec.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-/* tslint:disable:typedef space-before-function-paren */
-import { assert } from "chai";
-import * as fs from "fs";
-import { stub } from "sinon";
-
-import { LocalPacker } from "../../export/packer/local";
-import { File, Paragraph } from "../../file";
-
-describe("LocalPacker", () => {
- let packer: LocalPacker;
-
- beforeEach(() => {
- const file = new File({
- creator: "Dolan Miu",
- revision: "1",
- lastModifiedBy: "Dolan Miu",
- });
- const paragraph = new Paragraph("test text");
- const heading = new Paragraph("Hello world").heading1();
- file.addParagraph(new Paragraph("title").title());
- file.addParagraph(heading);
- file.addParagraph(new Paragraph("heading 2").heading2());
- file.addParagraph(paragraph);
-
- packer = new LocalPacker(file);
- });
-
- describe("#pack()", () => {
- it("should create a standard docx file", async function() {
- this.timeout(99999999);
- await packer.pack("build/tests/test");
- fs.statSync("build/tests/test.docx");
- });
-
- it("should handle exception if it throws any", () => {
- // tslint:disable-next-line:no-any
- const compiler = stub((packer as any).packer, "compile");
- compiler.throwsException();
- return packer.pack("build/tests/test").catch((error) => {
- assert.isDefined(error);
- });
- });
- });
-
- describe("#packPdf", () => {
- it("should create a standard PDF file", async function() {
- this.timeout(99999999);
-
- // tslint:disable-next-line:no-any
- const pdfConverterConvert = stub((packer as any).pdfConverter, "convert");
- pdfConverterConvert.returns("Test PDF Contents");
-
- await packer.packPdf("build/tests/pdf-test");
- fs.statSync("build/tests/pdf-test.pdf");
- });
-
- it("should handle exception if it throws any", () => {
- // tslint:disable-next-line:no-any
- const compiler = stub((packer as any).packer, "compile");
- compiler.throwsException();
- return packer.packPdf("build/tests/pdf-test").catch((error) => {
- assert.isDefined(error);
- });
- });
- });
-});
diff --git a/src/export/packer/local.ts b/src/export/packer/local.ts
deleted file mode 100644
index 892a3422f1..0000000000
--- a/src/export/packer/local.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-import * as fs from "fs";
-import * as os from "os";
-import * as path from "path";
-
-import { File } from "../../file";
-import { Compiler } from "./next-compiler";
-import { IPacker } from "./packer";
-import { PdfConvertWrapper } from "./pdf-convert-wrapper";
-
-export class LocalPacker implements IPacker {
- private readonly pdfConverter: PdfConvertWrapper;
- private readonly packer: Compiler;
-
- constructor(file: File) {
- this.pdfConverter = new PdfConvertWrapper();
- this.packer = new Compiler(file);
- }
-
- public async pack(filePath: string): Promise {
- filePath = filePath.replace(/.docx$/, "");
-
- const zip = await this.packer.compile();
- const zipData = (await zip.generateAsync({ type: "base64" })) as string;
-
- await this.writeToFile(`${filePath}.docx`, zipData);
- }
-
- public async packPdf(filePath: string): Promise {
- filePath = filePath.replace(/.pdf$/, "");
-
- const fileName = path.basename(filePath, path.extname(filePath));
- const tempPath = path.join(os.tmpdir(), `${fileName}.docx`);
-
- const zip = await this.packer.compile();
- const zipData = (await zip.generateAsync({ type: "base64" })) as string;
- await this.writeToFile(tempPath, zipData);
-
- const text = await this.pdfConverter.convert(tempPath);
- // const writeFile = util.promisify(fs.writeFile); --use this in future, in 3 years time. Only in node 8
- // return writeFile(`${filePath}.pdf`, text);
-
- await this.writeToFile(`${filePath}.pdf`, text);
- }
-
- private writeToFile(filePath: string, data: string): Promise {
- const file = fs.createWriteStream(filePath);
-
- return new Promise((resolve, reject) => {
- file.write(data, "base64");
- file.end();
- file.on("finish", () => {
- resolve();
- });
- file.on("error", reject);
- });
- }
-}
diff --git a/src/export/packer/next-compiler.spec.ts b/src/export/packer/next-compiler.spec.ts
new file mode 100644
index 0000000000..fc02d4a814
--- /dev/null
+++ b/src/export/packer/next-compiler.spec.ts
@@ -0,0 +1,66 @@
+/* tslint:disable:typedef space-before-function-paren */
+import { expect } from "chai";
+import { File } from "../../file";
+import { Compiler } from "./next-compiler";
+
+describe("Compiler", () => {
+ let compiler: Compiler;
+ let file: File;
+
+ beforeEach(() => {
+ file = new File();
+ compiler = new Compiler();
+ });
+
+ describe("#compile()", () => {
+ it("should pack all the content", async function() {
+ this.timeout(99999999);
+ const zipFile = await compiler.compile(file);
+ const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
+
+ expect(fileNames).is.an.instanceof(Array);
+ expect(fileNames).has.length(17);
+ expect(fileNames).to.include("word/document.xml");
+ expect(fileNames).to.include("word/styles.xml");
+ expect(fileNames).to.include("docProps/core.xml");
+ expect(fileNames).to.include("docProps/app.xml");
+ expect(fileNames).to.include("word/numbering.xml");
+ expect(fileNames).to.include("word/header1.xml");
+ expect(fileNames).to.include("word/_rels/header1.xml.rels");
+ expect(fileNames).to.include("word/footer1.xml");
+ expect(fileNames).to.include("word/footnotes.xml");
+ expect(fileNames).to.include("word/_rels/footer1.xml.rels");
+ expect(fileNames).to.include("word/_rels/document.xml.rels");
+ expect(fileNames).to.include("[Content_Types].xml");
+ expect(fileNames).to.include("_rels/.rels");
+ });
+
+ it("should pack all additional headers and footers", async function() {
+ file.createFooter();
+ file.createFooter();
+ file.createHeader();
+ file.createHeader();
+
+ this.timeout(99999999);
+
+ const zipFile = await compiler.compile(file);
+ const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
+
+ expect(fileNames).is.an.instanceof(Array);
+ expect(fileNames).has.length(25);
+
+ expect(fileNames).to.include("word/header1.xml");
+ expect(fileNames).to.include("word/_rels/header1.xml.rels");
+ expect(fileNames).to.include("word/header2.xml");
+ expect(fileNames).to.include("word/_rels/header2.xml.rels");
+ expect(fileNames).to.include("word/header3.xml");
+ expect(fileNames).to.include("word/_rels/header3.xml.rels");
+ expect(fileNames).to.include("word/footer1.xml");
+ expect(fileNames).to.include("word/_rels/footer1.xml.rels");
+ expect(fileNames).to.include("word/footer2.xml");
+ expect(fileNames).to.include("word/_rels/footer2.xml.rels");
+ expect(fileNames).to.include("word/footer3.xml");
+ expect(fileNames).to.include("word/_rels/footer3.xml.rels");
+ });
+ });
+});
diff --git a/src/export/packer/next-compiler.ts b/src/export/packer/next-compiler.ts
index 4d26fbd5ea..f06c09e196 100644
--- a/src/export/packer/next-compiler.ts
+++ b/src/export/packer/next-compiler.ts
@@ -16,37 +16,44 @@ interface IXmlifyedFileMapping {
Numbering: IXmlifyedFile;
Relationships: IXmlifyedFile;
FileRelationships: IXmlifyedFile;
- Header: IXmlifyedFile;
- Footer: IXmlifyedFile;
- HeaderRelationships: IXmlifyedFile;
- FooterRelationships: IXmlifyedFile;
+ Headers: IXmlifyedFile[];
+ Footers: IXmlifyedFile[];
+ HeaderRelationships: IXmlifyedFile[];
+ FooterRelationships: IXmlifyedFile[];
ContentTypes: IXmlifyedFile;
AppProperties: IXmlifyedFile;
+ FootNotes: IXmlifyedFile;
}
export class Compiler {
private readonly formatter: Formatter;
- constructor(private readonly file: File) {
+ constructor() {
this.formatter = new Formatter();
}
- public async compile(): Promise {
+ public async compile(file: File): Promise {
const zip = new JSZip();
- const xmlifiedFileMapping = this.xmlifyFile(this.file);
+ const xmlifiedFileMapping = this.xmlifyFile(file);
for (const key in xmlifiedFileMapping) {
if (!xmlifiedFileMapping[key]) {
continue;
}
- const xmlifiedFile = xmlifiedFileMapping[key];
+ const obj = xmlifiedFileMapping[key] as IXmlifyedFile | IXmlifyedFile[];
- zip.file(xmlifiedFile.path, xmlifiedFile.data);
+ if (Array.isArray(obj)) {
+ for (const subFile of obj) {
+ zip.file(subFile.path, subFile.data);
+ }
+ } else {
+ zip.file(obj.path, obj.data);
+ }
}
- for (const data of this.file.Media.Array) {
+ for (const data of file.Media.Array) {
const mediaData = data.stream;
zip.file(`word/media/${data.fileName}`, mediaData);
}
@@ -85,22 +92,22 @@ export class Compiler {
data: xml(this.formatter.format(file.FileRelationships)),
path: "_rels/.rels",
},
- Header: {
- data: xml(this.formatter.format(file.Header.Header)),
- path: "word/header1.xml",
- },
- Footer: {
- data: xml(this.formatter.format(file.Footer.Footer)),
- path: "word/footer1.xml",
- },
- HeaderRelationships: {
- data: xml(this.formatter.format(file.Header.Relationships)),
- path: "word/_rels/header1.xml.rels",
- },
- FooterRelationships: {
- data: xml(this.formatter.format(file.Footer.Relationships)),
- path: "word/_rels/footer1.xml.rels",
- },
+ Headers: file.Headers.map((headerWrapper, index) => ({
+ data: xml(this.formatter.format(headerWrapper.Header)),
+ path: `word/header${index + 1}.xml`,
+ })),
+ Footers: file.Footers.map((footerWrapper, index) => ({
+ data: xml(this.formatter.format(footerWrapper.Footer)),
+ path: `word/footer${index + 1}.xml`,
+ })),
+ HeaderRelationships: file.Headers.map((headerWrapper, index) => ({
+ data: xml(this.formatter.format(headerWrapper.Relationships)),
+ path: `word/_rels/header${index + 1}.xml.rels`,
+ })),
+ FooterRelationships: file.Footers.map((footerWrapper, index) => ({
+ data: xml(this.formatter.format(footerWrapper.Relationships)),
+ path: `word/_rels/footer${index + 1}.xml.rels`,
+ })),
ContentTypes: {
data: xml(this.formatter.format(file.ContentTypes)),
path: "[Content_Types].xml",
@@ -109,6 +116,10 @@ export class Compiler {
data: xml(this.formatter.format(file.AppProperties)),
path: "docProps/app.xml",
},
+ FootNotes: {
+ data: xml(this.formatter.format(file.FootNotes)),
+ path: "word/footnotes.xml",
+ },
};
}
}
diff --git a/src/export/packer/buffer.spec.ts b/src/export/packer/packer.spec.ts
similarity index 72%
rename from src/export/packer/buffer.spec.ts
rename to src/export/packer/packer.spec.ts
index 95eccc5ecc..497e3e46b7 100644
--- a/src/export/packer/buffer.spec.ts
+++ b/src/export/packer/packer.spec.ts
@@ -2,41 +2,45 @@
import { assert } from "chai";
import { stub } from "sinon";
-import { BufferPacker } from "../../export/packer/buffer";
import { File, Paragraph } from "../../file";
+import { Packer } from "./packer";
-describe("BufferPacker", () => {
- let packer: BufferPacker;
+describe("Packer", () => {
+ let packer: Packer;
+ let file: File;
beforeEach(() => {
- const file = new File({
+ file = new File({
creator: "Dolan Miu",
revision: "1",
lastModifiedBy: "Dolan Miu",
});
const paragraph = new Paragraph("test text");
const heading = new Paragraph("Hello world").heading1();
+
file.addParagraph(new Paragraph("title").title());
file.addParagraph(heading);
file.addParagraph(new Paragraph("heading 2").heading2());
file.addParagraph(paragraph);
- packer = new BufferPacker(file);
+ packer = new Packer();
});
- describe("#pack()", () => {
+ describe("#toBuffer()", () => {
it("should create a standard docx file", async function() {
this.timeout(99999999);
- const buffer = await packer.pack();
+ const buffer = await packer.toBuffer(file);
+
assert.isDefined(buffer);
assert.isTrue(buffer.byteLength > 0);
});
it("should handle exception if it throws any", () => {
// tslint:disable-next-line:no-any
- const compiler = stub((packer as any).packer, "compile");
+ const compiler = stub((packer as any).compiler, "compile");
+
compiler.throwsException();
- return packer.pack().catch((error) => {
+ return packer.toBuffer(file).catch((error) => {
assert.isDefined(error);
});
});
diff --git a/src/export/packer/packer.ts b/src/export/packer/packer.ts
index 848924f457..e85b42c8ea 100644
--- a/src/export/packer/packer.ts
+++ b/src/export/packer/packer.ts
@@ -1,9 +1,31 @@
-export interface IPacker {
- pack(path: string): void;
-}
+import { File } from "file";
+import { Compiler } from "./next-compiler";
-// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432
-/**
- * @ignore
- */
-export const WORKAROUND = "";
+export class Packer {
+ private readonly compiler: Compiler;
+
+ constructor() {
+ this.compiler = new Compiler();
+ }
+
+ public async toBuffer(file: File): Promise {
+ const zip = await this.compiler.compile(file);
+ const zipData = (await zip.generateAsync({ type: "nodebuffer" })) as Buffer;
+
+ return zipData;
+ }
+
+ public async toBase64String(file: File): Promise {
+ const zip = await this.compiler.compile(file);
+ const zipData = (await zip.generateAsync({ type: "base64" })) as string;
+
+ return zipData;
+ }
+
+ public async toBlob(file: File): Promise {
+ const zip = await this.compiler.compile(file);
+ const zipData = (await zip.generateAsync({ type: "blob" })) as Blob;
+
+ return zipData;
+ }
+}
diff --git a/src/export/packer/pdf-packer.spec.ts b/src/export/packer/pdf-packer.spec.ts
new file mode 100644
index 0000000000..b1062b348b
--- /dev/null
+++ b/src/export/packer/pdf-packer.spec.ts
@@ -0,0 +1,48 @@
+/* tslint:disable:typedef space-before-function-paren */
+import { assert, expect } from "chai";
+import { stub } from "sinon";
+
+import { File, Paragraph } from "../../file";
+import { PdfPacker } from "./pdf-packer";
+
+describe("PdfPacker", () => {
+ let packer: PdfPacker;
+ let file: File;
+
+ beforeEach(() => {
+ file = new File({
+ creator: "Dolan Miu",
+ revision: "1",
+ lastModifiedBy: "Dolan Miu",
+ });
+ const paragraph = new Paragraph("test text");
+ const heading = new Paragraph("Hello world").heading1();
+
+ file.addParagraph(new Paragraph("title").title());
+ file.addParagraph(heading);
+ file.addParagraph(new Paragraph("heading 2").heading2());
+ file.addParagraph(paragraph);
+
+ packer = new PdfPacker();
+ });
+
+ describe("#packPdf", () => {
+ it("should create a standard PDF file", async function() {
+ this.timeout(99999999);
+ // tslint:disable-next-line:no-any
+ const pdfConverterConvert = stub((packer as any).pdfConverter, "convert");
+ pdfConverterConvert.returns(new Buffer(""));
+ const buffer = await packer.toBuffer(file);
+ expect(buffer).is.an.instanceof(Buffer);
+ });
+
+ it("should handle exception if it throws any", () => {
+ // tslint:disable-next-line:no-any
+ const compiler = stub((packer as any).packer, "toBuffer");
+ compiler.throwsException();
+ return packer.toBuffer(file).catch((error) => {
+ assert.isDefined(error);
+ });
+ });
+ });
+});
diff --git a/src/export/packer/pdf-convert-wrapper.ts b/src/export/packer/pdf-packer.ts
similarity index 66%
rename from src/export/packer/pdf-convert-wrapper.ts
rename to src/export/packer/pdf-packer.ts
index e1229cf768..9c3629ed0e 100644
--- a/src/export/packer/pdf-convert-wrapper.ts
+++ b/src/export/packer/pdf-packer.ts
@@ -1,12 +1,23 @@
-import * as fs from "fs";
import * as request from "request-promise";
-export interface IConvertOutput {
- data: string;
-}
+import { File } from "file";
+import { Packer } from "./packer";
-export class PdfConvertWrapper {
- public convert(filePath: string): request.RequestPromise {
+export class PdfPacker {
+ private readonly packer: Packer;
+
+ constructor() {
+ this.packer = new Packer();
+ }
+
+ public async toBuffer(file: File): Promise {
+ const buffer = await this.packer.toBuffer(file);
+ const text = await this.convert(buffer);
+
+ return text;
+ }
+
+ private convert(buffer: Buffer): request.RequestPromise {
return request.post({
url: "http://mirror1.convertonlinefree.com",
// tslint:disable-next-line:no-null-keyword
@@ -20,7 +31,7 @@ export class PdfConvertWrapper {
__EVENTARGUMENT: "",
__VIEWSTATE: "",
ctl00$MainContent$fu: {
- value: fs.readFileSync(filePath),
+ value: buffer,
options: {
filename: "output.docx",
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
diff --git a/src/export/packer/stream.ts b/src/export/packer/stream.ts
deleted file mode 100644
index 1182cd5dcb..0000000000
--- a/src/export/packer/stream.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import { Readable, Transform } from "stream";
-import { File } from "../../file";
-import { Compiler } from "./compiler";
-import { IPacker } from "./packer";
-
-class Pipe extends Transform {
- public _transform(chunk: Buffer | string, encoding: string, callback: () => void): void {
- this.push(chunk, encoding);
- callback();
- }
-}
-
-export class StreamPacker implements IPacker {
- private readonly compiler: Compiler;
-
- constructor(file: File) {
- this.compiler = new Compiler(file);
- }
-
- public pack(): Readable {
- const pipe = new Pipe();
- this.compiler.compile(pipe);
- return pipe;
- }
-}
diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts
index cf3f926315..3bbb3f815e 100644
--- a/src/file/drawing/drawing.spec.ts
+++ b/src/file/drawing/drawing.spec.ts
@@ -10,7 +10,7 @@ function createDrawing(drawingOptions?: IDrawingOptions): Drawing {
{
fileName: "test.jpg",
referenceId: 1,
- stream: fs.createReadStream(path),
+ stream: fs.readFileSync(path),
path: path,
dimensions: {
pixels: {
From 36157bb8db1ddfd0887c784700c57ed4d2b99774 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?=
Date: Tue, 14 Aug 2018 11:06:17 -0300
Subject: [PATCH 41/63] Added optional drawingOptions parameter to addImage and
addImageFromBuffer methods. This way it can be passaed to Image constructor.
---
src/file/media/media.ts | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/file/media/media.ts b/src/file/media/media.ts
index e813a2c051..ccd1c5b5c1 100644
--- a/src/file/media/media.ts
+++ b/src/file/media/media.ts
@@ -3,6 +3,7 @@ import * as sizeOf from "image-size";
import * as path from "path";
import { File } from "../file";
+import { IDrawingOptions } from "../drawing";
import { Image } from "../paragraph";
import { IMediaData } from "./data";
@@ -11,7 +12,7 @@ interface IHackedFile {
}
export class Media {
- public static addImage(file: File, filePath: string): Image {
+ public static addImage(file: File, filePath: string, drawingOptions?: IDrawingOptions): Image {
// Workaround to expose id without exposing to API
const exposedFile = (file as {}) as IHackedFile;
const mediaData = file.Media.addMedia(filePath, exposedFile.currentRelationshipId++);
@@ -20,10 +21,10 @@ export class Media {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
- return new Image(mediaData);
+ return new Image(mediaData, drawingOptions);
}
- public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): Image {
+ public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number, drawingOptions?: IDrawingOptions): Image {
// Workaround to expose id without exposing to API
const exposedFile = (file as {}) as IHackedFile;
const mediaData = file.Media.addMediaFromBuffer(
@@ -39,7 +40,7 @@ export class Media {
`media/${mediaData.fileName}`,
);
- return new Image(mediaData);
+ return new Image(mediaData, drawingOptions);
}
private static generateId(): string {
From 3d91c8496634d7f7f64d7b16ba489dd7f1fee6b7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?=
Date: Tue, 14 Aug 2018 11:28:01 -0300
Subject: [PATCH 42/63] alphabetized import sources
---
src/file/media/media.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/file/media/media.ts b/src/file/media/media.ts
index dcec83de47..704abba6a6 100644
--- a/src/file/media/media.ts
+++ b/src/file/media/media.ts
@@ -2,8 +2,8 @@ import * as fs from "fs";
import * as sizeOf from "image-size";
import * as path from "path";
-import { File } from "../file";
import { IDrawingOptions } from "../drawing";
+import { File } from "../file";
import { ImageParagraph } from "../paragraph";
import { IMediaData } from "./data";
import { Image } from "./image";
From bfa0edeb09c6659fbea2f2c280e7fa4c2292f61a Mon Sep 17 00:00:00 2001
From: Dolan
Date: Tue, 14 Aug 2018 21:56:38 +0100
Subject: [PATCH 43/63] Remove pdf packer
---
package.json | 2 --
src/export/index.ts | 1 -
src/export/packer/pdf-packer.spec.ts | 48 ----------------------------
src/export/packer/pdf-packer.ts | 45 --------------------------
4 files changed, 96 deletions(-)
delete mode 100644 src/export/packer/pdf-packer.spec.ts
delete mode 100644 src/export/packer/pdf-packer.ts
diff --git a/package.json b/package.json
index 5d4bd2e7a8..dbffae25cb 100644
--- a/package.json
+++ b/package.json
@@ -51,13 +51,11 @@
"@types/express": "^4.0.35",
"@types/image-size": "0.0.29",
"@types/jszip": "^3.1.3",
- "@types/request-promise": "^4.1.42",
"archiver": "^2.1.1",
"fast-xml-parser": "^3.3.6",
"image-size": "^0.6.2",
"jszip": "^3.1.5",
"request": "^2.83.0",
- "request-promise": "^4.2.2",
"xml": "^1.0.1"
},
"author": "Dolan Miu",
diff --git a/src/export/index.ts b/src/export/index.ts
index 1a661debc6..042cb4366d 100644
--- a/src/export/index.ts
+++ b/src/export/index.ts
@@ -1,2 +1 @@
export * from "./packer/packer";
-export * from "./packer/pdf-packer";
diff --git a/src/export/packer/pdf-packer.spec.ts b/src/export/packer/pdf-packer.spec.ts
deleted file mode 100644
index b1062b348b..0000000000
--- a/src/export/packer/pdf-packer.spec.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-/* tslint:disable:typedef space-before-function-paren */
-import { assert, expect } from "chai";
-import { stub } from "sinon";
-
-import { File, Paragraph } from "../../file";
-import { PdfPacker } from "./pdf-packer";
-
-describe("PdfPacker", () => {
- let packer: PdfPacker;
- let file: File;
-
- beforeEach(() => {
- file = new File({
- creator: "Dolan Miu",
- revision: "1",
- lastModifiedBy: "Dolan Miu",
- });
- const paragraph = new Paragraph("test text");
- const heading = new Paragraph("Hello world").heading1();
-
- file.addParagraph(new Paragraph("title").title());
- file.addParagraph(heading);
- file.addParagraph(new Paragraph("heading 2").heading2());
- file.addParagraph(paragraph);
-
- packer = new PdfPacker();
- });
-
- describe("#packPdf", () => {
- it("should create a standard PDF file", async function() {
- this.timeout(99999999);
- // tslint:disable-next-line:no-any
- const pdfConverterConvert = stub((packer as any).pdfConverter, "convert");
- pdfConverterConvert.returns(new Buffer(""));
- const buffer = await packer.toBuffer(file);
- expect(buffer).is.an.instanceof(Buffer);
- });
-
- it("should handle exception if it throws any", () => {
- // tslint:disable-next-line:no-any
- const compiler = stub((packer as any).packer, "toBuffer");
- compiler.throwsException();
- return packer.toBuffer(file).catch((error) => {
- assert.isDefined(error);
- });
- });
- });
-});
diff --git a/src/export/packer/pdf-packer.ts b/src/export/packer/pdf-packer.ts
deleted file mode 100644
index 9c3629ed0e..0000000000
--- a/src/export/packer/pdf-packer.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import * as request from "request-promise";
-
-import { File } from "file";
-import { Packer } from "./packer";
-
-export class PdfPacker {
- private readonly packer: Packer;
-
- constructor() {
- this.packer = new Packer();
- }
-
- public async toBuffer(file: File): Promise {
- const buffer = await this.packer.toBuffer(file);
- const text = await this.convert(buffer);
-
- return text;
- }
-
- private convert(buffer: Buffer): request.RequestPromise {
- return request.post({
- url: "http://mirror1.convertonlinefree.com",
- // tslint:disable-next-line:no-null-keyword
- encoding: null,
- headers: {
- "User-Agent":
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36",
- },
- formData: {
- __EVENTTARGET: "",
- __EVENTARGUMENT: "",
- __VIEWSTATE: "",
- ctl00$MainContent$fu: {
- value: buffer,
- options: {
- filename: "output.docx",
- contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
- },
- },
- ctl00$MainContent$btnConvert: "Convert",
- ctl00$MainContent$fuZip: "",
- },
- });
- }
-}
From 2018f41368136709e5b2c5731211b923c5d88146 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Tue, 14 Aug 2018 22:05:04 +0100
Subject: [PATCH 44/63] Discard pdf
---
My Document.pdf | 519 ------------------------------------------------
1 file changed, 519 deletions(-)
delete mode 100644 My Document.pdf
diff --git a/My Document.pdf b/My Document.pdf
deleted file mode 100644
index 0a2b5bf7bf..0000000000
--- a/My Document.pdf
+++ /dev/null
@@ -1,519 +0,0 @@
-
-
-
-
-
- Convert DOC to PDF Online | Convert DOCX to PDF Online | Convert Word to PDF Online | Free online converter
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
From 07261232ffd2b6f85e48d76715810d914f9f8022 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Wed, 15 Aug 2018 01:22:22 +0100
Subject: [PATCH 45/63] Removed old compiler and unused dependencies
---
package.json | 5 --
src/export/packer/compiler.spec.ts | 76 -------------------
src/export/packer/compiler.ts | 114 -----------------------------
3 files changed, 195 deletions(-)
delete mode 100644 src/export/packer/compiler.spec.ts
delete mode 100644 src/export/packer/compiler.ts
diff --git a/package.json b/package.json
index dbffae25cb..98acfbfe79 100644
--- a/package.json
+++ b/package.json
@@ -46,16 +46,11 @@
],
"types": "./build/index.d.ts",
"dependencies": {
- "@types/archiver": "^2.1.0",
- "@types/bluebird": "3.5.20",
- "@types/express": "^4.0.35",
"@types/image-size": "0.0.29",
"@types/jszip": "^3.1.3",
- "archiver": "^2.1.1",
"fast-xml-parser": "^3.3.6",
"image-size": "^0.6.2",
"jszip": "^3.1.5",
- "request": "^2.83.0",
"xml": "^1.0.1"
},
"author": "Dolan Miu",
diff --git a/src/export/packer/compiler.spec.ts b/src/export/packer/compiler.spec.ts
deleted file mode 100644
index 78d2339b11..0000000000
--- a/src/export/packer/compiler.spec.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-/* tslint:disable:typedef space-before-function-paren */
-import * as fs from "fs";
-import * as JSZip from "jszip";
-
-import { expect } from "chai";
-import { File } from "../../file";
-import { Compiler } from "./compiler";
-
-describe("Compiler", () => {
- let compiler: Compiler;
- let file: File;
-
- beforeEach(() => {
- file = new File();
- compiler = new Compiler(file);
- });
-
- describe("#compile()", () => {
- it("should pack all the content", async function() {
- this.timeout(99999999);
- const fileName = "build/tests/test.docx";
- await compiler.compile(fs.createWriteStream(fileName));
-
- const docxFile = fs.readFileSync(fileName);
- const zipFile: JSZip = await JSZip.loadAsync(docxFile);
- const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
-
- expect(fileNames).is.an.instanceof(Array);
- expect(fileNames).has.length(13);
- expect(fileNames).to.include("word/document.xml");
- expect(fileNames).to.include("word/styles.xml");
- expect(fileNames).to.include("docProps/core.xml");
- expect(fileNames).to.include("docProps/app.xml");
- expect(fileNames).to.include("word/numbering.xml");
- expect(fileNames).to.include("word/header1.xml");
- expect(fileNames).to.include("word/_rels/header1.xml.rels");
- expect(fileNames).to.include("word/footer1.xml");
- expect(fileNames).to.include("word/footnotes.xml");
- expect(fileNames).to.include("word/_rels/footer1.xml.rels");
- expect(fileNames).to.include("word/_rels/document.xml.rels");
- expect(fileNames).to.include("[Content_Types].xml");
- expect(fileNames).to.include("_rels/.rels");
- });
-
- it("should pack all additional headers and footers", async function() {
- file.createFooter();
- file.createFooter();
- file.createHeader();
- file.createHeader();
-
- this.timeout(99999999);
- const fileName = "build/tests/test2.docx";
- await compiler.compile(fs.createWriteStream(fileName));
-
- const docxFile = fs.readFileSync(fileName);
- const zipFile: JSZip = await JSZip.loadAsync(docxFile);
- const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
-
- expect(fileNames).is.an.instanceof(Array);
- expect(fileNames).has.length(21);
-
- expect(fileNames).to.include("word/header1.xml");
- expect(fileNames).to.include("word/_rels/header1.xml.rels");
- expect(fileNames).to.include("word/header2.xml");
- expect(fileNames).to.include("word/_rels/header2.xml.rels");
- expect(fileNames).to.include("word/header3.xml");
- expect(fileNames).to.include("word/_rels/header3.xml.rels");
- expect(fileNames).to.include("word/footer1.xml");
- expect(fileNames).to.include("word/_rels/footer1.xml.rels");
- expect(fileNames).to.include("word/footer2.xml");
- expect(fileNames).to.include("word/_rels/footer2.xml.rels");
- expect(fileNames).to.include("word/footer3.xml");
- expect(fileNames).to.include("word/_rels/footer3.xml.rels");
- });
- });
-});
diff --git a/src/export/packer/compiler.ts b/src/export/packer/compiler.ts
deleted file mode 100644
index a845833a34..0000000000
--- a/src/export/packer/compiler.ts
+++ /dev/null
@@ -1,114 +0,0 @@
-import * as archiver from "archiver";
-import * as express from "express";
-import { Writable } from "stream";
-import * as xml from "xml";
-
-import { File } from "file";
-import { Formatter } from "../formatter";
-
-export class Compiler {
- protected archive: archiver.Archiver;
- private readonly formatter: Formatter;
-
- constructor(private readonly file: File) {
- this.formatter = new Formatter();
- this.archive = archiver.create("zip", {});
-
- this.archive.on("error", (err) => {
- throw err;
- });
- }
-
- public async compile(output: Writable | express.Response): Promise {
- this.archive.pipe(output);
-
- const xmlDocument = xml(this.formatter.format(this.file.Document));
- const xmlStyles = xml(this.formatter.format(this.file.Styles));
- const xmlProperties = xml(this.formatter.format(this.file.CoreProperties), {
- declaration: {
- standalone: "yes",
- encoding: "UTF-8",
- },
- });
- const xmlNumbering = xml(this.formatter.format(this.file.Numbering));
- const xmlRelationships = xml(this.formatter.format(this.file.DocumentRelationships));
- const xmlFileRelationships = xml(this.formatter.format(this.file.FileRelationships));
- const xmlContentTypes = xml(this.formatter.format(this.file.ContentTypes));
- const xmlAppProperties = xml(this.formatter.format(this.file.AppProperties));
- const xmlFootnotes = xml(this.formatter.format(this.file.FootNotes));
-
- this.archive.append(xmlDocument, {
- name: "word/document.xml",
- });
-
- this.archive.append(xmlStyles, {
- name: "word/styles.xml",
- });
-
- this.archive.append(xmlProperties, {
- name: "docProps/core.xml",
- });
-
- this.archive.append(xmlAppProperties, {
- name: "docProps/app.xml",
- });
-
- this.archive.append(xmlNumbering, {
- name: "word/numbering.xml",
- });
-
- // headers
- for (let i = 0; i < this.file.Headers.length; i++) {
- const element = this.file.Headers[i];
- this.archive.append(xml(this.formatter.format(element.Header)), {
- name: `word/header${i + 1}.xml`,
- });
-
- this.archive.append(xml(this.formatter.format(element.Relationships)), {
- name: `word/_rels/header${i + 1}.xml.rels`,
- });
- }
-
- // footers
- for (let i = 0; i < this.file.Footers.length; i++) {
- const element = this.file.Footers[i];
- this.archive.append(xml(this.formatter.format(element.Footer)), {
- name: `word/footer${i + 1}.xml`,
- });
-
- this.archive.append(xml(this.formatter.format(element.Relationships)), {
- name: `word/_rels/footer${i + 1}.xml.rels`,
- });
- }
-
- this.archive.append(xmlFootnotes, {
- name: "word/footnotes.xml",
- });
-
- this.archive.append(xmlRelationships, {
- name: "word/_rels/document.xml.rels",
- });
-
- this.archive.append(xmlContentTypes, {
- name: "[Content_Types].xml",
- });
-
- this.archive.append(xmlFileRelationships, {
- name: "_rels/.rels",
- });
-
- for (const data of this.file.Media.Array) {
- this.archive.append(data.stream, {
- name: `word/media/${data.fileName}`,
- });
- }
-
- this.archive.finalize();
-
- return new Promise((resolve) => {
- output.on("close", () => {
- resolve();
- });
- });
- }
-}
From f969c866a78ae40b04d1e97117f7a9b7046327e4 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Wed, 15 Aug 2018 01:38:00 +0100
Subject: [PATCH 46/63] Update target for library
---
webpack.config.js | 23 ++++++++++-------------
webpack.web.config.js | 32 --------------------------------
2 files changed, 10 insertions(+), 45 deletions(-)
delete mode 100644 webpack.web.config.js
diff --git a/webpack.config.js b/webpack.config.js
index a8000404c1..01ecff1540 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -1,17 +1,17 @@
-const path = require('path');
+const path = require("path");
module.exports = {
- entry: './src/index.ts',
+ entry: "./src/index.ts",
output: {
- path: path.resolve('build'),
- filename: 'index.js',
- libraryTarget: 'umd'
+ path: path.resolve("build"),
+ filename: "index.js",
+ libraryTarget: "umd",
},
resolve: {
- extensions: ['.tsx', '.ts', '.js'],
- modules: [path.resolve('./src'), "node_modules"]
+ extensions: [".tsx", ".ts", ".js"],
+ modules: [path.resolve("./src"), "node_modules"],
},
module: {
@@ -19,13 +19,10 @@ module.exports = {
{
test: /\.ts$/,
loaders: ["awesome-typescript-loader"],
- }
+ },
],
},
- target: 'node',
-
- node: {
- __dirname: true
- }
+ // Because docx is now targetting web
+ // target: 'node',
};
diff --git a/webpack.web.config.js b/webpack.web.config.js
deleted file mode 100644
index 9672df789c..0000000000
--- a/webpack.web.config.js
+++ /dev/null
@@ -1,32 +0,0 @@
-const path = require('path');
-
-module.exports = {
- entry: './src/index.ts',
-
- output: {
- path: path.resolve('build'),
- filename: 'index.web.js',
- libraryTarget: 'umd'
- },
-
- resolve: {
- extensions: ['.tsx', '.ts', '.js'],
- modules: [path.resolve('./src'), "node_modules"]
- },
-
- module: {
- rules: [
- {
- test: /\.ts$/,
- loaders: ["awesome-typescript-loader"],
- }
- ],
- },
-
- node: {
- __dirname: true,
- fs: "empty",
- tls: "empty",
- net: "empty"
- }
-};
From bf5bcea607a14d5e81a5be4c42f21fe5f0ced3cd Mon Sep 17 00:00:00 2001
From: Dolan
Date: Wed, 15 Aug 2018 22:20:43 +0100
Subject: [PATCH 47/63] Make add image support base64 strings
---
src/file/file.ts | 2 +-
src/file/media/data.ts | 2 +-
src/file/media/media.ts | 53 ++++++++++++++++++++++++++++++-----------
3 files changed, 41 insertions(+), 16 deletions(-)
diff --git a/src/file/file.ts b/src/file/file.ts
index becc713176..9ab305fd57 100644
--- a/src/file/file.ts
+++ b/src/file/file.ts
@@ -129,7 +129,7 @@ export class File {
return this;
}
- public createImage(buffer: Buffer, width?: number, height?: number): Image {
+ public createImage(buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): Image {
const image = Media.addImage(this, buffer, width, height);
this.document.addParagraph(image.Paragraph);
diff --git a/src/file/media/data.ts b/src/file/media/data.ts
index 9ac7ab5eee..42e5874f99 100644
--- a/src/file/media/data.ts
+++ b/src/file/media/data.ts
@@ -11,7 +11,7 @@ export interface IMediaDataDimensions {
export interface IMediaData {
referenceId: number;
- stream: Buffer;
+ stream: Buffer | Uint8Array | ArrayBuffer;
path?: string;
fileName: string;
dimensions: IMediaDataDimensions;
diff --git a/src/file/media/media.ts b/src/file/media/media.ts
index f2cb71aa2f..1c8e0609dc 100644
--- a/src/file/media/media.ts
+++ b/src/file/media/media.ts
@@ -1,5 +1,3 @@
-import * as sizeOf from "image-size";
-
import { File } from "../file";
import { ImageParagraph } from "../paragraph";
import { IMediaData } from "./data";
@@ -10,7 +8,7 @@ interface IHackedFile {
}
export class Media {
- public static addImage(file: File, buffer: Buffer, width?: number, height?: number): Image {
+ public static addImage(file: File, buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): Image {
// Workaround to expose id without exposing to API
const exposedFile = (file as {}) as IHackedFile;
const mediaData = file.Media.addMedia(buffer, exposedFile.currentRelationshipId++, width, height);
@@ -51,28 +49,36 @@ export class Media {
return data;
}
- public addMedia(buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData {
+ public addMedia(
+ buffer: Buffer | string | Uint8Array | ArrayBuffer,
+ referenceId: number,
+ width: number = 100,
+ height: number = 100,
+ ): IMediaData {
const key = `${Media.generateId()}.png`;
- let dimensions;
- if (width && height) {
- dimensions = {
+
+ return this.createMedia(
+ key,
+ referenceId,
+ {
width: width,
height: height,
- };
- } else {
- dimensions = sizeOf(buffer);
- }
-
- return this.createMedia(key, referenceId, dimensions, buffer);
+ },
+ buffer,
+ );
}
private createMedia(
key: string,
relationshipsCount: number,
dimensions: { width: number; height: number },
- data: Buffer,
+ data: Buffer | string | Uint8Array | ArrayBuffer,
filePath?: string,
): IMediaData {
+ if (typeof data === "string") {
+ data = this.convertDataURIToBinary(data);
+ }
+
const imageData = {
referenceId: this.map.size + relationshipsCount + 1,
stream: data,
@@ -104,4 +110,23 @@ export class Media {
return array;
}
+
+ private convertDataURIToBinary(dataURI: string): Uint8Array {
+ // https://gist.github.com/borismus/1032746
+ // https://github.com/mafintosh/base64-to-uint8array
+ const BASE64_MARKER = ";base64,";
+
+ const base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
+
+ if (typeof atob === "function") {
+ return new Uint8Array(
+ atob(dataURI.substring(base64Index))
+ .split("")
+ .map((c) => c.charCodeAt(0)),
+ );
+ } else {
+ const b = require("buf" + "fer");
+ return new b.Buffer(dataURI, "base64");
+ }
+ }
}
From f9a0d1b3881459347b97f34a4110bc750d35a5df Mon Sep 17 00:00:00 2001
From: Dolan
Date: Wed, 15 Aug 2018 23:06:48 +0100
Subject: [PATCH 48/63] Fix tests
---
src/file/drawing/drawing.spec.ts | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts
index 3bbb3f815e..87ea5e9e44 100644
--- a/src/file/drawing/drawing.spec.ts
+++ b/src/file/drawing/drawing.spec.ts
@@ -1,16 +1,17 @@
import { assert } from "chai";
-import * as fs from "fs";
import { Utility } from "../../tests/utility";
import { Drawing, IDrawingOptions, PlacementPosition } from "./";
+const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh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JdEikroVkjFk9DcStXxpdfCD2MoXodu4RUU9ptxxmXssOfxnvDVcxRTod9FxyhqLoAqis5aPhwTDp9spRgEH2Q6KLbYoKqlaKTm6Isp0C/sJMnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`;
+
function createDrawing(drawingOptions?: IDrawingOptions): Drawing {
const path = "./demo/images/image1.jpeg";
return new Drawing(
{
fileName: "test.jpg",
referenceId: 1,
- stream: fs.readFileSync(path),
+ stream: Buffer.from(imageBase64Data, "base64"),
path: path,
dimensions: {
pixels: {
From 0411153832a01d7b2f5464f06fd0990005d02ea1 Mon Sep 17 00:00:00 2001
From: Noah Schechter
Date: Sun, 19 Aug 2018 11:59:38 -0400
Subject: [PATCH 49/63] Added in the ability to create borders for paragraphs
---
demo/demo10.js | 14 +++---
demo/demo26.js | 23 ++++++++++
src/file/paragraph/formatting/border.ts | 60 ++++++++++++++++++++-----
src/file/paragraph/paragraph.ts | 6 ++-
src/file/paragraph/properties.ts | 5 +++
5 files changed, 89 insertions(+), 19 deletions(-)
create mode 100644 demo/demo26.js
diff --git a/demo/demo10.js b/demo/demo10.js
index 2b986dbf65..3ffac93f32 100644
--- a/demo/demo10.js
+++ b/demo/demo10.js
@@ -135,7 +135,7 @@ class DocumentCreator {
for (const education of educations) {
document.addParagraph(
- this.createInstitutionHeader(education.schoolName, `${education.startDate.year} - ${education.endDate.year}`),
+ this.createInstitutionHeader(education.schoolName, `${education.startDate.year} - ${education.endDate.year}`)
);
document.addParagraph(this.createRoleText(`${education.fieldOfStudy} - ${education.degree}`));
@@ -151,8 +151,8 @@ class DocumentCreator {
document.addParagraph(
this.createInstitutionHeader(
position.company.name,
- this.createPositionDateText(position.startDate, position.endDate, position.isCurrent),
- ),
+ this.createPositionDateText(position.startDate, position.endDate, position.isCurrent)
+ )
);
document.addParagraph(this.createRoleText(position.title));
@@ -182,14 +182,14 @@ class DocumentCreator {
document.addParagraph(
new docx.Paragraph(
- "Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk",
- ),
+ "Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk"
+ )
);
document.addParagraph(new docx.Paragraph("More references upon request"));
document.addParagraph(
new docx.Paragraph(
- "This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.",
- ).center(),
+ "This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio."
+ ).center()
);
return document;
}
diff --git a/demo/demo26.js b/demo/demo26.js
new file mode 100644
index 0000000000..566c3ea419
--- /dev/null
+++ b/demo/demo26.js
@@ -0,0 +1,23 @@
+/*
+ * Creates two paragraphs, one with a border and one without
+ */
+
+const docx = require("../build");
+
+let doc = new docx.Document();
+
+let paragraph = new docx.Paragraph("No border!");
+
+doc.addParagraph(paragraph);
+
+let borderParagraph = new docx.Paragraph("I have a border on all but one side!");
+console.log(borderParagraph.Borders);
+borderParagraph.Borders.addTopBorder();
+borderParagraph.Borders.addBottomBorder();
+borderParagraph.Borders.addLeftBorder();
+console.log(borderParagraph.Borders);
+
+doc.addParagraph(borderParagraph);
+
+let exporter = new docx.LocalPacker(doc);
+exporter.packPdf('My Document');
\ No newline at end of file
diff --git a/src/file/paragraph/formatting/border.ts b/src/file/paragraph/formatting/border.ts
index 9ceb0c1336..5a07774024 100644
--- a/src/file/paragraph/formatting/border.ts
+++ b/src/file/paragraph/formatting/border.ts
@@ -1,23 +1,61 @@
// http://officeopenxml.com/WPborders.php
import { Attributes, XmlComponent } from "file/xml-components";
-class Border extends XmlComponent {
+class BorderProperty extends XmlComponent {
+ public setProperties(space: string, value: string, size: string): XmlComponent {
+ const attrs = new Attributes({
+ space: space,
+ val: value,
+ sz: size
+ });
+ this.root.push(attrs);
+
+ return this;
+ }
+}
+
+export class Border extends XmlComponent {
constructor() {
- super("w:bottom");
- this.root.push(
- new Attributes({
- color: "auto",
- space: "1",
- val: "single",
- sz: "6",
- }),
- );
+ super("w:pBdr");
+ }
+
+ public addTopBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
+ const top = new BorderProperty("w:top");
+ top.setProperties(space, value, size);
+ this.root.push(top);
+
+ return this;
+ }
+
+ public addBottomBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
+ const bottom = new BorderProperty("w:bottom");
+ bottom.setProperties(space, value, size);
+ this.root.push(bottom);
+
+ return this;
+ }
+
+ public addLeftBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
+ const left = new BorderProperty("w:left");
+ left.setProperties(space, value, size);
+ this.root.push(left);
+
+ return this;
+ }
+
+ public addRightBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
+ const right = new BorderProperty("w:right");
+ right.setProperties(space, value, size);
+ this.root.push(right);
+
+ return this;
}
}
export class ThematicBreak extends XmlComponent {
constructor() {
super("w:pBdr");
- this.root.push(new Border());
+ const bottom = new BorderProperty("w:bottom");
+ this.root.push(bottom);
}
}
diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts
index b21e2c3ac0..45693f3cf5 100644
--- a/src/file/paragraph/paragraph.ts
+++ b/src/file/paragraph/paragraph.ts
@@ -6,7 +6,7 @@ import { XmlComponent } from "file/xml-components";
import { Alignment } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
-import { ThematicBreak } from "./formatting/border";
+import { ThematicBreak, Border } from "./formatting/border";
import { Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
@@ -30,6 +30,10 @@ export class Paragraph extends XmlComponent {
}
}
+ public get Borders(): Border {
+ return this.properties.paragraphBorder;
+ }
+
public addRun(run: Run): Paragraph {
this.root.push(run);
return this;
diff --git a/src/file/paragraph/properties.ts b/src/file/paragraph/properties.ts
index 93fba4ce31..a006c40d27 100644
--- a/src/file/paragraph/properties.ts
+++ b/src/file/paragraph/properties.ts
@@ -1,9 +1,14 @@
// http://officeopenxml.com/WPparagraphProperties.php
import { XmlComponent } from "file/xml-components";
+import { Border } from "./formatting/border";
export class ParagraphProperties extends XmlComponent {
+ public paragraphBorder: Border;
+
constructor() {
super("w:pPr");
+ this.paragraphBorder = new Border();
+ this.push(this.paragraphBorder);
}
public push(item: XmlComponent): void {
From e97c432a64a3b3618dd0342b6d6643195adfd519 Mon Sep 17 00:00:00 2001
From: Noah Schechter
Date: Sun, 19 Aug 2018 19:37:36 -0400
Subject: [PATCH 50/63] added border specs, createBorder method, and color
attribute to setProperties
---
demo/demo26.js | 7 ++--
src/file/paragraph/formatting/border.ts | 22 ++++++------
src/file/paragraph/paragraph.spec.ts | 45 +++++++++++++++++++++++++
src/file/paragraph/paragraph.ts | 7 +++-
src/file/paragraph/properties.ts | 5 ++-
5 files changed, 69 insertions(+), 17 deletions(-)
diff --git a/demo/demo26.js b/demo/demo26.js
index 566c3ea419..e88469c48b 100644
--- a/demo/demo26.js
+++ b/demo/demo26.js
@@ -10,14 +10,11 @@ let paragraph = new docx.Paragraph("No border!");
doc.addParagraph(paragraph);
-let borderParagraph = new docx.Paragraph("I have a border on all but one side!");
-console.log(borderParagraph.Borders);
+let borderParagraph = new docx.Paragraph("I have borders on my top and bottom sides!").createBorder();
borderParagraph.Borders.addTopBorder();
borderParagraph.Borders.addBottomBorder();
-borderParagraph.Borders.addLeftBorder();
-console.log(borderParagraph.Borders);
doc.addParagraph(borderParagraph);
let exporter = new docx.LocalPacker(doc);
-exporter.packPdf('My Document');
\ No newline at end of file
+exporter.pack('My Document');
\ No newline at end of file
diff --git a/src/file/paragraph/formatting/border.ts b/src/file/paragraph/formatting/border.ts
index 5a07774024..f37854a3c7 100644
--- a/src/file/paragraph/formatting/border.ts
+++ b/src/file/paragraph/formatting/border.ts
@@ -2,11 +2,12 @@
import { Attributes, XmlComponent } from "file/xml-components";
class BorderProperty extends XmlComponent {
- public setProperties(space: string, value: string, size: string): XmlComponent {
+ public setProperties(color: string, space: string, value: string, size: string): XmlComponent {
const attrs = new Attributes({
+ color: color,
space: space,
val: value,
- sz: size
+ sz: size,
});
this.root.push(attrs);
@@ -19,33 +20,33 @@ export class Border extends XmlComponent {
super("w:pBdr");
}
- public addTopBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
+ public addTopBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const top = new BorderProperty("w:top");
- top.setProperties(space, value, size);
+ top.setProperties(color, space, value, size);
this.root.push(top);
return this;
}
- public addBottomBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
+ public addBottomBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const bottom = new BorderProperty("w:bottom");
- bottom.setProperties(space, value, size);
+ bottom.setProperties(color, space, value, size);
this.root.push(bottom);
return this;
}
- public addLeftBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
+ public addLeftBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const left = new BorderProperty("w:left");
- left.setProperties(space, value, size);
+ left.setProperties(color, space, value, size);
this.root.push(left);
return this;
}
- public addRightBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
+ public addRightBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const right = new BorderProperty("w:right");
- right.setProperties(space, value, size);
+ right.setProperties(color, space, value, size);
this.root.push(right);
return this;
@@ -56,6 +57,7 @@ export class ThematicBreak extends XmlComponent {
constructor() {
super("w:pBdr");
const bottom = new BorderProperty("w:bottom");
+ bottom.setProperties("auto", "1", "single", "6");
this.root.push(bottom);
}
}
diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts
index e00fa6167c..2413769b1e 100644
--- a/src/file/paragraph/paragraph.spec.ts
+++ b/src/file/paragraph/paragraph.spec.ts
@@ -144,6 +144,51 @@ describe("Paragraph", () => {
});
});
+ describe("#paragraphBorders()", () => {
+ it("should add a left and right border to a paragraph", () => {
+ paragraph.createBorder();
+ paragraph.Borders.addLeftBorder();
+ paragraph.Borders.addRightBorder();
+ const tree = new Formatter().format(paragraph);
+ expect(tree).to.deep.equal({
+ "w:p": [
+ {
+ "w:pPr": [
+ {
+ "w:pBdr": [
+ {
+ "w:left": [
+ {
+ _attr: {
+ "w:color": "auto",
+ "w:space": "1",
+ "w:sz": "6",
+ "w:val": "single",
+ },
+ },
+ ],
+ },
+ {
+ "w:right": [
+ {
+ _attr: {
+ "w:color": "auto",
+ "w:space": "1",
+ "w:sz": "6",
+ "w:val": "single",
+ },
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ });
+ });
+ });
+
describe("#pageBreak()", () => {
it("should add page break to JSON", () => {
paragraph.pageBreak();
diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts
index 45693f3cf5..70b8ea9064 100644
--- a/src/file/paragraph/paragraph.ts
+++ b/src/file/paragraph/paragraph.ts
@@ -6,7 +6,7 @@ import { XmlComponent } from "file/xml-components";
import { Alignment } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
-import { ThematicBreak, Border } from "./formatting/border";
+import { Border, ThematicBreak } from "./formatting/border";
import { Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
@@ -34,6 +34,11 @@ export class Paragraph extends XmlComponent {
return this.properties.paragraphBorder;
}
+ public createBorder(): Paragraph {
+ this.properties.createBorder();
+ return this;
+ }
+
public addRun(run: Run): Paragraph {
this.root.push(run);
return this;
diff --git a/src/file/paragraph/properties.ts b/src/file/paragraph/properties.ts
index a006c40d27..5f0c651246 100644
--- a/src/file/paragraph/properties.ts
+++ b/src/file/paragraph/properties.ts
@@ -4,10 +4,13 @@ import { Border } from "./formatting/border";
export class ParagraphProperties extends XmlComponent {
public paragraphBorder: Border;
-
+
constructor() {
super("w:pPr");
this.paragraphBorder = new Border();
+ }
+
+ public createBorder(): void {
this.push(this.paragraphBorder);
}
From a7f6224fb2df29dc620c25718a486b61f0b05164 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Tue, 21 Aug 2018 02:46:21 +0100
Subject: [PATCH 51/63] Made demo files into typescript files
---
demo/demo1.js | 20 -------
demo/demo1.ts | 20 +++++++
demo/{demo10.js => demo10.ts} | 62 +++++++++++---------
demo/{demo11.js => demo11.ts} | 52 ++++++++++-------
demo/{demo12.js => demo12.ts} | 19 +++---
demo/demo13.js | 26 ---------
demo/demo13.ts | 28 +++++++++
demo/demo14.js | 24 --------
demo/demo14.ts | 28 +++++++++
demo/demo15.js | 14 -----
demo/demo15.ts | 18 ++++++
demo/demo16.js | 36 ------------
demo/demo16.ts | 40 +++++++++++++
demo/demo17.js | 17 ------
demo/demo17.ts | 21 +++++++
demo/{demo18.js => demo18.ts} | 18 +++---
demo/demo19.js | 20 -------
demo/demo19.ts | 20 +++++++
demo/demo2.js | 74 -----------------------
demo/demo2.ts | 78 +++++++++++++++++++++++++
demo/demo20.js | 17 ------
demo/demo20.ts | 21 +++++++
demo/demo21.js | 31 ----------
demo/demo21.ts | 37 ++++++++++++
demo/demo22.js | 26 ---------
demo/demo22.ts | 27 +++++++++
demo/{demo23.js => demo23.ts} | 29 ++++-----
demo/demo24.js | 15 -----
demo/demo24.ts | 18 ++++++
demo/demo25.js | 15 -----
demo/demo26.js | 21 -------
demo/demo3.js | 45 --------------
demo/demo3.ts | 46 +++++++++++++++
demo/demo4.js | 12 ----
demo/demo4.ts | 15 +++++
demo/demo5.js | 18 ------
demo/demo5.ts | 21 +++++++
demo/demo6.js | 25 --------
demo/demo6.ts | 29 +++++++++
demo/demo7.js | 14 -----
demo/demo7.ts | 18 ++++++
demo/demo8.js | 13 -----
demo/demo8.ts | 17 ++++++
demo/demo9.js | 14 -----
demo/demo9.ts | 17 ++++++
demo/index.js | 8 ++-
package.json | 4 +-
src/file/paragraph/formatting/indent.ts | 4 +-
src/file/paragraph/paragraph.ts | 8 ++-
tsconfig.json | 3 +-
50 files changed, 638 insertions(+), 585 deletions(-)
delete mode 100644 demo/demo1.js
create mode 100644 demo/demo1.ts
rename demo/{demo10.js => demo10.ts} (83%)
rename demo/{demo11.js => demo11.ts} (75%)
rename demo/{demo12.js => demo12.ts} (50%)
delete mode 100644 demo/demo13.js
create mode 100644 demo/demo13.ts
delete mode 100644 demo/demo14.js
create mode 100644 demo/demo14.ts
delete mode 100644 demo/demo15.js
create mode 100644 demo/demo15.ts
delete mode 100644 demo/demo16.js
create mode 100644 demo/demo16.ts
delete mode 100644 demo/demo17.js
create mode 100644 demo/demo17.ts
rename demo/{demo18.js => demo18.ts} (89%)
delete mode 100644 demo/demo19.js
create mode 100644 demo/demo19.ts
delete mode 100644 demo/demo2.js
create mode 100644 demo/demo2.ts
delete mode 100644 demo/demo20.js
create mode 100644 demo/demo20.ts
delete mode 100644 demo/demo21.js
create mode 100644 demo/demo21.ts
delete mode 100644 demo/demo22.js
create mode 100644 demo/demo22.ts
rename demo/{demo23.js => demo23.ts} (82%)
delete mode 100644 demo/demo24.js
create mode 100644 demo/demo24.ts
delete mode 100644 demo/demo25.js
delete mode 100644 demo/demo26.js
delete mode 100644 demo/demo3.js
create mode 100644 demo/demo3.ts
delete mode 100644 demo/demo4.js
create mode 100644 demo/demo4.ts
delete mode 100644 demo/demo5.js
create mode 100644 demo/demo5.ts
delete mode 100644 demo/demo6.js
create mode 100644 demo/demo6.ts
delete mode 100644 demo/demo7.js
create mode 100644 demo/demo7.ts
delete mode 100644 demo/demo8.js
create mode 100644 demo/demo8.ts
delete mode 100644 demo/demo9.js
create mode 100644 demo/demo9.ts
diff --git a/demo/demo1.js b/demo/demo1.js
deleted file mode 100644
index fc8d4c31a1..0000000000
--- a/demo/demo1.js
+++ /dev/null
@@ -1,20 +0,0 @@
-const docx = require('../build');
-const fs = require('fs');
-
-var doc = new docx.Document();
-
-var paragraph = new docx.Paragraph("Hello World");
-var institutionText = new docx.TextRun("University College London").bold();
-var dateText = new docx.TextRun("5th Dec 2015").tab().bold();
-paragraph.addRun(institutionText);
-paragraph.addRun(dateText);
-
-doc.addParagraph(paragraph);
-
-var packer = new docx.Packer();
-
-packer.toBuffer(doc).then((buffer) => {
- fs.writeFileSync('My Document.docx', buffer);
-});
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo1.ts b/demo/demo1.ts
new file mode 100644
index 0000000000..ba3a0bcc2f
--- /dev/null
+++ b/demo/demo1.ts
@@ -0,0 +1,20 @@
+// Simple example to add text to a document
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun } from "../build";
+
+const doc = new Document();
+
+const paragraph = new Paragraph("Hello World");
+const institutionText = new TextRun("Foo Bar").bold();
+const dateText = new TextRun("Github is the best").tab().bold();
+paragraph.addRun(institutionText);
+paragraph.addRun(dateText);
+
+doc.addParagraph(paragraph);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo10.js b/demo/demo10.ts
similarity index 83%
rename from demo/demo10.js
rename to demo/demo10.ts
index 2b986dbf65..b55542b866 100644
--- a/demo/demo10.js
+++ b/demo/demo10.ts
@@ -1,8 +1,11 @@
-const docx = require("../build");
+// Add images to header and footer
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun } from "../build";
const PHONE_NUMBER = "07534563401";
const PROFILE_URL = "https://www.linkedin.com/in/dolan1";
-const EMAIL = "docx@docx.com";
+const EMAIL = "docx@com";
const experiences = [
{
@@ -122,13 +125,13 @@ const achievements = [
];
class DocumentCreator {
- create(data) {
+ public create(data): Document {
const experiences = data[0];
const educations = data[1];
const skills = data[2];
const achivements = data[3];
- const document = new docx.Document();
- document.addParagraph(new docx.Paragraph("Dolan Miu").title());
+ const document = new Document();
+ document.addParagraph(new Paragraph("Dolan Miu").title());
document.addParagraph(this.createContactInfo(PHONE_NUMBER, PROFILE_URL, EMAIL));
document.addParagraph(this.createHeading("Education"));
@@ -181,23 +184,23 @@ class DocumentCreator {
document.addParagraph(this.createHeading("References"));
document.addParagraph(
- new docx.Paragraph(
+ new Paragraph(
"Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk",
),
);
- document.addParagraph(new docx.Paragraph("More references upon request"));
+ document.addParagraph(new Paragraph("More references upon request"));
document.addParagraph(
- new docx.Paragraph(
+ new Paragraph(
"This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.",
).center(),
);
return document;
}
- createContactInfo(phoneNumber, profileUrl, email) {
- const paragraph = new docx.Paragraph().center();
- const contactInfo = new docx.TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`);
- const address = new docx.TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break();
+ createContactInfo(phoneNumber: string, profileUrl: string, email: string) {
+ const paragraph = new Paragraph().center();
+ const contactInfo = new TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`);
+ const address = new TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break();
paragraph.addRun(contactInfo);
paragraph.addRun(address);
@@ -206,17 +209,17 @@ class DocumentCreator {
}
createHeading(text) {
- return new docx.Paragraph(text).heading1().thematicBreak();
+ return new Paragraph(text).heading1().thematicBreak();
}
createSubHeading(text) {
- return new docx.Paragraph(text).heading2();
+ return new Paragraph(text).heading2();
}
createInstitutionHeader(institutionName, dateText) {
- const paragraph = new docx.Paragraph().maxRightTabStop();
- const institution = new docx.TextRun(institutionName).bold();
- const date = new docx.TextRun(dateText).tab().bold();
+ const paragraph = new Paragraph().maxRightTabStop();
+ const institution = new TextRun(institutionName).bold();
+ const date = new TextRun(dateText).tab().bold();
paragraph.addRun(institution);
paragraph.addRun(date);
@@ -225,8 +228,8 @@ class DocumentCreator {
}
createRoleText(roleText) {
- const paragraph = new docx.Paragraph();
- const role = new docx.TextRun(roleText).italic();
+ const paragraph = new Paragraph();
+ const role = new TextRun(roleText).italic();
paragraph.addRun(role);
@@ -234,32 +237,32 @@ class DocumentCreator {
}
createBullet(text) {
- return new docx.Paragraph(text).bullet();
+ return new Paragraph(text).bullet();
}
createSkillList(skills) {
- const paragraph = new docx.Paragraph();
+ const paragraph = new Paragraph();
const skillConcat = skills.map((skill) => skill.name).join(", ") + ".";
- paragraph.addRun(new docx.TextRun(skillConcat));
+ paragraph.addRun(new TextRun(skillConcat));
return paragraph;
}
- createAchivementsList(achivements) {
+ public createAchivementsList(achivements): Paragraph {
const arr = [];
for (const achievement of achivements) {
- arr.push(new docx.Paragraph(achievement.name).bullet());
+ arr.push(new Paragraph(achievement.name).bullet());
}
return arr;
}
createInterests(interests) {
- const paragraph = new docx.Paragraph();
+ const paragraph = new Paragraph();
- paragraph.addRun(new docx.TextRun(interests));
+ paragraph.addRun(new TextRun(interests));
return paragraph;
}
@@ -308,7 +311,8 @@ const documentCreator = new DocumentCreator();
const doc = documentCreator.create([experiences, education, skills, achievements]);
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("Dolan Miu CV");
+const packer = new Packer();
-console.log("Document created successfully at project root!");
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo11.js b/demo/demo11.ts
similarity index 75%
rename from demo/demo11.js
rename to demo/demo11.ts
index f0597e0da4..3b0cf0b01f 100644
--- a/demo/demo11.js
+++ b/demo/demo11.ts
@@ -1,7 +1,9 @@
-const docx = require("../build");
-const fs = require('fs');
+// Setting styles with JavaScript configuration
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, Table } from "../build";
-const doc = new docx.Document(undefined, {
+const doc = new Document(undefined, {
top: 700,
right: 700,
bottom: 700,
@@ -52,7 +54,7 @@ doc.Styles.createParagraphStyle("normalPara", "Normal Para")
.font("Calibri")
.quickFormat()
.leftTabStop(453.543307087)
- .maxRightTabStop(453.543307087)
+ .maxRightTabStop()
.size(26)
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 });
@@ -70,7 +72,7 @@ doc.Styles.createParagraphStyle("aside", "Aside")
.next("Normal")
.color("999999")
.italics()
- .indent(720)
+ .indent({ left: 720 })
.spacing({ line: 276 });
doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced")
@@ -105,29 +107,35 @@ doc.createParagraph("Sir,").style("normalPara");
doc.createParagraph("BRIEF DESCRIPTION").style("normalPara");
-var table = new docx.Table(4, 4);
-var contentParagraph = table
+const table = new Table(4, 4);
+table
.getRow(0)
.getCell(0)
- .addContent(new docx.Paragraph("Pole No."));
-table.properties.width = 10000;
+ .addContent(new Paragraph("Pole No."));
+table.Properties.width = 10000;
doc.addTable(table);
-var arrboth = [{
- image: "./demo/images/pizza.gif",
- comment: "Test"
-}, {
- image: "./demo/images/pizza.gif",
- comment: "Test 2"
-}];
+const arrboth = [
+ {
+ image: "./demo/images/pizza.gif",
+ comment: "Test",
+ },
+ {
+ image: "./demo/images/pizza.gif",
+ comment: "Test 2",
+ },
+];
-arrboth.forEach(function(item) {
+arrboth.forEach((item) => {
const para = doc.createParagraph();
- para.createTextRun(doc.createImage(fs.readFileSync(item.image)));
- para.properties.width = 60;
- para.properties.height = 90;
+ para.addImage(doc.createImage(fs.readFileSync(item.image)));
+ para.Properties.width = 60;
+ para.Properties.height = 90;
doc.createParagraph(item.comment).style("normalPara2");
});
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo12.js b/demo/demo12.ts
similarity index 50%
rename from demo/demo12.js
rename to demo/demo12.ts
index 87c7a405e0..a3479c3cbf 100644
--- a/demo/demo12.js
+++ b/demo/demo12.ts
@@ -1,9 +1,11 @@
-const docx = require("../build");
-const fs = require('fs');
+// Scaling images
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph } from "../build";
-var doc = new docx.Document();
+const doc = new Document();
-var paragraph = new docx.Paragraph("Hello World");
+const paragraph = new Paragraph("Hello World");
doc.addParagraph(paragraph);
const image = doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
@@ -12,11 +14,12 @@ const image3 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
const image4 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
image.scale(0.5);
-image2.scale(1)
+image2.scale(1);
image3.scale(2.5);
image4.scale(4);
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
+const packer = new Packer();
-console.log("Document created successfully at project root!");
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo13.js b/demo/demo13.js
deleted file mode 100644
index f141547d7a..0000000000
--- a/demo/demo13.js
+++ /dev/null
@@ -1,26 +0,0 @@
-// This example shows 3 styles
-const fs = require('fs');
-const docx = require('../build');
-
-const styles = fs.readFileSync('./demo/assets/custom-styles.xml', 'utf-8');
-const doc = new docx.Document({
- title: 'Title',
- externalStyles: styles
-});
-
-doc.createParagraph('Cool Heading Text').heading1();
-
-let paragraph = new docx.Paragraph('This is a custom named style from the template "MyFancyStyle"');
-paragraph.style('MyFancyStyle');
-doc.addParagraph(paragraph);
-
-doc.createParagraph('Some normal text')
-
-doc.createParagraph('MyFancyStyle again').style('MyFancyStyle');
-paragraph.style('MyFancyStyle');
-doc.addParagraph(paragraph);
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo13.ts b/demo/demo13.ts
new file mode 100644
index 0000000000..b80b9fdc22
--- /dev/null
+++ b/demo/demo13.ts
@@ -0,0 +1,28 @@
+// This example shows 3 styles using XML styles
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph } from "../build";
+
+const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8");
+const doc = new Document({
+ title: "Title",
+ externalStyles: styles,
+});
+
+doc.createParagraph("Cool Heading Text").heading1();
+
+const paragraph = new Paragraph('This is a custom named style from the template "MyFancyStyle"');
+paragraph.style("MyFancyStyle");
+doc.addParagraph(paragraph);
+
+doc.createParagraph("Some normal text");
+
+doc.createParagraph("MyFancyStyle again").style("MyFancyStyle");
+paragraph.style("MyFancyStyle");
+doc.addParagraph(paragraph);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo14.js b/demo/demo14.js
deleted file mode 100644
index b2262f106a..0000000000
--- a/demo/demo14.js
+++ /dev/null
@@ -1,24 +0,0 @@
-const docx = require('../build');
-
-var doc = new docx.Document();
-
-doc.createParagraph("First Page").pageBreak()
-doc.createParagraph("Second Page");
-
-var pageNumber = new docx.TextRun().pageNumber()
-
-var pageoneheader = new docx.Paragraph("First Page Header ").right();
-
-pageoneheader.addRun(pageNumber);
-var firstPageHeader = doc.createFirstPageHeader();
-firstPageHeader.addParagraph(pageoneheader);
-
-var pagetwoheader = new docx.Paragraph("My Title ").right();
-
-pagetwoheader.addRun(pageNumber)
-doc.Header.addParagraph(pagetwoheader)
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo14.ts b/demo/demo14.ts
new file mode 100644
index 0000000000..94d45a66a6
--- /dev/null
+++ b/demo/demo14.ts
@@ -0,0 +1,28 @@
+// Page numbers
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun } from "../build";
+
+const doc = new Document();
+
+doc.createParagraph("First Page").pageBreak();
+doc.createParagraph("Second Page");
+
+const pageNumber = new TextRun("Page ").pageNumber();
+
+const pageoneheader = new Paragraph("First Page Header ").right();
+
+pageoneheader.addRun(pageNumber);
+const firstPageHeader = doc.createFirstPageHeader();
+firstPageHeader.addParagraph(pageoneheader);
+
+const pagetwoheader = new Paragraph("My Title ").right();
+
+pagetwoheader.addRun(pageNumber);
+doc.Header.addParagraph(pagetwoheader);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo15.js b/demo/demo15.js
deleted file mode 100644
index 61d9351817..0000000000
--- a/demo/demo15.js
+++ /dev/null
@@ -1,14 +0,0 @@
-const docx = require('../build');
-
-var doc = new docx.Document();
-
-var paragraph = new docx.Paragraph("Hello World");
-var paragraph2 = new docx.Paragraph("Hello World on another page").pageBreakBefore();
-
-doc.addParagraph(paragraph);
-doc.addParagraph(paragraph2);
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo15.ts b/demo/demo15.ts
new file mode 100644
index 0000000000..5da6c826b5
--- /dev/null
+++ b/demo/demo15.ts
@@ -0,0 +1,18 @@
+// Page break before example
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph } from "../build";
+
+const doc = new Document();
+
+const paragraph = new Paragraph("Hello World");
+const paragraph2 = new Paragraph("Hello World on another page").pageBreakBefore();
+
+doc.addParagraph(paragraph);
+doc.addParagraph(paragraph2);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo16.js b/demo/demo16.js
deleted file mode 100644
index f850bc9b4b..0000000000
--- a/demo/demo16.js
+++ /dev/null
@@ -1,36 +0,0 @@
-const docx = require("../build");
-
-var doc = new docx.Document();
-
-var paragraph = new docx.Paragraph("Hello World").pageBreak();
-
-doc.addParagraph(paragraph);
-
-var header = doc.createHeader();
-header.createParagraph("Header on another page");
-var footer = doc.createFooter();
-footer.createParagraph("Footer on another page");
-
-doc.addSection({
- headerId: header.Header.ReferenceId,
- footerId: footer.Footer.ReferenceId,
- pageNumberStart: 1,
- pageNumberFormatType: docx.PageNumberFormat.DECIMAL,
-});
-
-doc.createParagraph("hello");
-
-doc.addSection({
- headerId: header.Header.ReferenceId,
- footerId: footer.Footer.ReferenceId,
- pageNumberStart: 1,
- pageNumberFormatType: docx.PageNumberFormat.DECIMAL,
- orientation: docx.PageOrientation.LANDSCAPE,
-});
-
-doc.createParagraph("hello in landscape");
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
-
-console.log("Document created successfully at project root!");
diff --git a/demo/demo16.ts b/demo/demo16.ts
new file mode 100644
index 0000000000..4b92dc0d7e
--- /dev/null
+++ b/demo/demo16.ts
@@ -0,0 +1,40 @@
+// Multiple sections and headers
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, PageNumberFormat, PageOrientation, Paragraph } from "../build";
+
+const doc = new Document();
+
+const paragraph = new Paragraph("Hello World").pageBreak();
+
+doc.addParagraph(paragraph);
+
+const header = doc.createHeader();
+header.createParagraph("Header on another page");
+const footer = doc.createFooter();
+footer.createParagraph("Footer on another page");
+
+doc.addSection({
+ headerId: header.Header.ReferenceId,
+ footerId: footer.Footer.ReferenceId,
+ pageNumberStart: 1,
+ pageNumberFormatType: PageNumberFormat.DECIMAL,
+});
+
+doc.createParagraph("hello");
+
+doc.addSection({
+ headerId: header.Header.ReferenceId,
+ footerId: footer.Footer.ReferenceId,
+ pageNumberStart: 1,
+ pageNumberFormatType: PageNumberFormat.DECIMAL,
+ orientation: PageOrientation.LANDSCAPE,
+});
+
+doc.createParagraph("hello in landscape");
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo17.js b/demo/demo17.js
deleted file mode 100644
index 266e556b7d..0000000000
--- a/demo/demo17.js
+++ /dev/null
@@ -1,17 +0,0 @@
-const docx = require('../build');
-
-var doc = new docx.Document();
-
-var paragraph = new docx.Paragraph("Hello World").referenceFootnote(1);
-var paragraph2 = new docx.Paragraph("Hello World").referenceFootnote(2);
-
-doc.addParagraph(paragraph);
-doc.addParagraph(paragraph2);
-
-doc.createFootnote(new docx.Paragraph("Test"));
-doc.createFootnote(new docx.Paragraph("My amazing reference"));
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo17.ts b/demo/demo17.ts
new file mode 100644
index 0000000000..c48a2e67e8
--- /dev/null
+++ b/demo/demo17.ts
@@ -0,0 +1,21 @@
+// Footnotes
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph } from "../build";
+
+const doc = new Document();
+
+const paragraph = new Paragraph("Hello World").referenceFootnote(1);
+const paragraph2 = new Paragraph("Hello World").referenceFootnote(2);
+
+doc.addParagraph(paragraph);
+doc.addParagraph(paragraph2);
+
+doc.createFootnote(new Paragraph("Test"));
+doc.createFootnote(new Paragraph("My amazing reference"));
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo18.js b/demo/demo18.ts
similarity index 89%
rename from demo/demo18.js
rename to demo/demo18.ts
index 73a778a6c3..4a61488c1f 100644
--- a/demo/demo18.js
+++ b/demo/demo18.ts
@@ -1,15 +1,17 @@
// Insert image from a buffer
-const docx = require('../build');
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer } from "../build";
-var doc = new docx.Document();
+const doc = new Document();
-
-const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHiAjHh4hHx8iHx8iHx8iHyAjHSAj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MnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`;
// doc.createImage(Buffer.from(imageBase64Data, 'base64'));
-doc.createImage(Buffer.from(imageBase64Data, 'base64'), 100, 100);
+doc.createImage(Buffer.from(imageBase64Data, "base64"), 100, 100);
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
+const packer = new Packer();
-console.log('Document created successfully at project root!');
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo19.js b/demo/demo19.js
deleted file mode 100644
index 3aafebc142..0000000000
--- a/demo/demo19.js
+++ /dev/null
@@ -1,20 +0,0 @@
-const fs = require("fs");
-const docx = require("../build");
-
-var doc = new docx.Document();
-
-var paragraph = new docx.Paragraph("Hello World");
-var institutionText = new docx.TextRun("Foo").bold();
-var dateText = new docx.TextRun("Bar").tab().bold();
-paragraph.addRun(institutionText);
-paragraph.addRun(dateText);
-
-doc.addParagraph(paragraph);
-
-var exporter = new docx.BufferPacker(doc);
-exporter.pack("My Document").then((buffer) => {
- // At this point, you can do anything with the buffer, including casting it to a string etc.
- console.log(buffer);
- fs.writeFileSync('My Document.docx', buffer);
- console.log("Document created successfully at project root!");
-});
diff --git a/demo/demo19.ts b/demo/demo19.ts
new file mode 100644
index 0000000000..52b5fd6694
--- /dev/null
+++ b/demo/demo19.ts
@@ -0,0 +1,20 @@
+// Export to base64 string - Useful in a browser environment.
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun } from "../build";
+
+const doc = new Document();
+
+const paragraph = new Paragraph("Hello World");
+const institutionText = new TextRun("Foo").bold();
+const dateText = new TextRun("Bar").tab().bold();
+paragraph.addRun(institutionText);
+paragraph.addRun(dateText);
+
+doc.addParagraph(paragraph);
+
+const packer = new Packer();
+
+packer.toBase64String(doc).then((str) => {
+ fs.writeFileSync("My Document.docx", str);
+});
diff --git a/demo/demo2.js b/demo/demo2.js
deleted file mode 100644
index 841e31b6bf..0000000000
--- a/demo/demo2.js
+++ /dev/null
@@ -1,74 +0,0 @@
-const docx = require('../build');
-
-const doc = new docx.Document({
- creator: 'Clippy',
- title: 'Sample Document',
- description: 'A brief example of using docx',
-});
-
-doc.Styles.createParagraphStyle('Heading1', 'Heading 1')
- .basedOn("Normal")
- .next("Normal")
- .quickFormat()
- .size(28)
- .bold()
- .italics()
- .spacing({after: 120});
-
-doc.Styles.createParagraphStyle('Heading2', 'Heading 2')
- .basedOn("Normal")
- .next("Normal")
- .quickFormat()
- .size(26)
- .bold()
- .underline('double', 'FF0000')
- .spacing({before: 240, after: 120});
-
-doc.Styles.createParagraphStyle('aside', 'Aside')
- .basedOn('Normal')
- .next('Normal')
- .color('999999')
- .italics()
- .indent(720)
- .spacing({line: 276});
-
-doc.Styles.createParagraphStyle('wellSpaced', 'Well Spaced')
- .basedOn('Normal')
- .spacing({line: 276, before: 20 * 72 * .1, after: 20 * 72 * .05});
-
-doc.Styles.createParagraphStyle('ListParagraph', 'List Paragraph')
- .quickFormat()
- .basedOn('Normal');
-
-
-const numberedAbstract = doc.Numbering.createAbstractNumbering();
-numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left");
-
-doc.createParagraph('Test heading1, bold and italicized').heading1();
-doc.createParagraph('Some simple content');
-doc.createParagraph('Test heading2 with double red underline').heading2();
-
-const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract);
-const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract);
-letterNumbering5.overrideLevel(0, 5);
-
-doc.createParagraph('Option1').setNumbering(letterNumbering, 0);
-doc.createParagraph('Option5 -- override 2 to 5').setNumbering(letterNumbering5, 0);
-doc.createParagraph('Option3').setNumbering(letterNumbering, 0);
-
-doc.createParagraph()
- .createTextRun('Some monospaced content')
- .font('Monospace');
-
-doc.createParagraph('An aside, in light gray italics and indented').style('aside');
-doc.createParagraph('This is normal, but well-spaced text').style('wellSpaced');
-const para = doc.createParagraph();
-para.createTextRun('This is a bold run,').bold();
-para.createTextRun(' switching to normal ');
-para.createTextRun('and then underlined ').underline();
-para.createTextRun('and back to normal.');
-
-const exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo2.ts b/demo/demo2.ts
new file mode 100644
index 0000000000..f32779fa06
--- /dev/null
+++ b/demo/demo2.ts
@@ -0,0 +1,78 @@
+// Example on how to customise the look at feel using Styles
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer } from "../build";
+
+const doc = new Document({
+ creator: "Clippy",
+ title: "Sample Document",
+ description: "A brief example of using docx",
+});
+
+doc.Styles.createParagraphStyle("Heading1", "Heading 1")
+ .basedOn("Normal")
+ .next("Normal")
+ .quickFormat()
+ .size(28)
+ .bold()
+ .italics()
+ .spacing({ after: 120 });
+
+doc.Styles.createParagraphStyle("Heading2", "Heading 2")
+ .basedOn("Normal")
+ .next("Normal")
+ .quickFormat()
+ .size(26)
+ .bold()
+ .underline("double", "FF0000")
+ .spacing({ before: 240, after: 120 });
+
+doc.Styles.createParagraphStyle("aside", "Aside")
+ .basedOn("Normal")
+ .next("Normal")
+ .color("999999")
+ .italics()
+ .indent({ left: 720 })
+ .spacing({ line: 276 });
+
+doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced")
+ .basedOn("Normal")
+ .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 });
+
+doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph")
+ .quickFormat()
+ .basedOn("Normal");
+
+const numberedAbstract = doc.Numbering.createAbstractNumbering();
+numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left");
+
+doc.createParagraph("Test heading1, bold and italicized").heading1();
+doc.createParagraph("Some simple content");
+doc.createParagraph("Test heading2 with double red underline").heading2();
+
+const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract);
+const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract);
+letterNumbering5.overrideLevel(0, 5);
+
+doc.createParagraph("Option1").setNumbering(letterNumbering, 0);
+doc.createParagraph("Option5 -- override 2 to 5").setNumbering(letterNumbering5, 0);
+doc.createParagraph("Option3").setNumbering(letterNumbering, 0);
+
+doc
+ .createParagraph()
+ .createTextRun("Some monospaced content")
+ .font("Monospace");
+
+doc.createParagraph("An aside, in light gray italics and indented").style("aside");
+doc.createParagraph("This is normal, but well-spaced text").style("wellSpaced");
+const para = doc.createParagraph();
+para.createTextRun("This is a bold run,").bold();
+para.createTextRun(" switching to normal ");
+para.createTextRun("and then underlined ").underline();
+para.createTextRun("and back to normal.");
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo20.js b/demo/demo20.js
deleted file mode 100644
index eef332c1ae..0000000000
--- a/demo/demo20.js
+++ /dev/null
@@ -1,17 +0,0 @@
-const docx = require("../build");
-
-var doc = new docx.Document();
-
-const table = doc.createTable(4, 4);
-table
- .getCell(2, 2)
- .addContent(new docx.Paragraph("Hello"))
- .CellProperties.Borders.addTopBorder(docx.BorderStyle.DASH_DOT_STROKED, 3, "red")
- .addBottomBorder(docx.BorderStyle.DOUBLE, 3, "blue")
- .addStartBorder(docx.BorderStyle.DOT_DOT_DASH, 3, "green")
- .addEndBorder(docx.BorderStyle.DOT_DOT_DASH, 3, "#ff8000");
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
-
-console.log("Document created successfully at project root!");
diff --git a/demo/demo20.ts b/demo/demo20.ts
new file mode 100644
index 0000000000..f3f43edb26
--- /dev/null
+++ b/demo/demo20.ts
@@ -0,0 +1,21 @@
+// Add custom borders to table cell
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { BorderStyle, Document, Packer, Paragraph } from "../build";
+
+const doc = new Document();
+
+const table = doc.createTable(4, 4);
+table
+ .getCell(2, 2)
+ .addContent(new Paragraph("Hello"))
+ .CellProperties.Borders.addTopBorder(BorderStyle.DASH_DOT_STROKED, 3, "red")
+ .addBottomBorder(BorderStyle.DOUBLE, 3, "blue")
+ .addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green")
+ .addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000");
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo21.js b/demo/demo21.js
deleted file mode 100644
index fd74e61b31..0000000000
--- a/demo/demo21.js
+++ /dev/null
@@ -1,31 +0,0 @@
-/** This demo shows how to create bookmarks then link to them with internal hyperlinks */
-
-const docx = require("../build");
-
-const loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante.";
-
-const doc = new docx.Document({
- creator: 'Clippy',
- title: 'Sample Document',
- description: 'A brief example of using docx with bookmarks and internal hyperlinks',
-});
-
-const anchorId = "anchorID";
-
-// First create the bookmark
-const bookmark = doc.createBookmark(anchorId, "Lorem Ipsum");
-// That has header styling
-doc.createParagraph().addBookmark(bookmark).heading1();
-doc.createParagraph("\n");
-
-doc.createParagraph(loremIpsum);
-doc.createParagraph().pageBreak();
-
-// Now the link back up to the bookmark
-const hyperlink = doc.createInternalHyperLink(anchorId, `Click me!`);
-doc.createParagraph().addHyperLink(hyperlink);
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
-
-console.log("Document created successfully at project root!");
diff --git a/demo/demo21.ts b/demo/demo21.ts
new file mode 100644
index 0000000000..b02da56065
--- /dev/null
+++ b/demo/demo21.ts
@@ -0,0 +1,37 @@
+// This demo shows how to create bookmarks then link to them with internal hyperlinks
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer } from "../build";
+
+const loremIpsum =
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante.";
+
+const doc = new Document({
+ creator: "Clippy",
+ title: "Sample Document",
+ description: "A brief example of using docx with bookmarks and internal hyperlinks",
+});
+
+const anchorId = "anchorID";
+
+// First create the bookmark
+const bookmark = doc.createBookmark(anchorId, "Lorem Ipsum");
+// That has header styling
+doc
+ .createParagraph()
+ .addBookmark(bookmark)
+ .heading1();
+doc.createParagraph("\n");
+
+doc.createParagraph(loremIpsum);
+doc.createParagraph().pageBreak();
+
+// Now the link back up to the bookmark
+const hyperlink = doc.createInternalHyperLink(anchorId, `Click me!`);
+doc.createParagraph().addHyperLink(hyperlink);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo22.js b/demo/demo22.js
deleted file mode 100644
index 9bc7e9ac54..0000000000
--- a/demo/demo22.js
+++ /dev/null
@@ -1,26 +0,0 @@
-const docx = require('../build');
-
-var doc = new docx.Document();
-
-
-
-var paragraph1 = new docx.Paragraph().bidirectional();
-var textRun1 = new docx.TextRun("שלום עולם").rightToLeft();
-paragraph1.addRun(textRun1);
-doc.addParagraph(paragraph1);
-
-var paragraph2 = new docx.Paragraph().bidirectional();
-var textRun2 = new docx.TextRun("שלום עולם").bold().rightToLeft();
-paragraph2.addRun(textRun2);
-doc.addParagraph(paragraph2);
-
-var paragraph3 = new docx.Paragraph().bidirectional();
-var textRun3 = new docx.TextRun("שלום עולם").italic().rightToLeft();
-paragraph3.addRun(textRun3);
-doc.addParagraph(paragraph3);
-
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo22.ts b/demo/demo22.ts
new file mode 100644
index 0000000000..c5051ab019
--- /dev/null
+++ b/demo/demo22.ts
@@ -0,0 +1,27 @@
+// This demo shows right to left for special languages
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun } from "../build";
+
+const doc = new Document();
+
+const paragraph1 = new Paragraph().bidirectional();
+const textRun1 = new TextRun("שלום עולם").rightToLeft();
+paragraph1.addRun(textRun1);
+doc.addParagraph(paragraph1);
+
+const paragraph2 = new Paragraph().bidirectional();
+const textRun2 = new TextRun("שלום עולם").bold().rightToLeft();
+paragraph2.addRun(textRun2);
+doc.addParagraph(paragraph2);
+
+const paragraph3 = new Paragraph().bidirectional();
+const textRun3 = new TextRun("שלום עולם").italic().rightToLeft();
+paragraph3.addRun(textRun3);
+doc.addParagraph(paragraph3);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo23.js b/demo/demo23.ts
similarity index 82%
rename from demo/demo23.js
rename to demo/demo23.ts
index 7fd5ebb6fb..47ee26a6f9 100644
--- a/demo/demo23.js
+++ b/demo/demo23.ts
@@ -1,19 +1,21 @@
// This demo adds an image to the Media cache, and then insert to the document afterwards
-const docx = require("../build");
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Media, Packer, Paragraph } from "../build";
-var doc = new docx.Document();
+const doc = new Document();
-var paragraph = new docx.Paragraph("Hello World");
+const paragraph = new Paragraph("Hello World");
doc.addParagraph(paragraph);
-const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg");
-const image2 = docx.Media.addImage(doc, "./demo/images/dog.png");
-const image3 = docx.Media.addImage(doc, "./demo/images/cat.jpg");
-const image4 = docx.Media.addImage(doc, "./demo/images/parrots.bmp");
-const image5 = docx.Media.addImage(doc, "./demo/images/pizza.gif");
+const image = Media.addImage(doc, "./demo/images/image1.jpeg");
+const image2 = Media.addImage(doc, "./demo/images/dog.png");
+const image3 = Media.addImage(doc, "./demo/images/cat.jpg");
+const image4 = Media.addImage(doc, "./demo/images/parrots.bmp");
+const image5 = Media.addImage(doc, "./demo/images/pizza.gif");
-const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHiAjHh4hHx8iHx8iHx8iHyAjHSAjHiAjHiAjHh4hHx8iHx8iHx8jHyAjHiAhHh4iHx8iHx8jHyAjHSAjHSAhHiAhHh4iHx8iHx8jHx8jHyAjHSAjHSAiHh4iHh4jHx8jHx8jHyAjHyAhHSAhHSAiHh4iHh4jHx8jHx8jHyAhHyAhHSAiHSAiHh4jHh4jHx8jHx8jHyAhHyAhHSAiHSAjHR4jHh4jHx8jHx8hHyA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elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`;
+const image6 = Media.addImage(doc, Buffer.from(imageBase64Data, "base64"), 100, 100);
// I am adding an image to the paragraph rather than the document to make the image inline
paragraph.addImage(image5);
@@ -25,7 +27,8 @@ doc.addImage(image4);
doc.addImage(image5);
doc.addImage(image6);
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
+const packer = new Packer();
-console.log("Document created successfully at project root!");
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo24.js b/demo/demo24.js
deleted file mode 100644
index a95189a6ed..0000000000
--- a/demo/demo24.js
+++ /dev/null
@@ -1,15 +0,0 @@
-// Add image to table cell
-const docx = require('../build');
-
-var doc = new docx.Document();
-
-const table = doc.createTable(4, 4);
-table.getCell(2, 2).addContent(new docx.Paragraph('Hello'));
-
-const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg");
-table.getCell(1, 1).addContent(image);
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo24.ts b/demo/demo24.ts
new file mode 100644
index 0000000000..23828aec65
--- /dev/null
+++ b/demo/demo24.ts
@@ -0,0 +1,18 @@
+// Add image to table cell
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Media, Packer, Paragraph } from "../build";
+
+const doc = new Document();
+
+const table = doc.createTable(4, 4);
+table.getCell(2, 2).addContent(new Paragraph("Hello"));
+
+const image = Media.addImage(doc, "./demo/images/image1.jpeg");
+table.getCell(1, 1).addContent(image.Paragraph);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo25.js b/demo/demo25.js
deleted file mode 100644
index 85dd29febe..0000000000
--- a/demo/demo25.js
+++ /dev/null
@@ -1,15 +0,0 @@
-const fs = require("fs");
-const docx = require("../build");
-
-var doc = new docx.Document();
-
-var paragraph = new docx.Paragraph("Hello World");
-var institutionText = new docx.TextRun("Foo").bold();
-var dateText = new docx.TextRun("Bar").tab().bold();
-paragraph.addRun(institutionText);
-paragraph.addRun(dateText);
-
-doc.addParagraph(paragraph);
-
-var exporter = new docx.LocalPacker(doc);
-exporter.packPdf("My Document");
diff --git a/demo/demo26.js b/demo/demo26.js
deleted file mode 100644
index c88fdaea8e..0000000000
--- a/demo/demo26.js
+++ /dev/null
@@ -1,21 +0,0 @@
-const docx = require('../build');
-const fs = require('fs');
-
-var doc = new docx.Document();
-
-var paragraph = new docx.Paragraph("Hello World");
-var institutionText = new docx.TextRun("University College London").bold();
-var dateText = new docx.TextRun("5th Dec 2015").tab().bold();
-paragraph.addRun(institutionText);
-paragraph.addRun(dateText);
-
-doc.addParagraph(paragraph);
-
-var packer = new docx.PdfPacker();
-
-packer.toBuffer(doc).then((buffer) => {
- console.log(buffer);
- fs.writeFileSync('My Document.pdf', buffer);
-});
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo3.js b/demo/demo3.js
deleted file mode 100644
index accd597905..0000000000
--- a/demo/demo3.js
+++ /dev/null
@@ -1,45 +0,0 @@
-const docx = require('../build');
-
-var doc = new docx.Document();
-
-const numbering = new docx.Numbering();
-
-const abstractNum = numbering.createAbstractNumbering();
-abstractNum.createLevel(0, "upperRoman", "%1", "start")
- .addParagraphProperty(new docx.Indent(720, 260));
-abstractNum.createLevel(1, "decimal", "%2.", "start")
- .addParagraphProperty(new docx.Indent(1440, 980));
-abstractNum.createLevel(2, "lowerLetter", "%3)", "start")
- .addParagraphProperty(new docx.Indent(2160, 1700));
-
-const concrete = numbering.createConcreteNumbering(abstractNum);
-
-var topLevelP = new docx.Paragraph("Hey you");
-var subP = new docx.Paragraph("What's up fam");
-var secondSubP = new docx.Paragraph("Hello World 2");
-var subSubP = new docx.Paragraph("Yeah boi");
-
-topLevelP.setNumbering(concrete, 0);
-subP.setNumbering(concrete, 1);
-secondSubP.setNumbering(concrete, 1);
-subSubP.setNumbering(concrete, 2);
-
-doc.addParagraph(topLevelP);
-doc.addParagraph(subP);
-doc.addParagraph(secondSubP);
-doc.addParagraph(subSubP);
-
-var bullet1 = new docx.Paragraph("Hey you").bullet();
-var bullet2 = new docx.Paragraph("What's up fam").bullet(1);
-var bullet3 = new docx.Paragraph("Hello World 2").bullet(2);
-var bullet4 = new docx.Paragraph("Yeah boi").bullet(3);
-
-doc.addParagraph(bullet1);
-doc.addParagraph(bullet2);
-doc.addParagraph(bullet3);
-doc.addParagraph(bullet4);
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo3.ts b/demo/demo3.ts
new file mode 100644
index 0000000000..ac3680ef32
--- /dev/null
+++ b/demo/demo3.ts
@@ -0,0 +1,46 @@
+// Numbering and bullet points example
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Indent, Numbering, Packer, Paragraph } from "../build";
+
+const doc = new Document();
+
+const numbering = new Numbering();
+
+const abstractNum = numbering.createAbstractNumbering();
+abstractNum.createLevel(0, "upperRoman", "%1", "start").addParagraphProperty(new Indent({ left: 720, hanging: 260 }));
+abstractNum.createLevel(1, "decimal", "%2.", "start").addParagraphProperty(new Indent({ left: 1440, hanging: 980 }));
+abstractNum.createLevel(2, "lowerLetter", "%3)", "start").addParagraphProperty(new Indent({ left: 14402160, hanging: 1700 }));
+
+const concrete = numbering.createConcreteNumbering(abstractNum);
+
+const topLevelP = new Paragraph("Hey you");
+const subP = new Paragraph("What's up fam");
+const secondSubP = new Paragraph("Hello World 2");
+const subSubP = new Paragraph("Yeah boi");
+
+topLevelP.setNumbering(concrete, 0);
+subP.setNumbering(concrete, 1);
+secondSubP.setNumbering(concrete, 1);
+subSubP.setNumbering(concrete, 2);
+
+doc.addParagraph(topLevelP);
+doc.addParagraph(subP);
+doc.addParagraph(secondSubP);
+doc.addParagraph(subSubP);
+
+const bullet1 = new Paragraph("Hey you").bullet();
+const bullet2 = new Paragraph("What's up fam").bullet(1);
+const bullet3 = new Paragraph("Hello World 2").bullet(2);
+const bullet4 = new Paragraph("Yeah boi").bullet(3);
+
+doc.addParagraph(bullet1);
+doc.addParagraph(bullet2);
+doc.addParagraph(bullet3);
+doc.addParagraph(bullet4);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo4.js b/demo/demo4.js
deleted file mode 100644
index 6ded74254c..0000000000
--- a/demo/demo4.js
+++ /dev/null
@@ -1,12 +0,0 @@
-const docx = require('../build');
-
-var doc = new docx.Document();
-
-const table = doc.createTable(4, 4);
-table.getCell(2, 2).addContent(new docx.Paragraph('Hello'));
-
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo4.ts b/demo/demo4.ts
new file mode 100644
index 0000000000..eb6f5b8c09
--- /dev/null
+++ b/demo/demo4.ts
@@ -0,0 +1,15 @@
+// Example of how you would create a table and add data to it
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph } from "../build";
+
+const doc = new Document();
+
+const table = doc.createTable(4, 4);
+table.getCell(2, 2).addContent(new Paragraph("Hello"));
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo5.js b/demo/demo5.js
deleted file mode 100644
index fbb2cd0eaf..0000000000
--- a/demo/demo5.js
+++ /dev/null
@@ -1,18 +0,0 @@
-const docx = require("../build");
-const fs = require('fs');
-
-var doc = new docx.Document();
-
-var paragraph = new docx.Paragraph("Hello World");
-doc.addParagraph(paragraph);
-
-doc.createImage(fs.readFileSync("./demo/images/image1.jpeg"));
-doc.createImage(fs.readFileSync("./demo/images/dog.png"));
-doc.createImage(fs.readFileSync("./demo/images/cat.jpg"));
-doc.createImage(fs.readFileSync("./demo/images/parrots.bmp"));
-doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
-
-console.log("Document created successfully at project root!");
diff --git a/demo/demo5.ts b/demo/demo5.ts
new file mode 100644
index 0000000000..e56cdfc01e
--- /dev/null
+++ b/demo/demo5.ts
@@ -0,0 +1,21 @@
+// Example of how to add images to the document - You can use Buffers, UInt8Arrays or Base64 strings
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph } from "../build";
+
+const doc = new Document();
+
+const paragraph = new Paragraph("Hello World");
+doc.addParagraph(paragraph);
+
+doc.createImage(fs.readFileSync("./demo/images/image1.jpeg"));
+doc.createImage(fs.readFileSync("./demo/images/dog.png").toString("base64"));
+doc.createImage(fs.readFileSync("./demo/images/cat.jpg"));
+doc.createImage(fs.readFileSync("./demo/images/parrots.bmp"));
+doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo6.js b/demo/demo6.js
deleted file mode 100644
index 939f590d05..0000000000
--- a/demo/demo6.js
+++ /dev/null
@@ -1,25 +0,0 @@
-const docx = require("../build");
-
-var doc = new docx.Document(undefined, {
- top: 0,
- right: 0,
- bottom: 0,
- left: 0,
-});
-
-var paragraph = new docx.Paragraph("Hello World");
-var institutionText = new docx.TextRun("University College London").bold();
-var dateText = new docx.TextRun("5th Dec 2015").tab().bold();
-paragraph.addRun(institutionText);
-paragraph.addRun(dateText);
-
-doc.addParagraph(paragraph);
-
-doc.createParagraph("Hello World").heading1();
-doc.createParagraph("University College London");
-doc.createParagraph("5th Dec 2015");
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
-
-console.log("Document created successfully at project root!");
diff --git a/demo/demo6.ts b/demo/demo6.ts
new file mode 100644
index 0000000000..5c9ff2798f
--- /dev/null
+++ b/demo/demo6.ts
@@ -0,0 +1,29 @@
+// Example of how to change page borders
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun } from "../build";
+
+const doc = new Document(undefined, {
+ top: 0,
+ right: 0,
+ bottom: 0,
+ left: 0,
+});
+
+const paragraph = new Paragraph("Hello World");
+const institutionText = new TextRun("Foo bar").bold();
+const dateText = new TextRun("Github is the best").tab().bold();
+paragraph.addRun(institutionText);
+paragraph.addRun(dateText);
+
+doc.addParagraph(paragraph);
+
+doc.createParagraph("Hello World").heading1();
+doc.createParagraph("Foo bar");
+doc.createParagraph("Github is the best");
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo7.js b/demo/demo7.js
deleted file mode 100644
index f60dcd1911..0000000000
--- a/demo/demo7.js
+++ /dev/null
@@ -1,14 +0,0 @@
-const docx = require("../build");
-
-var doc = new docx.Document(undefined, {
- orientation: "landscape",
-});
-
-var paragraph = new docx.Paragraph("Hello World");
-
-doc.addParagraph(paragraph);
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack("My Document");
-
-console.log("Document created successfully at project root!");
diff --git a/demo/demo7.ts b/demo/demo7.ts
new file mode 100644
index 0000000000..fe9fccf928
--- /dev/null
+++ b/demo/demo7.ts
@@ -0,0 +1,18 @@
+// Example of how to set the document to landscape
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, PageOrientation, Paragraph } from "../build";
+
+const doc = new Document(undefined, {
+ orientation: PageOrientation.LANDSCAPE,
+});
+
+const paragraph = new Paragraph("Hello World");
+
+doc.addParagraph(paragraph);
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo8.js b/demo/demo8.js
deleted file mode 100644
index 03ba944473..0000000000
--- a/demo/demo8.js
+++ /dev/null
@@ -1,13 +0,0 @@
-const docx = require('../build');
-
-var doc = new docx.Document();
-
-doc.createParagraph("Hello World");
-
-doc.Header.createParagraph("Header text");
-doc.Footer.createParagraph("Footer text");
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
\ No newline at end of file
diff --git a/demo/demo8.ts b/demo/demo8.ts
new file mode 100644
index 0000000000..c4fb96a78c
--- /dev/null
+++ b/demo/demo8.ts
@@ -0,0 +1,17 @@
+// Add text to header and footer
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer } from "../build";
+
+const doc = new Document();
+
+doc.createParagraph("Hello World");
+
+doc.Header.createParagraph("Header text");
+doc.Footer.createParagraph("Footer text");
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/demo9.js b/demo/demo9.js
deleted file mode 100644
index c0a57cf8f6..0000000000
--- a/demo/demo9.js
+++ /dev/null
@@ -1,14 +0,0 @@
-const docx = require('../build');
-const fs = require('fs');
-
-var doc = new docx.Document();
-
-doc.createParagraph("Hello World");
-
-doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif"));
-doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif"));
-
-var exporter = new docx.LocalPacker(doc);
-exporter.pack('My Document');
-
-console.log('Document created successfully at project root!');
diff --git a/demo/demo9.ts b/demo/demo9.ts
new file mode 100644
index 0000000000..87fe9b5192
--- /dev/null
+++ b/demo/demo9.ts
@@ -0,0 +1,17 @@
+// Add images to header and footer
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer } from "../build";
+
+const doc = new Document();
+
+doc.createParagraph("Hello World");
+
+doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif"));
+doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif"));
+
+const packer = new Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/index.js b/demo/index.js
index 8ae9aac536..e6c7d926d0 100644
--- a/demo/index.js
+++ b/demo/index.js
@@ -18,12 +18,16 @@ prompt.start();
prompt.get(schema, function (err, result) {
var demoNumber = result.number;
- var filePath = `./demo/demo${demoNumber}.js`;
+ var filePath = `./demo/demo${demoNumber}.ts`;
if (!fs.existsSync(filePath)) {
console.error(`demo${demoNumber} does not exist: ${filePath}`);
return;
}
console.log(`Running demo ${demoNumber}`);
- shelljs.exec(`node ${filePath}`);
+ if (shelljs.exec(`npm run ts-node -- ${filePath}`).code === 0) {
+ console.log("Document created successfully");
+ } else {
+ console.error('Something went wrong with the demo');
+ }
});
diff --git a/package.json b/package.json
index 98acfbfe79..3132ae6445 100644
--- a/package.json
+++ b/package.json
@@ -17,7 +17,8 @@
"typedoc": "typedoc src/index.ts",
"style": "prettier -l \"src/**/*.ts\"",
"style.fix": "prettier \"src/**/*.ts\" --write",
- "fix-types": "node types-absolute-fixer.js"
+ "fix-types": "node types-absolute-fixer.js",
+ "ts-node": "ts-node"
},
"pre-commit": [
"style",
@@ -76,6 +77,7 @@
"rimraf": "^2.5.2",
"shelljs": "^0.7.7",
"sinon": "^5.0.7",
+ "ts-node": "^7.0.1",
"tslint": "^5.11.0",
"typedoc": "^0.11.1",
"typescript": "2.9.2",
diff --git a/src/file/paragraph/formatting/indent.ts b/src/file/paragraph/formatting/indent.ts
index 0b42246689..cd7528d2b8 100644
--- a/src/file/paragraph/formatting/indent.ts
+++ b/src/file/paragraph/formatting/indent.ts
@@ -1,7 +1,7 @@
// http://officeopenxml.com/WPindentation.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
-interface IIndentAttributesProperties {
+export interface IIndentAttributesProperties {
left?: number;
hanging?: number;
firstLine?: number;
@@ -20,7 +20,7 @@ class IndentAttributes extends XmlAttributeComponent
Date: Tue, 21 Aug 2018 10:27:46 +0100
Subject: [PATCH 52/63] Update .travis.yml
---
.travis.yml | 36 ++++++++++++++++++++++++------------
1 file changed, 24 insertions(+), 12 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 986233743d..373070eeb8 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,18 +8,30 @@ script:
- npm test
- npm run style
- npm run build
- - node ./demo/demo1.js
- - node ./demo/demo2.js
- - node ./demo/demo3.js
- - node ./demo/demo4.js
- - node ./demo/demo5.js
- - node ./demo/demo6.js
- - node ./demo/demo7.js
- - node ./demo/demo8.js
- - node ./demo/demo9.js
- - node ./demo/demo10.js
- - node ./demo/demo11.js
- - node ./demo/demo12.js
+ - npm run ts-node -- ./demo/demo1.ts
+ - npm run ts-node -- ./demo/demo2.ts
+ - npm run ts-node -- ./demo/demo3.ts
+ - npm run ts-node -- ./demo/demo4.ts
+ - npm run ts-node -- ./demo/demo5.ts
+ - npm run ts-node -- ./demo/demo6.ts
+ - npm run ts-node -- ./demo/demo7.ts
+ - npm run ts-node -- ./demo/demo8.ts
+ - npm run ts-node -- ./demo/demo9.ts
+ - npm run ts-node -- ./demo/demo10.ts
+ - npm run ts-node -- ./demo/demo11.ts
+ - npm run ts-node -- ./demo/demo12.ts
+ - npm run ts-node -- ./demo/demo13.ts
+ - npm run ts-node -- ./demo/demo14.ts
+ - npm run ts-node -- ./demo/demo15.ts
+ - npm run ts-node -- ./demo/demo16.ts
+ - npm run ts-node -- ./demo/demo17.ts
+ - npm run ts-node -- ./demo/demo18.ts
+ - npm run ts-node -- ./demo/demo19.ts
+ - npm run ts-node -- ./demo/demo20.ts
+ - npm run ts-node -- ./demo/demo21.ts
+ - npm run ts-node -- ./demo/demo22.ts
+ - npm run ts-node -- ./demo/demo23.ts
+ - npm run ts-node -- ./demo/demo24.ts
after_failure:
- "cat /home/travis/builds/dolanmiu/docx/npm-debug.log"
after_success:
From a777842f9d3488562770f202e663df069bd5e290 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?=
Date: Tue, 21 Aug 2018 07:16:27 -0300
Subject: [PATCH 53/63] updated mocha to version 5 due to vulnerabilities found
by npm audit
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index 52b8d3fc56..c89bbd2de6 100644
--- a/package.json
+++ b/package.json
@@ -71,7 +71,7 @@
"chai": "^3.5.0",
"glob": "^7.1.2",
"jszip": "^3.1.5",
- "mocha": "^3.2.0",
+ "mocha": "^5.2.0",
"mocha-webpack": "^1.0.1",
"pre-commit": "^1.2.2",
"prettier": "^1.12.1",
From 410080a8a3ccb6bd843ae85a0587fa4e8fe53b07 Mon Sep 17 00:00:00 2001
From: Dolan
Date: Tue, 21 Aug 2018 19:27:07 +0100
Subject: [PATCH 54/63] Fix tests
---
demo/demo10.ts | 48 +++++++++++++++++++++++++----------------
demo/demo11.ts | 8 +++----
src/file/table/table.ts | 4 ++++
3 files changed, 37 insertions(+), 23 deletions(-)
diff --git a/demo/demo10.ts b/demo/demo10.ts
index b55542b866..84713e777f 100644
--- a/demo/demo10.ts
+++ b/demo/demo10.ts
@@ -3,6 +3,8 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "../build";
+// tslint:disable:no-shadowed-variable
+
const PHONE_NUMBER = "07534563401";
const PROFILE_URL = "https://www.linkedin.com/in/dolan1";
const EMAIL = "docx@com";
@@ -125,11 +127,13 @@ const achievements = [
];
class DocumentCreator {
- public create(data): Document {
- const experiences = data[0];
- const educations = data[1];
- const skills = data[2];
- const achivements = data[3];
+ public create(data: object[]): Document {
+ // tslint:disable-next-line:no-any
+ const experiences = data[0] as any[];
+ // tslint:disable-next-line:no-any
+ const educations = data[1] as any[];
+ const skills = data[2] as object[];
+ const achivements = data[3] as object[];
const document = new Document();
document.addParagraph(new Paragraph("Dolan Miu").title());
@@ -197,7 +201,7 @@ class DocumentCreator {
return document;
}
- createContactInfo(phoneNumber: string, profileUrl: string, email: string) {
+ public createContactInfo(phoneNumber: string, profileUrl: string, email: string): Paragraph {
const paragraph = new Paragraph().center();
const contactInfo = new TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`);
const address = new TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break();
@@ -208,15 +212,15 @@ class DocumentCreator {
return paragraph;
}
- createHeading(text) {
+ public createHeading(text: string): Paragraph {
return new Paragraph(text).heading1().thematicBreak();
}
- createSubHeading(text) {
+ public createSubHeading(text: string): Paragraph {
return new Paragraph(text).heading2();
}
- createInstitutionHeader(institutionName, dateText) {
+ public createInstitutionHeader(institutionName: string, dateText: string): Paragraph {
const paragraph = new Paragraph().maxRightTabStop();
const institution = new TextRun(institutionName).bold();
const date = new TextRun(dateText).tab().bold();
@@ -227,7 +231,7 @@ class DocumentCreator {
return paragraph;
}
- createRoleText(roleText) {
+ public createRoleText(roleText: string): Paragraph {
const paragraph = new Paragraph();
const role = new TextRun(roleText).italic();
@@ -236,11 +240,12 @@ class DocumentCreator {
return paragraph;
}
- createBullet(text) {
+ public createBullet(text: string): Paragraph {
return new Paragraph(text).bullet();
}
- createSkillList(skills) {
+ // tslint:disable-next-line:no-any
+ public createSkillList(skills: any[]): Paragraph {
const paragraph = new Paragraph();
const skillConcat = skills.map((skill) => skill.name).join(", ") + ".";
@@ -249,35 +254,38 @@ class DocumentCreator {
return paragraph;
}
- public createAchivementsList(achivements): Paragraph {
- const arr = [];
+ // tslint:disable-next-line:no-any
+ public createAchivementsList(achivements: any[]): Paragraph[] {
+ const arr: Paragraph[] = [];
for (const achievement of achivements) {
- arr.push(new Paragraph(achievement.name).bullet());
+ const paragraph = new Paragraph(achievement.name).bullet();
+ arr.push(paragraph);
}
return arr;
}
- createInterests(interests) {
+ public createInterests(interests: string): Paragraph {
const paragraph = new Paragraph();
paragraph.addRun(new TextRun(interests));
return paragraph;
}
- splitParagraphIntoBullets(text) {
+ public splitParagraphIntoBullets(text: string): string[] {
return text.split("\n\n");
}
- createPositionDateText(startDate, endDate, isCurrent) {
+ // tslint:disable-next-line:no-any
+ public createPositionDateText(startDate: any, endDate: any, isCurrent: boolean): string {
const startDateText = this.getMonthFromInt(startDate.month) + ". " + startDate.year;
const endDateText = isCurrent ? "Present" : `${this.getMonthFromInt(endDate.month)}. ${endDate.year}`;
return `${startDateText} - ${endDateText}`;
}
- getMonthFromInt(value) {
+ public getMonthFromInt(value: number): string {
switch (value) {
case 1:
return "Jan";
@@ -303,6 +311,8 @@ class DocumentCreator {
return "Nov";
case 12:
return "Dec";
+ default:
+ return "N/A";
}
}
}
diff --git a/demo/demo11.ts b/demo/demo11.ts
index 3b0cf0b01f..76ec915060 100644
--- a/demo/demo11.ts
+++ b/demo/demo11.ts
@@ -18,7 +18,7 @@ doc.Styles.createParagraphStyle("Heading1", "Heading 1")
.size(52)
.center()
.bold()
- .color(000000)
+ .color("000000")
.spacing({ line: 340 })
.underline("single", "000000");
@@ -112,7 +112,7 @@ table
.getRow(0)
.getCell(0)
.addContent(new Paragraph("Pole No."));
-table.Properties.width = 10000;
+// table.Properties.width = 10000;
doc.addTable(table);
const arrboth = [
@@ -129,8 +129,8 @@ const arrboth = [
arrboth.forEach((item) => {
const para = doc.createParagraph();
para.addImage(doc.createImage(fs.readFileSync(item.image)));
- para.Properties.width = 60;
- para.Properties.height = 90;
+ // para.Properties.width = 60;
+ // para.Properties.height = 90;
doc.createParagraph(item.comment).style("normalPara2");
});
diff --git a/src/file/table/table.ts b/src/file/table/table.ts
index d82da34ff2..335f5fd1c2 100644
--- a/src/file/table/table.ts
+++ b/src/file/table/table.ts
@@ -76,6 +76,10 @@ export class Table extends XmlComponent {
this.properties.setFixedWidthLayout();
return this;
}
+
+ public get Properties(): TableProperties {
+ return this.properties;
+ }
}
export class TableRow extends XmlComponent {
From 6f0f3ccca737360fd466c7a5fc73a6c42aae704a Mon Sep 17 00:00:00 2001
From: Dolan
Date: Tue, 21 Aug 2018 21:46:12 +0100
Subject: [PATCH 55/63] Update documentation
---
docs/usage/packers.md | 57 +++++++++++++++++++++++++++++++++++--------
1 file changed, 47 insertions(+), 10 deletions(-)
diff --git a/docs/usage/packers.md b/docs/usage/packers.md
index db837702de..11f107390d 100644
--- a/docs/usage/packers.md
+++ b/docs/usage/packers.md
@@ -2,7 +2,48 @@
> Packers are the way in which `docx` turns your code into `.docx` format. It is completely decoupled from the `docx.Document`.
-## File System Packer
+## 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.
+
+### Export as Buffer
+
+This will return a NodeJS `Buffer`. If this is used in the browser, it will return a `UInt8Array` instead.
+
+```js
+const packer = new docx.Packer();
+
+packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
+```
+
+### Export as a `base64` string
+
+```js
+const packer = new docx.Packer();
+
+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.
+
+```js
+const packer = new docx.Packer();
+
+packer.toBlob(doc).then((blob) => {
+ // saveAs from FileSaver will download the file
+ saveAs(blob, "example.docx");
+});
+```
+
+## Version 3 and below
+
+### File System Packer
```js
const docx = require("docx");
@@ -13,7 +54,7 @@ exporter.pack("My Document");
// Word Document is in file system
```
-## Buffer Packer
+### Buffer Packer
```js
const docx = require("docx");
@@ -23,7 +64,7 @@ const exporter = new docx.BufferPacker(doc);
const buffer = exporter.pack();
```
-## Stream Packer
+### Stream Packer
Creates a `node` `Readable` stream
@@ -35,7 +76,7 @@ const exporter = new docx.StreamPacker(doc);
const stream = exporter.pack();
```
-## Express Packer
+### Express Packer
The old express packer is now deprecated and may disappear soon, so you should upgrade.
@@ -56,13 +97,13 @@ const exporter = new docx.StreamPacker(doc);
const stream = exporter.pack();
// Express' response object
-res.attachment('yourfile.xlsx');
+res.attachment("yourfile.xlsx");
stream.pipe(res);
```
where `res` is the response object obtained through the Express router. It is that simple. The file will begin downloading in the browser.
-## PDF Exporting
+### PDF Exporting
You can export your word document as a PDF file like so:
@@ -74,7 +115,3 @@ exporter.packPdf("My Document");
const exporter = new docx.ExpressPacker(doc, res);
exporter.packPdf("My Document");
```
-
-## Browser based docx exporting
-
-It is on the bucket list. It has been requested by a few, and work is already on it
From 6c2f335a318d6a71b15f2bfa8dd169ead50bde4b Mon Sep 17 00:00:00 2001
From: Dolan
Date: Tue, 21 Aug 2018 22:29:40 +0100
Subject: [PATCH 56/63] Add new slogan
---
README.md | 2 +-
docs/README.md | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 84b4a75e0b..7e25666ee0 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
- Easily generate .docx files with JS/TS.
+ Easily generate .docx files with JS/TS. Works for Node and on the Browser.