Enable pdf export
This commit is contained in:
@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user