diff --git a/ts/export/packer/express.ts b/ts/export/packer/express.ts index b0c638f38b..8c9bac1e6d 100644 --- a/ts/export/packer/express.ts +++ b/ts/export/packer/express.ts @@ -21,6 +21,8 @@ export class ExpressPacker extends Packer { } public pack(name: string, options: IPackOptions): void { + name = name.replace(/.docx$/, ""); + this.res.attachment(`${name}.docx`); super.compile(this.res); } diff --git a/ts/export/packer/local.spec.ts b/ts/export/packer/local.spec.ts index cfdda4ce02..f16e239c6e 100644 --- a/ts/export/packer/local.spec.ts +++ b/ts/export/packer/local.spec.ts @@ -1,3 +1,5 @@ +/* tslint:disable:typedef space-before-function-paren */ + import { assert } from "chai"; import * as fs from "fs"; @@ -29,11 +31,10 @@ describe("Packer", () => { }); describe("#pack()", () => { - /* tslint:disable */ it("should create a standard docx file", function (done) { - /* tslint:enable */ this.timeout(99999999); - packer.pack("build-tests/tests/test.docx"); + packer.pack("build-tests/tests/test"); + const int = setInterval(() => { const stats = fs.statSync("build-tests/tests/test.docx"); if (stats.size > 2000) { @@ -51,5 +52,12 @@ describe("Packer", () => { } }, 2000); }); + + it("should create a standard PDF file", async function () { + this.timeout(99999999); + + await packer.packPdf("build-tests/tests/pdf-test"); + fs.statSync("build-tests/tests/pdf-test.pdf"); + }); }); }); diff --git a/ts/export/packer/local.ts b/ts/export/packer/local.ts index 67ddc336fd..94d9551a70 100644 --- a/ts/export/packer/local.ts +++ b/ts/export/packer/local.ts @@ -1,4 +1,7 @@ import * as fs from "fs"; +import * as os from "os"; +import * as path from "path"; +import * as util from "util"; import { Document } from "../../docx/document"; import { Media } from "../../media"; @@ -7,17 +10,44 @@ import { Properties } from "../../properties"; import { Styles } from "../../styles"; import { IPackOptions } from "./pack-options"; import { Packer } from "./packer"; +import { PdfConvertWrapper } from "./pdf-convert-wrapper"; export class LocalPacker extends Packer { private stream: fs.WriteStream; + private pdfConverter: PdfConvertWrapper; constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) { super(document, styles, properties, numbering, media); + + this.pdfConverter = new PdfConvertWrapper(); } - public pack(path: string, options?: IPackOptions): void { - path = path.replace(/.docx$/, ""); - this.stream = fs.createWriteStream(`${path}.docx`); + public pack(filePath: string): void { + filePath = filePath.replace(/.docx$/, ""); + + this.stream = fs.createWriteStream(`${filePath}.docx`); super.compile(this.stream); } + + 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`); + this.stream = fs.createWriteStream(tempPath); + await super.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((resolve, reject) => { + fs.writeFile(`${filePath}.pdf`, text, (err) => { + if (err) { + reject(err); + return; + } + resolve(); + }); + }); + + } } diff --git a/ts/export/packer/pack-options.ts b/ts/export/packer/pack-options.ts index 466de05f93..5b3f43308d 100644 --- a/ts/export/packer/pack-options.ts +++ b/ts/export/packer/pack-options.ts @@ -1,3 +1,3 @@ +// tslint:disable-next-line:no-empty-interface export interface IPackOptions { - pdf: boolean; } diff --git a/ts/export/packer/packer.ts b/ts/export/packer/packer.ts index 2f35b6fa3d..e90f1cb9cf 100644 --- a/ts/export/packer/packer.ts +++ b/ts/export/packer/packer.ts @@ -48,7 +48,7 @@ export abstract class Packer { public abstract pack(path: string, options?: IPackOptions): void; - protected compile(output: fs.WriteStream | express.Response): void { + protected async compile(output: fs.WriteStream | express.Response): Promise { this.archive.pipe(output); this.archive.glob("**", { cwd: TEMPLATE_PATH, @@ -91,9 +91,12 @@ export abstract class Packer { } this.archive.finalize(); - } - protected convertToPdf(): void { - // TODO + return new Promise((resolve) => { + output.on("close", () => { + resolve(); + }); + }); + } } diff --git a/ts/export/packer/pdf-convert-wrapper.ts b/ts/export/packer/pdf-convert-wrapper.ts index 203786f6b3..ace10c03e1 100644 --- a/ts/export/packer/pdf-convert-wrapper.ts +++ b/ts/export/packer/pdf-convert-wrapper.ts @@ -6,8 +6,8 @@ export interface IConvertOutput { } export class PdfConvertWrapper { - public convert(): Promise { - const buffer = fs.readFileSync("test.docx"); + public convert(filePath: string): Promise { + const buffer = fs.readFileSync(filePath); return new Promise((resolve, reject) => { const r = request.post({