Merge pull request #373 from dolanmiu/feat/declaritive

Static Packer
This commit is contained in:
Dolan
2019-08-08 01:06:00 +01:00
committed by GitHub
51 changed files with 128 additions and 183 deletions

View File

@ -24,8 +24,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -336,8 +336,6 @@ const documentCreator = new DocumentCreator();
const doc = documentCreator.create([experiences, education, skills, achievements]); const doc = documentCreator.create([experiences, education, skills, achievements]);
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -163,8 +163,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -14,8 +14,6 @@ doc.addSection({
children: [new Paragraph("Hello World"), new Paragraph(image), new Paragraph(image2), new Paragraph(image3), new Paragraph(image4)], children: [new Paragraph("Hello World"), new Paragraph(image), new Paragraph(image2), new Paragraph(image3), new Paragraph(image4)],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -27,8 +27,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -32,8 +32,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -15,8 +15,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -107,8 +107,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -12,8 +12,6 @@ doc.addSection({
doc.createFootnote(new Paragraph("Test")); doc.createFootnote(new Paragraph("Test"));
doc.createFootnote(new Paragraph("My amazing reference")); doc.createFootnote(new Paragraph("My amazing reference"));
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -17,8 +17,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -23,8 +23,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBase64String(doc).then((str) => {
packer.toBase64String(doc).then((str) => {
fs.writeFileSync("My Document.docx", str); fs.writeFileSync("My Document.docx", str);
}); });

View File

@ -117,8 +117,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -19,8 +19,6 @@ table
.addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green") .addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green")
.addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000"); .addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000");
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -30,8 +30,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -39,8 +39,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -27,8 +27,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -19,8 +19,6 @@ table.getCell(2, 2).add(new Paragraph("Hello"));
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
table.getCell(1, 1).add(new Paragraph(image)); table.getCell(1, 1).add(new Paragraph(image));
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -28,8 +28,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -37,8 +37,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -48,12 +48,9 @@ doc.addSection({
style: "MySpectacularStyle", style: "MySpectacularStyle",
pageBreakBefore: true, pageBreakBefore: true,
}), }),
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -61,8 +61,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -71,8 +71,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -23,8 +23,7 @@ fs.readFile(filePath, (err, data) => {
children: [new Paragraph("Hello World")], children: [new Paragraph("Hello World")],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });
}); });

View File

@ -27,8 +27,6 @@ doc.addSection({
children: [table], children: [table],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -108,8 +108,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -26,8 +26,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -36,8 +36,6 @@ doc.addSection({
children: [table], children: [table],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -12,8 +12,6 @@ doc.addSection({
children: [paragraph], children: [paragraph],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -22,8 +22,6 @@ doc.addSection({
children: [table], children: [table],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -17,8 +17,6 @@ doc.addSection({
children: [new Paragraph("Hello World")], children: [new Paragraph("Hello World")],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -40,8 +40,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -40,8 +40,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -16,8 +16,6 @@ doc.addSection({
children: [table], children: [table],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -24,8 +24,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -49,8 +49,6 @@ doc.addSection({
children: [table], children: [table],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -17,8 +17,6 @@ doc.addSection({
children: [table], children: [table],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -35,8 +35,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -30,8 +30,6 @@ doc.addSection({
children: [], children: [],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -34,8 +34,6 @@ doc.addSection({
children: [], children: [],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -57,8 +57,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -35,8 +35,6 @@ doc.addSection({
], ],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -12,8 +12,6 @@ doc.addSection({
children: [new Paragraph("Hello World")], children: [new Paragraph("Hello World")],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -19,8 +19,6 @@ doc.addSection({
children: [new Paragraph("Hello World")], children: [new Paragraph("Hello World")],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -30,8 +30,6 @@ doc.addSection({
children: [new Paragraph("Hello World")], children: [new Paragraph("Hello World")],
}); });
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -32,9 +32,9 @@
], ],
}); });
const packer = new Packer();
packer.toBlob(doc).then((blob) => {
Packer.toBlob(doc).then((blob) => {
console.log(blob); console.log(blob);
saveAs(blob, "example.docx"); saveAs(blob, "example.docx");
console.log("Document created successfully"); console.log("Document created successfully");

View File

@ -59,9 +59,7 @@ doc.addSection({
}); });
// Used to export the file into a .docx file // Used to export the file into a .docx file
const packer = new Packer(); Packer.toBuffer(doc).then((buffer) => {
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });

View File

@ -2,9 +2,44 @@
> Packers are the way in which `docx` turns your code into `.docx` format. It is completely decoupled from the `docx.Document`. > Packers are the way in which `docx` turns your code into `.docx` format. It is completely decoupled from the `docx.Document`.
Packers in `version 4` and above are now one single `Packer`. It works in both a node and browser environment (Angular etc). Now, the packer returns a `Buffer`, `Blob` or `base64 string`. It is up to you to take that and persist it with node's `fs`, send it down as a downloadable file, or anything else you wish. As of version 4, this library will not have options to export to PDF.
## Version 5
Packers in `version 5` and above are now static methods on `Packer`.
### Export as Buffer
This will return a NodeJS `Buffer`. If this is used in the browser, it will return a `UInt8Array` instead.
```ts
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
```
### Export as a `base64` string
```ts
Packer.toBase64String(doc).then((string) => {
console.log(string);
});
```
### Export as Blob
This is useful if you want to send it as an downloadable in a browser environment.
```ts
Packer.toBlob(doc).then((blob) => {
// saveAs from FileSaver will download the file
saveAs(blob, "example.docx");
});
```
## Version 4 ## Version 4
Packers in `version 4` and above are now one single `Packer`. It works in both a node and browser environment (Angular etc). Now, the packer returns a `Buffer`, `Blob` or `base64 string`. It is up to you to take that and persist it with node's `fs`, send it down as a downloadable file, or anything else you wish. As of version 4, this library will not have options to export to PDF. The `Packer` in `version 4` requires an instance of `Packer`, so be sure to `new` it.
### Export as Buffer ### Export as Buffer

View File

@ -107,8 +107,7 @@ There are three parts to using custom styles with `docx`:
3. When you generate your document, make sure to pass the `styles` container to the `Packer`: 3. When you generate your document, make sure to pass the `styles` container to the `Packer`:
```ts ```ts
const packer = new Packer(doc, myStyles); Packer.pack(myOutStream);
packer.pack(myOutStream);
``` ```
**Note**: If you are using the `.headingX` or `.title` methods of paragraphs, you must make sure to define `HeadingX` or `Title` styles for these. Otherwise they'll show up unstyled :(. If you are using the `.bullet` or `.setNumbering` methods, you need to define a `ListParagraph` style or the numbers may not show up. **Note**: If you are using the `.headingX` or `.title` methods of paragraphs, you must make sure to define `HeadingX` or `Title` styles for these. Otherwise they'll show up unstyled :(. If you are using the `.bullet` or `.setNumbering` methods, you need to define a `ListParagraph` style or the numbers may not show up.

View File

@ -30,17 +30,15 @@ interface IXmlifyedFileMapping {
export class Compiler { export class Compiler {
private readonly formatter: Formatter; private readonly formatter: Formatter;
private readonly imageReplacer: ImageReplacer; private readonly imageReplacer: ImageReplacer;
private readonly prettifyXml?: boolean;
constructor(prettifyXml?: boolean) { constructor() {
this.formatter = new Formatter(); this.formatter = new Formatter();
this.imageReplacer = new ImageReplacer(); this.imageReplacer = new ImageReplacer();
this.prettifyXml = prettifyXml;
} }
public compile(file: File): JSZip { public compile(file: File, prettifyXml?: boolean): JSZip {
const zip = new JSZip(); const zip = new JSZip();
const xmlifiedFileMapping = this.xmlifyFile(file); const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml);
for (const key in xmlifiedFileMapping) { for (const key in xmlifiedFileMapping) {
if (!xmlifiedFileMapping[key]) { if (!xmlifiedFileMapping[key]) {
@ -66,14 +64,14 @@ export class Compiler {
return zip; return zip;
} }
private xmlifyFile(file: File): IXmlifyedFileMapping { private xmlifyFile(file: File, prettify?: boolean): IXmlifyedFileMapping {
file.verifyUpdateFields(); file.verifyUpdateFields();
const documentRelationshipCount = file.DocumentRelationships.RelationshipCount + 1; const documentRelationshipCount = file.DocumentRelationships.RelationshipCount + 1;
return { return {
Relationships: { Relationships: {
data: (() => { data: (() => {
const xmlData = xml(this.formatter.format(file.Document), this.prettifyXml); const xmlData = xml(this.formatter.format(file.Document), prettify);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => { mediaDatas.forEach((mediaData, i) => {
@ -84,13 +82,13 @@ export class Compiler {
); );
}); });
return xml(this.formatter.format(file.DocumentRelationships), this.prettifyXml); return xml(this.formatter.format(file.DocumentRelationships), prettify);
})(), })(),
path: "word/_rels/document.xml.rels", path: "word/_rels/document.xml.rels",
}, },
Document: { Document: {
data: (() => { data: (() => {
const tempXmlData = xml(this.formatter.format(file.Document), this.prettifyXml); const tempXmlData = xml(this.formatter.format(file.Document), prettify);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, documentRelationshipCount); const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, documentRelationshipCount);
@ -99,7 +97,7 @@ export class Compiler {
path: "word/document.xml", path: "word/document.xml",
}, },
Styles: { Styles: {
data: xml(this.formatter.format(file.Styles), this.prettifyXml), data: xml(this.formatter.format(file.Styles), prettify),
path: "word/styles.xml", path: "word/styles.xml",
}, },
Properties: { Properties: {
@ -112,15 +110,15 @@ export class Compiler {
path: "docProps/core.xml", path: "docProps/core.xml",
}, },
Numbering: { Numbering: {
data: xml(this.formatter.format(file.Numbering), this.prettifyXml), data: xml(this.formatter.format(file.Numbering), prettify),
path: "word/numbering.xml", path: "word/numbering.xml",
}, },
FileRelationships: { FileRelationships: {
data: xml(this.formatter.format(file.FileRelationships), this.prettifyXml), data: xml(this.formatter.format(file.FileRelationships), prettify),
path: "_rels/.rels", path: "_rels/.rels",
}, },
HeaderRelationships: file.Headers.map((headerWrapper, index) => { HeaderRelationships: file.Headers.map((headerWrapper, index) => {
const xmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml); const xmlData = xml(this.formatter.format(headerWrapper.Header), prettify);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => { mediaDatas.forEach((mediaData, i) => {
@ -132,12 +130,12 @@ export class Compiler {
}); });
return { return {
data: xml(this.formatter.format(headerWrapper.Relationships), this.prettifyXml), data: xml(this.formatter.format(headerWrapper.Relationships), prettify),
path: `word/_rels/header${index + 1}.xml.rels`, path: `word/_rels/header${index + 1}.xml.rels`,
}; };
}), }),
FooterRelationships: file.Footers.map((footerWrapper, index) => { FooterRelationships: file.Footers.map((footerWrapper, index) => {
const xmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml); const xmlData = xml(this.formatter.format(footerWrapper.Footer), prettify);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => { mediaDatas.forEach((mediaData, i) => {
@ -149,12 +147,12 @@ export class Compiler {
}); });
return { return {
data: xml(this.formatter.format(footerWrapper.Relationships), this.prettifyXml), data: xml(this.formatter.format(footerWrapper.Relationships), prettify),
path: `word/_rels/footer${index + 1}.xml.rels`, path: `word/_rels/footer${index + 1}.xml.rels`,
}; };
}), }),
Headers: file.Headers.map((headerWrapper, index) => { Headers: file.Headers.map((headerWrapper, index) => {
const tempXmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml); const tempXmlData = xml(this.formatter.format(headerWrapper.Header), prettify);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
// TODO: 0 needs to be changed when headers get relationships of their own // TODO: 0 needs to be changed when headers get relationships of their own
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0); const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0);
@ -165,7 +163,7 @@ export class Compiler {
}; };
}), }),
Footers: file.Footers.map((footerWrapper, index) => { Footers: file.Footers.map((footerWrapper, index) => {
const tempXmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml); const tempXmlData = xml(this.formatter.format(footerWrapper.Footer), prettify);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
// TODO: 0 needs to be changed when headers get relationships of their own // TODO: 0 needs to be changed when headers get relationships of their own
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0); const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0);
@ -176,19 +174,19 @@ export class Compiler {
}; };
}), }),
ContentTypes: { ContentTypes: {
data: xml(this.formatter.format(file.ContentTypes), this.prettifyXml), data: xml(this.formatter.format(file.ContentTypes), prettify),
path: "[Content_Types].xml", path: "[Content_Types].xml",
}, },
AppProperties: { AppProperties: {
data: xml(this.formatter.format(file.AppProperties), this.prettifyXml), data: xml(this.formatter.format(file.AppProperties), prettify),
path: "docProps/app.xml", path: "docProps/app.xml",
}, },
FootNotes: { FootNotes: {
data: xml(this.formatter.format(file.FootNotes), this.prettifyXml), data: xml(this.formatter.format(file.FootNotes), prettify),
path: "word/footnotes.xml", path: "word/footnotes.xml",
}, },
Settings: { Settings: {
data: xml(this.formatter.format(file.Settings), this.prettifyXml), data: xml(this.formatter.format(file.Settings), prettify),
path: "word/settings.xml", path: "word/settings.xml",
}, },
}; };

View File

@ -7,7 +7,6 @@ import { File, HeadingLevel, Paragraph } from "file";
import { Packer } from "./packer"; import { Packer } from "./packer";
describe("Packer", () => { describe("Packer", () => {
let packer: Packer;
let file: File; let file: File;
beforeEach(() => { beforeEach(() => {
@ -34,14 +33,12 @@ describe("Packer", () => {
new Paragraph("test text"), new Paragraph("test text"),
], ],
}); });
packer = new Packer();
}); });
describe("#toBuffer()", () => { describe("#toBuffer()", () => {
it("should create a standard docx file", async function() { it("should create a standard docx file", async function() {
this.timeout(99999999); this.timeout(99999999);
const buffer = await packer.toBuffer(file); const buffer = await Packer.toBuffer(file);
assert.isDefined(buffer); assert.isDefined(buffer);
assert.isTrue(buffer.byteLength > 0); assert.isTrue(buffer.byteLength > 0);
@ -49,19 +46,24 @@ describe("Packer", () => {
it("should handle exception if it throws any", () => { it("should handle exception if it throws any", () => {
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
const compiler = stub((packer as any).compiler, "compile"); const compiler = stub((Packer as any).compiler, "compile");
compiler.throwsException(); compiler.throwsException();
return packer.toBuffer(file).catch((error) => { return Packer.toBuffer(file).catch((error) => {
assert.isDefined(error); assert.isDefined(error);
}); });
}); });
after(() => {
// tslint:disable-next-line:no-any
(Packer as any).compiler.compile.restore();
});
}); });
describe("#toBase64String()", () => { describe("#toBase64String()", () => {
it("should create a standard docx file", async function() { it("should create a standard docx file", async function() {
this.timeout(99999999); this.timeout(99999999);
const str = await packer.toBase64String(file); const str = await Packer.toBase64String(file);
assert.isDefined(str); assert.isDefined(str);
assert.isTrue(str.length > 0); assert.isTrue(str.length > 0);
@ -69,12 +71,17 @@ describe("Packer", () => {
it("should handle exception if it throws any", () => { it("should handle exception if it throws any", () => {
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
const compiler = stub((packer as any).compiler, "compile"); const compiler = stub((Packer as any).compiler, "compile");
compiler.throwsException(); compiler.throwsException();
return packer.toBase64String(file).catch((error) => { return Packer.toBase64String(file).catch((error) => {
assert.isDefined(error); assert.isDefined(error);
}); });
}); });
after(() => {
// tslint:disable-next-line:no-any
(Packer as any).compiler.compile.restore();
});
}); });
}); });

View File

@ -2,14 +2,8 @@ import { File } from "file";
import { Compiler } from "./next-compiler"; import { Compiler } from "./next-compiler";
export class Packer { export class Packer {
private readonly compiler: Compiler; public static async toBuffer(file: File, prettify?: boolean): Promise<Buffer> {
const zip = this.compiler.compile(file, prettify);
constructor(prettifyXml?: boolean) {
this.compiler = new Compiler(prettifyXml);
}
public async toBuffer(file: File): Promise<Buffer> {
const zip = this.compiler.compile(file);
const zipData = (await zip.generateAsync({ const zipData = (await zip.generateAsync({
type: "nodebuffer", type: "nodebuffer",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -18,8 +12,8 @@ export class Packer {
return zipData; return zipData;
} }
public async toBase64String(file: File): Promise<string> { public static async toBase64String(file: File, prettify?: boolean): Promise<string> {
const zip = this.compiler.compile(file); const zip = this.compiler.compile(file, prettify);
const zipData = (await zip.generateAsync({ const zipData = (await zip.generateAsync({
type: "base64", type: "base64",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -28,8 +22,8 @@ export class Packer {
return zipData; return zipData;
} }
public async toBlob(file: File): Promise<Blob> { public static async toBlob(file: File, prettify?: boolean): Promise<Blob> {
const zip = this.compiler.compile(file); const zip = this.compiler.compile(file, prettify);
const zipData = (await zip.generateAsync({ const zipData = (await zip.generateAsync({
type: "blob", type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -37,4 +31,6 @@ export class Packer {
return zipData; return zipData;
} }
private static readonly compiler = new Compiler();
} }