Enable pdf export
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
@ -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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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<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 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<void>((resolve, reject) => {
|
||||
fs.writeFile(`${filePath}.pdf`, text, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
// tslint:disable-next-line:no-empty-interface
|
||||
export interface IPackOptions {
|
||||
pdf: boolean;
|
||||
}
|
||||
|
@ -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<void> {
|
||||
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<void>((resolve) => {
|
||||
output.on("close", () => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ export interface IConvertOutput {
|
||||
}
|
||||
|
||||
export class PdfConvertWrapper {
|
||||
public convert(): Promise<IConvertOutput> {
|
||||
const buffer = fs.readFileSync("test.docx");
|
||||
public convert(filePath: string): Promise<IConvertOutput> {
|
||||
const buffer = fs.readFileSync(filePath);
|
||||
|
||||
return new Promise<IConvertOutput>((resolve, reject) => {
|
||||
const r = request.post({
|
||||
|
Reference in New Issue
Block a user