Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
cf1689a3c2 | |||
66d0bab0a5 | |||
5889f20f1e | |||
ebec10e312 | |||
ae8a0c7fd0 | |||
bb49200fad | |||
a6bba0bc6c | |||
fb08f79344 | |||
741c74825e | |||
28539cd47b | |||
8ca7c5a343 | |||
32b56e7071 | |||
7dad717952 | |||
34e928755f | |||
51e0f311fe |
@ -57,6 +57,8 @@ var exporter = new docx.LocalPacker(doc);
|
|||||||
var exporter = new docx.ExpressPacker(doc, res);
|
var exporter = new docx.ExpressPacker(doc, res);
|
||||||
|
|
||||||
exporter.pack('My First Document');
|
exporter.pack('My First Document');
|
||||||
|
// If you want to export it as a .pdf file instead
|
||||||
|
exporter.packPdf('My First Document');
|
||||||
|
|
||||||
// done! A file called 'My First Document.docx'
|
// done! A file called 'My First Document.docx'
|
||||||
// will be in your file system if you used LocalPacker
|
// will be in your file system if you used LocalPacker
|
||||||
@ -88,4 +90,3 @@ Huge thanks to [@felipeochoa](https://github.com/felipeochoa) for awesome contri
|
|||||||
[gitter-url]: https://gitter.im/docx-lib/Lobby
|
[gitter-url]: https://gitter.im/docx-lib/Lobby
|
||||||
[gemnasium-image]: https://gemnasium.com/badges/github.com/dolanmiu/docx.svg
|
[gemnasium-image]: https://gemnasium.com/badges/github.com/dolanmiu/docx.svg
|
||||||
[gemnasium-url]: https://gemnasium.com/github.com/dolanmiu/docx
|
[gemnasium-url]: https://gemnasium.com/github.com/dolanmiu/docx
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "docx",
|
"name": "docx",
|
||||||
"version": "3.0.1",
|
"version": "3.1.0",
|
||||||
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -37,7 +37,11 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/archiver": "^1.3.4",
|
"@types/archiver": "^1.3.4",
|
||||||
"@types/express": "^4.0.35",
|
"@types/express": "^4.0.35",
|
||||||
|
"@types/request": "^2.0.3",
|
||||||
|
"@types/request-promise": "^4.1.39",
|
||||||
"archiver": "^1.3.0",
|
"archiver": "^1.3.0",
|
||||||
|
"request": "^2.83.0",
|
||||||
|
"request-promise": "^4.2.2",
|
||||||
"xml": "^1.0.1"
|
"xml": "^1.0.1"
|
||||||
},
|
},
|
||||||
"author": "Dolan Miu",
|
"author": "Dolan Miu",
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
export { Alignment } from "./alignment";
|
|
||||||
export { ThematicBreak } from "./border";
|
|
||||||
export { Indent } from "./indent";
|
|
||||||
export { KeepLines, KeepNext } from "./keep";
|
|
||||||
export { PageBreak } from "./page-break";
|
|
||||||
export { ParagraphProperties } from "./properties";
|
|
||||||
export { ISpacingProperties, Spacing } from "./spacing";
|
|
||||||
export { Style } from "./style";
|
|
||||||
export { LeftTabStop, MaxRightTabStop } from "./tab-stop";
|
|
||||||
export { NumberProperties } from "./unordered-list";
|
|
@ -1,4 +1,5 @@
|
|||||||
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
|
// http://officeopenxml.com/WPalignment.php
|
||||||
|
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
export type AlignmentOptions = "left" | "center" | "right" | "both";
|
export type AlignmentOptions = "left" | "center" | "right" | "both";
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
import { assert } from "chai";
|
import { assert } from "chai";
|
||||||
|
|
||||||
import { Utility } from "../../tests/utility";
|
import { Utility } from "../../../tests/utility";
|
||||||
import { ThematicBreak } from "./border";
|
import { ThematicBreak } from "./border";
|
||||||
|
|
||||||
describe("Border", () => {
|
describe("Border", () => {
|
||||||
@ -15,6 +15,18 @@ describe("ThematicBreak", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("#constructor()", () => {
|
describe("#constructor()", () => {
|
||||||
|
it("should create valid JSON", () => {
|
||||||
|
const stringifiedJson = JSON.stringify(thematicBreak);
|
||||||
|
let newJson;
|
||||||
|
|
||||||
|
try {
|
||||||
|
newJson = JSON.parse(stringifiedJson);
|
||||||
|
} catch (e) {
|
||||||
|
assert.isTrue(false);
|
||||||
|
}
|
||||||
|
assert.isTrue(true);
|
||||||
|
});
|
||||||
|
|
||||||
it("should create a Thematic Break with correct border properties", () => {
|
it("should create a Thematic Break with correct border properties", () => {
|
||||||
const newJson = Utility.jsonify(thematicBreak);
|
const newJson = Utility.jsonify(thematicBreak);
|
||||||
const attributes = {
|
const attributes = {
|
@ -1,4 +1,5 @@
|
|||||||
import { Attributes, XmlComponent } from "../xml-components";
|
// http://officeopenxml.com/WPborders.php
|
||||||
|
import { Attributes, XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
class Border extends XmlComponent {
|
class Border extends XmlComponent {
|
||||||
|
|
@ -1,4 +1,5 @@
|
|||||||
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
|
// http://officeopenxml.com/WPindentation.php
|
||||||
|
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
interface IIndentAttributesProperties {
|
interface IIndentAttributesProperties {
|
||||||
left?: number;
|
left?: number;
|
9
ts/docx/paragraph/formatting/index.ts
Normal file
9
ts/docx/paragraph/formatting/index.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
export * from "./alignment";
|
||||||
|
export * from "./border";
|
||||||
|
export * from "./indent";
|
||||||
|
export * from "./keep";
|
||||||
|
export * from "./page-break";
|
||||||
|
export * from "./spacing";
|
||||||
|
export * from "./style";
|
||||||
|
export * from "./tab-stop";
|
||||||
|
export * from "./unordered-list";
|
@ -1,4 +1,4 @@
|
|||||||
import { XmlComponent } from "../xml-components";
|
import { XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
export class KeepLines extends XmlComponent {
|
export class KeepLines extends XmlComponent {
|
||||||
constructor() {
|
constructor() {
|
@ -1,6 +1,6 @@
|
|||||||
import { assert } from "chai";
|
import { assert } from "chai";
|
||||||
|
|
||||||
import { Utility } from "../../tests/utility";
|
import { Utility } from "../../../tests/utility";
|
||||||
import { PageBreak } from "./page-break";
|
import { PageBreak } from "./page-break";
|
||||||
|
|
||||||
describe("PageBreak", () => {
|
describe("PageBreak", () => {
|
@ -1,5 +1,6 @@
|
|||||||
import { Run } from "../run";
|
// http://officeopenxml.com/WPtextSpecialContent-break.php
|
||||||
import { Attributes, XmlComponent } from "../xml-components";
|
import { Run } from "../../run";
|
||||||
|
import { Attributes, XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
class Break extends XmlComponent {
|
class Break extends XmlComponent {
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
|
|
||||||
import { Formatter } from "../../export/formatter";
|
import { Formatter } from "../../../export/formatter";
|
||||||
import { Spacing } from "./spacing";
|
import { Spacing } from "./spacing";
|
||||||
|
|
||||||
describe("Spacing", () => {
|
describe("Spacing", () => {
|
@ -1,4 +1,5 @@
|
|||||||
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
|
// http://officeopenxml.com/WPspacing.php
|
||||||
|
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
export interface ISpacingProperties {
|
export interface ISpacingProperties {
|
||||||
after?: number;
|
after?: number;
|
@ -1,6 +1,6 @@
|
|||||||
import { assert } from "chai";
|
import { assert } from "chai";
|
||||||
|
|
||||||
import { Utility } from "../../tests/utility";
|
import { Utility } from "../../../tests/utility";
|
||||||
import { Style } from "./style";
|
import { Style } from "./style";
|
||||||
|
|
||||||
describe("ParagraphStyle", () => {
|
describe("ParagraphStyle", () => {
|
@ -1,4 +1,4 @@
|
|||||||
import { Attributes, XmlComponent } from "../xml-components";
|
import { Attributes, XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
export class Style extends XmlComponent {
|
export class Style extends XmlComponent {
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
import { assert } from "chai";
|
import { assert } from "chai";
|
||||||
|
|
||||||
import { Utility } from "../../tests/utility";
|
import { Utility } from "../../../tests/utility";
|
||||||
import { LeftTabStop, MaxRightTabStop } from "./tab-stop";
|
import { LeftTabStop, MaxRightTabStop } from "./tab-stop";
|
||||||
|
|
||||||
describe("LeftTabStop", () => {
|
describe("LeftTabStop", () => {
|
@ -1,5 +1,5 @@
|
|||||||
// http://officeopenxml.com/WPtab.php
|
// http://officeopenxml.com/WPtab.php
|
||||||
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
|
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
export class TabStop extends XmlComponent {
|
export class TabStop extends XmlComponent {
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
import { assert } from "chai";
|
import { assert } from "chai";
|
||||||
|
|
||||||
import { Utility } from "../../tests/utility";
|
import { Utility } from "../../../tests/utility";
|
||||||
import { NumberProperties } from "./unordered-list";
|
import { NumberProperties } from "./unordered-list";
|
||||||
|
|
||||||
describe("NumberProperties", () => {
|
describe("NumberProperties", () => {
|
@ -1,4 +1,4 @@
|
|||||||
import { Attributes, XmlComponent } from "../xml-components";
|
import { Attributes, XmlComponent } from "../../xml-components";
|
||||||
|
|
||||||
export class NumberProperties extends XmlComponent {
|
export class NumberProperties extends XmlComponent {
|
||||||
|
|
@ -1,2 +1,3 @@
|
|||||||
export * from "./formatting";
|
export * from "./formatting";
|
||||||
export * from "./paragraph";
|
export * from "./paragraph";
|
||||||
|
export * from "./properties";
|
||||||
|
@ -6,16 +6,16 @@ import { PictureRun } from "../run/picture-run";
|
|||||||
import { TextRun } from "../run/text-run";
|
import { TextRun } from "../run/text-run";
|
||||||
import { XmlComponent } from "../xml-components";
|
import { XmlComponent } from "../xml-components";
|
||||||
|
|
||||||
import { Alignment } from "./alignment";
|
import { Alignment } from "./formatting/alignment";
|
||||||
import { ThematicBreak } from "./border";
|
import { ThematicBreak } from "./formatting/border";
|
||||||
import { Indent } from "./indent";
|
import { Indent } from "./formatting/indent";
|
||||||
import { KeepLines, KeepNext } from "./keep";
|
import { KeepLines, KeepNext } from "./formatting/keep";
|
||||||
import { PageBreak } from "./page-break";
|
import { PageBreak } from "./formatting/page-break";
|
||||||
|
import { ISpacingProperties, Spacing } from "./formatting/spacing";
|
||||||
|
import { Style } from "./formatting/style";
|
||||||
|
import { CenterTabStop, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
|
||||||
|
import { NumberProperties } from "./formatting/unordered-list";
|
||||||
import { ParagraphProperties } from "./properties";
|
import { ParagraphProperties } from "./properties";
|
||||||
import { ISpacingProperties, Spacing } from "./spacing";
|
|
||||||
import { Style } from "./style";
|
|
||||||
import { CenterTabStop, LeftTabStop, MaxRightTabStop, RightTabStop } from "./tab-stop";
|
|
||||||
import { NumberProperties } from "./unordered-list";
|
|
||||||
|
|
||||||
export class Paragraph extends XmlComponent {
|
export class Paragraph extends XmlComponent {
|
||||||
private properties: ParagraphProperties;
|
private properties: ParagraphProperties;
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// http://officeopenxml.com/WPparagraphProperties.php
|
||||||
import { XmlComponent } from "../xml-components";
|
import { XmlComponent } from "../xml-components";
|
||||||
|
|
||||||
export class ParagraphProperties extends XmlComponent {
|
export class ParagraphProperties extends XmlComponent {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// http://officeopenxml.com/WPtextSpecialContent-break.php
|
||||||
import { XmlComponent } from "../xml-components";
|
import { XmlComponent } from "../xml-components";
|
||||||
|
|
||||||
export class Break extends XmlComponent {
|
export class Break extends XmlComponent {
|
||||||
|
99
ts/export/packer/compiler.ts
Normal file
99
ts/export/packer/compiler.ts
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
import * as archiver from "archiver";
|
||||||
|
import * as express from "express";
|
||||||
|
import * as fs from "fs";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as xml from "xml";
|
||||||
|
|
||||||
|
import { Document } from "../../docx";
|
||||||
|
import { Media } from "../../media";
|
||||||
|
import { Numbering } from "../../numbering";
|
||||||
|
import { Properties } from "../../properties";
|
||||||
|
import { Styles } from "../../styles";
|
||||||
|
import { DefaultStylesFactory } from "../../styles/factory";
|
||||||
|
import { Formatter } from "../formatter";
|
||||||
|
|
||||||
|
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
||||||
|
|
||||||
|
export class Compiler {
|
||||||
|
protected archive: archiver.Archiver;
|
||||||
|
private formatter: Formatter;
|
||||||
|
private style: Styles;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
protected document: Document,
|
||||||
|
style?: Styles,
|
||||||
|
private properties: Properties = new Properties({
|
||||||
|
creator: "Un-named",
|
||||||
|
revision: "1",
|
||||||
|
lastModifiedBy: "Un-named",
|
||||||
|
}),
|
||||||
|
private numbering: Numbering = new Numbering(),
|
||||||
|
private media: Media = new Media(),
|
||||||
|
) {
|
||||||
|
this.formatter = new Formatter();
|
||||||
|
this.archive = archiver.create("zip", {});
|
||||||
|
|
||||||
|
if (style) {
|
||||||
|
this.style = style;
|
||||||
|
} else {
|
||||||
|
const stylesFactory = new DefaultStylesFactory();
|
||||||
|
this.style = stylesFactory.newInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.archive.on("error", (err) => {
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async compile(output: fs.WriteStream | express.Response): Promise<void> {
|
||||||
|
this.archive.pipe(output);
|
||||||
|
this.archive.glob("**", {
|
||||||
|
cwd: TEMPLATE_PATH,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.archive.glob("**/.rels", {
|
||||||
|
cwd: TEMPLATE_PATH,
|
||||||
|
});
|
||||||
|
|
||||||
|
const xmlDocument = xml(this.formatter.format(this.document));
|
||||||
|
const xmlStyles = xml(this.formatter.format(this.style));
|
||||||
|
const xmlProperties = xml(this.formatter.format(this.properties), {
|
||||||
|
declaration: {
|
||||||
|
standalone: "yes",
|
||||||
|
encoding: "UTF-8",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const xmlNumbering = xml(this.formatter.format(this.numbering));
|
||||||
|
|
||||||
|
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(xmlNumbering, {
|
||||||
|
name: "word/numbering.xml",
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const data of this.media.array) {
|
||||||
|
this.archive.append(data.stream, {
|
||||||
|
name: `media/${data.fileName}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.archive.finalize();
|
||||||
|
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
output.on("close", () => {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +1,20 @@
|
|||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
|
|
||||||
import { Document } from "../../docx/document";
|
import { Document } from "../../docx/document";
|
||||||
import { Media } from "../../media";
|
import { Media } from "../../media";
|
||||||
import { Numbering } from "../../numbering";
|
import { Numbering } from "../../numbering";
|
||||||
import { Properties } from "../../properties";
|
import { Properties } from "../../properties";
|
||||||
import { Styles } from "../../styles";
|
import { Styles } from "../../styles";
|
||||||
import { Packer } from "./packer";
|
import { Compiler } from "./compiler";
|
||||||
|
import { IPacker } from "./packer";
|
||||||
|
|
||||||
export class ExpressPacker extends Packer {
|
export class ExpressPacker implements IPacker {
|
||||||
private res: express.Response;
|
private res: express.Response;
|
||||||
|
private packer: Compiler;
|
||||||
|
|
||||||
constructor(document: Document, res: express.Response, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
constructor(document: Document, res: express.Response, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
||||||
super(document, styles, properties, numbering, media);
|
this.packer = new Compiler(document, styles, properties, numbering, media);
|
||||||
|
|
||||||
this.res = res;
|
this.res = res;
|
||||||
|
|
||||||
this.res.on("close", () => {
|
this.res.on("close", () => {
|
||||||
@ -18,8 +22,10 @@ export class ExpressPacker extends Packer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public pack(name: string): void {
|
public async pack(name: string): Promise<void> {
|
||||||
|
name = name.replace(/.docx$/, "");
|
||||||
|
|
||||||
this.res.attachment(`${name}.docx`);
|
this.res.attachment(`${name}.docx`);
|
||||||
super.compile(this.res);
|
await this.packer.compile(this.res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { assert } from "chai";
|
/* tslint:disable:typedef space-before-function-paren */
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
|
|
||||||
import { Document } from "../../docx/document";
|
import { Document } from "../../docx/document";
|
||||||
@ -7,7 +7,7 @@ import { LocalPacker } from "../../export/packer/local";
|
|||||||
import { Properties } from "../../properties";
|
import { Properties } from "../../properties";
|
||||||
import { DefaultStylesFactory } from "../../styles/factory";
|
import { DefaultStylesFactory } from "../../styles/factory";
|
||||||
|
|
||||||
describe("Packer", () => {
|
describe("LocalPacker", () => {
|
||||||
let packer: LocalPacker;
|
let packer: LocalPacker;
|
||||||
let stylesFactory: DefaultStylesFactory;
|
let stylesFactory: DefaultStylesFactory;
|
||||||
|
|
||||||
@ -29,27 +29,19 @@ describe("Packer", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("#pack()", () => {
|
describe("#pack()", () => {
|
||||||
/* tslint:disable */
|
it("should create a standard docx file", async function () {
|
||||||
it("should create a standard docx file", function (done) {
|
|
||||||
/* tslint:enable */
|
|
||||||
this.timeout(99999999);
|
this.timeout(99999999);
|
||||||
packer.pack("build-tests/tests/test.docx");
|
await packer.pack("build-tests/tests/test");
|
||||||
const int = setInterval(() => {
|
fs.statSync("build-tests/tests/test.docx");
|
||||||
const stats = fs.statSync("build-tests/tests/test.docx");
|
});
|
||||||
if (stats.size > 2000) {
|
});
|
||||||
clearInterval(int);
|
|
||||||
clearTimeout(out);
|
describe("#packPdf", () => {
|
||||||
done();
|
it("should create a standard PDF file", async function () {
|
||||||
}
|
this.timeout(99999999);
|
||||||
}, 1000);
|
|
||||||
const out = setTimeout(() => {
|
await packer.packPdf("build-tests/tests/pdf-test");
|
||||||
clearInterval(int);
|
fs.statSync("build-tests/tests/pdf-test.pdf");
|
||||||
try {
|
|
||||||
assert(false, "did not create a file within the alloted time");
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
}, 2000);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,21 +1,52 @@
|
|||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
|
import * as os from "os";
|
||||||
|
import * as path from "path";
|
||||||
|
|
||||||
import { Document } from "../../docx/document";
|
import { Document } from "../../docx/document";
|
||||||
import { Media } from "../../media";
|
import { Media } from "../../media";
|
||||||
import { Numbering } from "../../numbering";
|
import { Numbering } from "../../numbering";
|
||||||
import { Properties } from "../../properties";
|
import { Properties } from "../../properties";
|
||||||
import { Styles } from "../../styles";
|
import { Styles } from "../../styles";
|
||||||
import { Packer } from "./packer";
|
import { Compiler } from "./compiler";
|
||||||
|
import { IPacker } from "./packer";
|
||||||
|
import { PdfConvertWrapper } from "./pdf-convert-wrapper";
|
||||||
|
|
||||||
export class LocalPacker extends Packer {
|
export class LocalPacker implements IPacker {
|
||||||
private stream: fs.WriteStream;
|
private stream: fs.WriteStream;
|
||||||
|
private pdfConverter: PdfConvertWrapper;
|
||||||
|
private packer: Compiler;
|
||||||
|
|
||||||
constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
||||||
super(document, styles, properties, numbering, media);
|
this.pdfConverter = new PdfConvertWrapper();
|
||||||
|
this.packer = new Compiler(document, styles, properties, numbering, media);
|
||||||
}
|
}
|
||||||
|
|
||||||
public pack(path: string): void {
|
public async pack(filePath: string): Promise<void> {
|
||||||
path = path.replace(/.docx$/, "");
|
filePath = filePath.replace(/.docx$/, "");
|
||||||
this.stream = fs.createWriteStream(`${path}.docx`);
|
|
||||||
super.compile(this.stream);
|
this.stream = fs.createWriteStream(`${filePath}.docx`);
|
||||||
|
await this.packer.compile(this.stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async packPdf(filePath: string): Promise<void> {
|
||||||
|
filePath = filePath.replace(/.pdf$/, "");
|
||||||
|
|
||||||
|
const fileName = path.basename(filePath, path.extname(filePath));
|
||||||
|
const tempPath = path.join(os.tmpdir(), `${fileName}.docx`);
|
||||||
|
this.stream = fs.createWriteStream(tempPath);
|
||||||
|
await this.packer.compile(this.stream);
|
||||||
|
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);
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
fs.writeFile(`${filePath}.pdf`, text, (err) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,91 +1,3 @@
|
|||||||
import * as archiver from "archiver";
|
export interface IPacker {
|
||||||
import * as express from "express";
|
pack(path: string): void;
|
||||||
import * as fs from "fs";
|
|
||||||
import * as path from "path";
|
|
||||||
import * as xml from "xml";
|
|
||||||
import { Document } from "../../docx";
|
|
||||||
import { Media } from "../../media";
|
|
||||||
import { Numbering } from "../../numbering";
|
|
||||||
import { Properties } from "../../properties";
|
|
||||||
import { Styles } from "../../styles";
|
|
||||||
import { DefaultStylesFactory } from "../../styles/factory";
|
|
||||||
import { Formatter } from "../formatter";
|
|
||||||
|
|
||||||
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
|
||||||
|
|
||||||
export abstract class Packer {
|
|
||||||
protected archive: archiver.Archiver;
|
|
||||||
private formatter: Formatter;
|
|
||||||
private style: Styles;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
protected document: Document,
|
|
||||||
style?: Styles,
|
|
||||||
private properties: Properties = new Properties({
|
|
||||||
creator: "Un-named",
|
|
||||||
revision: "1",
|
|
||||||
lastModifiedBy: "Un-named",
|
|
||||||
}),
|
|
||||||
private numbering: Numbering = new Numbering(),
|
|
||||||
private media: Media = new Media(),
|
|
||||||
) {
|
|
||||||
this.formatter = new Formatter();
|
|
||||||
this.archive = archiver.create("zip", {});
|
|
||||||
|
|
||||||
if (style) {
|
|
||||||
this.style = style;
|
|
||||||
} else {
|
|
||||||
const stylesFactory = new DefaultStylesFactory();
|
|
||||||
this.style = stylesFactory.newInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.archive.on("error", (err) => {
|
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected compile(output: fs.WriteStream | express.Response): void {
|
|
||||||
this.archive.pipe(output);
|
|
||||||
this.archive.glob("**", {
|
|
||||||
cwd: TEMPLATE_PATH,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.archive.glob("**/.rels", {
|
|
||||||
cwd: TEMPLATE_PATH,
|
|
||||||
});
|
|
||||||
|
|
||||||
const xmlDocument = xml(this.formatter.format(this.document));
|
|
||||||
const xmlStyles = xml(this.formatter.format(this.style));
|
|
||||||
const xmlProperties = xml(this.formatter.format(this.properties), {
|
|
||||||
declaration: {
|
|
||||||
standalone: "yes",
|
|
||||||
encoding: "UTF-8",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const xmlNumbering = xml(this.formatter.format(this.numbering));
|
|
||||||
|
|
||||||
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(xmlNumbering, {
|
|
||||||
name: "word/numbering.xml",
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const data of this.media.array) {
|
|
||||||
this.archive.append(data.stream, {
|
|
||||||
name: `media/${data.fileName}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.archive.finalize();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
34
ts/export/packer/pdf-convert-wrapper.ts
Normal file
34
ts/export/packer/pdf-convert-wrapper.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* tslint:disable:object-literal-key-quotes */
|
||||||
|
// This tslint disable is needed, or it simply won't work
|
||||||
|
import * as fs from "fs";
|
||||||
|
import * as request from "request-promise";
|
||||||
|
|
||||||
|
export interface IConvertOutput {
|
||||||
|
data: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PdfConvertWrapper {
|
||||||
|
public convert(filePath: string): request.RequestPromise {
|
||||||
|
return request.post({
|
||||||
|
url: "http://mirror1.convertonlinefree.com",
|
||||||
|
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: fs.readFileSync(filePath),
|
||||||
|
options: {
|
||||||
|
filename: "output.docx",
|
||||||
|
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"ctl00$MainContent$btnConvert": "Convert",
|
||||||
|
"ctl00$MainContent$fuZip": "",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,87 +1 @@
|
|||||||
import { DocumentAttributes } from "../docx/document/document-attributes";
|
export * from "./numbering";
|
||||||
import { Indent } from "../docx/paragraph/indent";
|
|
||||||
import { RunFonts } from "../docx/run/run-fonts";
|
|
||||||
import { XmlComponent } from "../docx/xml-components";
|
|
||||||
import { AbstractNumbering } from "./abstract-numbering";
|
|
||||||
import { Num } from "./num";
|
|
||||||
|
|
||||||
export class Numbering extends XmlComponent {
|
|
||||||
private nextId: number;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
super("w:numbering");
|
|
||||||
this.root.push(new DocumentAttributes({
|
|
||||||
wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
|
|
||||||
mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
|
||||||
o: "urn:schemas-microsoft-com:office:office",
|
|
||||||
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
|
||||||
m: "http://schemas.openxmlformats.org/officeDocument/2006/math",
|
|
||||||
v: "urn:schemas-microsoft-com:vml",
|
|
||||||
wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
|
|
||||||
wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
|
|
||||||
w10: "urn:schemas-microsoft-com:office:word",
|
|
||||||
w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
|
||||||
w14: "http://schemas.microsoft.com/office/word/2010/wordml",
|
|
||||||
w15: "http://schemas.microsoft.com/office/word/2012/wordml",
|
|
||||||
wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
|
|
||||||
wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
|
|
||||||
wne: "http://schemas.microsoft.com/office/word/2006/wordml",
|
|
||||||
wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
|
|
||||||
Ignorable: "w14 w15 wp14",
|
|
||||||
}));
|
|
||||||
|
|
||||||
this.nextId = 0;
|
|
||||||
|
|
||||||
const abstractNumbering = this.createAbstractNumbering();
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(0, "bullet", "•", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 720, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Symbol", "default"));
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(1, "bullet", "o", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 1440, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Courier New", "default"));
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(2, "bullet", "•", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 2160, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Wingdings", "default"));
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(3, "bullet", "•", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 2880, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Symbol", "default"));
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(4, "bullet", "o", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 3600, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Courier New", "default"));
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(5, "bullet", "•", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 4320, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Wingdings", "default"));
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(6, "bullet", "•", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 5040, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Symbol", "default"));
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(7, "bullet", "o", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 5760, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Courier New", "default"));
|
|
||||||
|
|
||||||
abstractNumbering.createLevel(8, "bullet", "•", "left")
|
|
||||||
.addParagraphProperty(new Indent({ left: 6480, hanging: 360 }))
|
|
||||||
.addRunProperty(new RunFonts("Wingdings", "default"));
|
|
||||||
|
|
||||||
this.createConcreteNumbering(abstractNumbering);
|
|
||||||
}
|
|
||||||
|
|
||||||
public createAbstractNumbering(): AbstractNumbering {
|
|
||||||
const num = new AbstractNumbering(this.nextId++);
|
|
||||||
this.root.push(num);
|
|
||||||
return num;
|
|
||||||
}
|
|
||||||
|
|
||||||
public createConcreteNumbering(abstractNumbering: AbstractNumbering): Num {
|
|
||||||
const num = new Num(this.nextId++, abstractNumbering.id);
|
|
||||||
this.root.push(num);
|
|
||||||
return num;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
87
ts/numbering/numbering.ts
Normal file
87
ts/numbering/numbering.ts
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
import { DocumentAttributes } from "../docx/document/document-attributes";
|
||||||
|
import { Indent } from "../docx/paragraph/formatting";
|
||||||
|
import { RunFonts } from "../docx/run/run-fonts";
|
||||||
|
import { XmlComponent } from "../docx/xml-components";
|
||||||
|
import { AbstractNumbering } from "./abstract-numbering";
|
||||||
|
import { Num } from "./num";
|
||||||
|
|
||||||
|
export class Numbering extends XmlComponent {
|
||||||
|
private nextId: number;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super("w:numbering");
|
||||||
|
this.root.push(new DocumentAttributes({
|
||||||
|
wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
|
||||||
|
mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
||||||
|
o: "urn:schemas-microsoft-com:office:office",
|
||||||
|
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
||||||
|
m: "http://schemas.openxmlformats.org/officeDocument/2006/math",
|
||||||
|
v: "urn:schemas-microsoft-com:vml",
|
||||||
|
wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
|
||||||
|
wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
|
||||||
|
w10: "urn:schemas-microsoft-com:office:word",
|
||||||
|
w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
||||||
|
w14: "http://schemas.microsoft.com/office/word/2010/wordml",
|
||||||
|
w15: "http://schemas.microsoft.com/office/word/2012/wordml",
|
||||||
|
wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
|
||||||
|
wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
|
||||||
|
wne: "http://schemas.microsoft.com/office/word/2006/wordml",
|
||||||
|
wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
|
||||||
|
Ignorable: "w14 w15 wp14",
|
||||||
|
}));
|
||||||
|
|
||||||
|
this.nextId = 0;
|
||||||
|
|
||||||
|
const abstractNumbering = this.createAbstractNumbering();
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(0, "bullet", "•", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 720, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Symbol", "default"));
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(1, "bullet", "o", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 1440, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Courier New", "default"));
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(2, "bullet", "•", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 2160, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Wingdings", "default"));
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(3, "bullet", "•", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 2880, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Symbol", "default"));
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(4, "bullet", "o", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 3600, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Courier New", "default"));
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(5, "bullet", "•", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 4320, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Wingdings", "default"));
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(6, "bullet", "•", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 5040, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Symbol", "default"));
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(7, "bullet", "o", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 5760, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Courier New", "default"));
|
||||||
|
|
||||||
|
abstractNumbering.createLevel(8, "bullet", "•", "left")
|
||||||
|
.addParagraphProperty(new Indent({ left: 6480, hanging: 360 }))
|
||||||
|
.addRunProperty(new RunFonts("Wingdings", "default"));
|
||||||
|
|
||||||
|
this.createConcreteNumbering(abstractNumbering);
|
||||||
|
}
|
||||||
|
|
||||||
|
public createAbstractNumbering(): AbstractNumbering {
|
||||||
|
const num = new AbstractNumbering(this.nextId++);
|
||||||
|
this.root.push(num);
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
public createConcreteNumbering(abstractNumbering: AbstractNumbering): Num {
|
||||||
|
const num = new Num(this.nextId++, abstractNumbering.id);
|
||||||
|
this.root.push(num);
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import * as paragraph from "../../docx/paragraph/formatting";
|
import * as paragraph from "../../docx/paragraph";
|
||||||
import * as formatting from "../../docx/run/formatting";
|
import * as formatting from "../../docx/run/formatting";
|
||||||
import { RunProperties } from "../../docx/run/properties";
|
import { RunProperties } from "../../docx/run/properties";
|
||||||
import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
|
import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
|
||||||
|
@ -8,7 +8,8 @@
|
|||||||
"outDir": "../build-tests",
|
"outDir": "../build-tests",
|
||||||
"sourceRoot": "./",
|
"sourceRoot": "./",
|
||||||
"rootDir": "./",
|
"rootDir": "./",
|
||||||
"module": "commonjs"
|
"module": "commonjs",
|
||||||
|
"noUnusedLocals": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"**/*.spec.ts",
|
"**/*.spec.ts",
|
||||||
|
@ -9,7 +9,8 @@
|
|||||||
"sourceRoot": "./",
|
"sourceRoot": "./",
|
||||||
"rootDir": "./",
|
"rootDir": "./",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"declaration": true
|
"declaration": true,
|
||||||
|
"noUnusedLocals": true
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"tests",
|
"tests",
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
],
|
],
|
||||||
"no-unused-variable": [
|
"no-unused-variable": [
|
||||||
true
|
true
|
||||||
]
|
],
|
||||||
|
"no-implicit-dependencies": false
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user