diff --git a/My Document.pdf b/My Document.pdf new file mode 100644 index 0000000000..0a2b5bf7bf --- /dev/null +++ b/My Document.pdf @@ -0,0 +1,519 @@ + + + + + + Convert DOC to PDF Online | Convert DOCX to PDF Online | Convert Word to PDF Online | Free online converter + + + + + + +
+
+ + + +
+ + + + + + + + + + +
+ + +
+ +
+
+ + + + + + + +
+
+ + + + +
+
+
+ + + + + + + + + +
+ +

+ WELCOME TO 100% FREE WORD TO PDF ONLINE CONVERTER

+
+ +

+ You can convert DOC to PDF and DOCX to PDF for free

+
+
+ + + + + +
+
+
+ Advantages of our free service +
+
+
    +
  1. Absolutely 100% free service
  2. +
  3. No need to have MS Word on your PC (your file is converted on our servers)
  4. +
  5. Produced PDF 100% matches your MS Word document
  6. +
  7. High quality PDF
  8. +
  9. Pictures are not lost
  10. +
  11. MS Word documents in all languages are supported
  12. +
  13. No personal information is required (file is returned to your Internet browser)
  14. +
  15. Conversion of ZIP archives
  16. +
  17. Immediate conversion
  18. +
+ +
+
+
+
+ + + +
+
+
+
+
+
+ +
+
+
+

+ Convert one file online: + How it works

+
+ +
+ +
+
+
+
+ + + + +
+
+
+
+
+
+
+ +
+ +   + +
+ Document is not an MS Word document inside it just has .DOC/.DOCX extension or damaged +
+
+
+
+ On this page you can only convert DOC DOCX to + PDF.
+
    +
  • To convert several files, please, use tab Convert zipped files.
  • To convert RTF + ODT MHT HTM HTML TXT FB2 DOT DOTX XLS XLSX XLSB ODS XLT XLTX PPT PPTX PPS PPSX ODP + POT POTX to PDF, please, use the following link Other documents to PDF.
  • To convert JPG JPEG PNG + BMP GIF TIF TIFF to PDF, please, use the following link Image to PDF.
  • +
  • To extract text from PDF, please, use the following link PDF to TXT.
  • To convert DOC DOCX RTF ODT MHT HTM HTML + TXT FB2 DOT DOTX to DOC DOCX DOT ODT RTF TXT or XLS XLSX XLSB XLT XLTX ODS to XLS + XLSX or PPT PPTX PPS PPSX ODP POT POTX to PPT PPTX PPS PPSX JPG TIF PNG GIF BMP, + please, use the following link Other formats.
  • To convert DOC DOCX DOT DOTX RTF + ODT MHT HTM HTML TXT to FB2, please, use the following link Documents to FB2.
  • To + convert JPG JPEG JFIF PNG BMP GIF TIF ICO to other image formats, please, use the + following link + Convert Image.
  • +
  • To convert PDF to MS Word (DOC, DOCX), please, use the following link Convert PDF to Word. +
  • +
  • To convert PDF to JPG, please, use the following link Convert PDF to JPG.
  • +
  • To convert DJVU to PDF, please, use the following link Convert DJVU to PDF.
  • +
  • To recognize text in a PDF or in an image, please, use the following link Recognize + text in PDF or in image.
  • +
+
+
+ +
+
+ + + + +
+
+
+
+
+ + +
+
+ +
+
+
+ + TERMS OF SERVICE
+
+ +
+
+
+
+
+
+ + + + +
+
+ +
+
+ + + + + + + +
+ + + + + + + + + + + + + diff --git a/demo/demo1.js b/demo/demo1.js index 53e0e58834..fc8d4c31a1 100644 --- a/demo/demo1.js +++ b/demo/demo1.js @@ -1,4 +1,5 @@ const docx = require('../build'); +const fs = require('fs'); var doc = new docx.Document(); @@ -10,7 +11,10 @@ paragraph.addRun(dateText); doc.addParagraph(paragraph); -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); +var packer = new docx.Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync('My Document.docx', buffer); +}); console.log('Document created successfully at project root!'); diff --git a/demo/demo26.js b/demo/demo26.js new file mode 100644 index 0000000000..c88fdaea8e --- /dev/null +++ b/demo/demo26.js @@ -0,0 +1,21 @@ +const docx = require('../build'); +const fs = require('fs'); + +var doc = new docx.Document(); + +var paragraph = new docx.Paragraph("Hello World"); +var institutionText = new docx.TextRun("University College London").bold(); +var dateText = new docx.TextRun("5th Dec 2015").tab().bold(); +paragraph.addRun(institutionText); +paragraph.addRun(dateText); + +doc.addParagraph(paragraph); + +var packer = new docx.PdfPacker(); + +packer.toBuffer(doc).then((buffer) => { + console.log(buffer); + fs.writeFileSync('My Document.pdf', buffer); +}); + +console.log('Document created successfully at project root!'); diff --git a/src/export/index.ts b/src/export/index.ts index f6a47e4826..1a661debc6 100644 --- a/src/export/index.ts +++ b/src/export/index.ts @@ -1,5 +1,2 @@ -export * from "./packer/local"; -export * from "./packer/express"; export * from "./packer/packer"; -export * from "./packer/stream"; -export * from "./packer/buffer"; +export * from "./packer/pdf-packer"; diff --git a/src/export/packer/browser.ts b/src/export/packer/browser.ts deleted file mode 100644 index ad88a5552c..0000000000 --- a/src/export/packer/browser.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Compiler } from "./next-compiler"; -import { IPacker } from "./packer"; - -declare var saveAs; - -export class BrowserPacker implements IPacker { - private readonly packer: Compiler; - - public async pack(filePath: string): Promise { - filePath = filePath.replace(/.docx$/, ""); - - const zip = await this.packer.compile(); - const zipBlob = await zip.generateAsync({ type: "blob" }); - - saveAs(zipBlob, `${filePath}.docx`); - } -} diff --git a/src/export/packer/buffer-stream.ts b/src/export/packer/buffer-stream.ts deleted file mode 100644 index e0d2f8ed73..0000000000 --- a/src/export/packer/buffer-stream.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { Writable } from "stream"; - -export class BufferStream extends Writable { - private readonly data: Buffer[]; - - constructor() { - super(); - - this.data = []; - } - - // tslint:disable-next-line:no-any - public _write(chunk: any, _: string, next: (err?: Error) => void): void { - this.data.push(Buffer.from(chunk)); - next(); - } - - // tslint:disable-next-line:ban-types - public end(cb?: Function): void { - super.end(cb); - - this.emit("close"); - } - - public get Buffer(): Buffer { - return Buffer.concat(this.data); - } -} diff --git a/src/export/packer/buffer.ts b/src/export/packer/buffer.ts deleted file mode 100644 index 9c46c4e166..0000000000 --- a/src/export/packer/buffer.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { File } from "../../file"; -import { BufferStream } from "./buffer-stream"; -import { Compiler } from "./compiler"; -import { IPacker } from "./packer"; - -export class BufferPacker implements IPacker { - private readonly packer: Compiler; - - constructor(file: File) { - this.packer = new Compiler(file); - } - - public async pack(): Promise { - const stream = new BufferStream(); - - await this.packer.compile(stream); - - return stream.Buffer; - } -} diff --git a/src/export/packer/express.spec.ts b/src/export/packer/express.spec.ts deleted file mode 100644 index 416d206815..0000000000 --- a/src/export/packer/express.spec.ts +++ /dev/null @@ -1,43 +0,0 @@ -// tslint:disable:typedef space-before-function-paren -// tslint:disable:no-empty -// tslint:disable:no-any -import { assert } from "chai"; -import { stub } from "sinon"; - -import { ExpressPacker } from "../../export/packer/express"; -import { File, Paragraph } from "../../file"; - -describe("LocalPacker", () => { - let packer: ExpressPacker; - - beforeEach(() => { - const file = new File({ - creator: "Dolan Miu", - revision: "1", - lastModifiedBy: "Dolan Miu", - }); - const paragraph = new Paragraph("test text"); - const heading = new Paragraph("Hello world").heading1(); - file.addParagraph(new Paragraph("title").title()); - file.addParagraph(heading); - file.addParagraph(new Paragraph("heading 2").heading2()); - file.addParagraph(paragraph); - - const expressResMock = { - on: () => {}, - attachment: () => {}, - }; - - packer = new ExpressPacker(file, expressResMock as any); - }); - - describe("#pack()", () => { - it("should handle exception if it throws any", () => { - const compiler = stub((packer as any).packer, "compile"); - compiler.throwsException(); - return packer.pack("build/tests/test").catch((error) => { - assert.isDefined(error); - }); - }); - }); -}); diff --git a/src/export/packer/express.ts b/src/export/packer/express.ts deleted file mode 100644 index 3a4fa4b3ef..0000000000 --- a/src/export/packer/express.ts +++ /dev/null @@ -1,32 +0,0 @@ -import * as express from "express"; - -import { File } from "file"; -import { Compiler } from "./compiler"; -import { IPacker } from "./packer"; - -/** - * @deprecated ExpressPacker is now deprecated. Please use the StreamPacker instead and pipe that to `express`' `res` object - */ -export class ExpressPacker implements IPacker { - private readonly packer: Compiler; - - constructor(file: File, private readonly res: express.Response) { - this.packer = new Compiler(file); - - this.res = res; - - this.res.on("close", () => { - return res - .status(200) - .send("OK") - .end(); - }); - } - - public async pack(name: string): Promise { - name = name.replace(/.docx$/, ""); - - this.res.attachment(`${name}.docx`); - await this.packer.compile(this.res); - } -} diff --git a/src/export/packer/local.spec.ts b/src/export/packer/local.spec.ts deleted file mode 100644 index e942fb70a9..0000000000 --- a/src/export/packer/local.spec.ts +++ /dev/null @@ -1,66 +0,0 @@ -/* tslint:disable:typedef space-before-function-paren */ -import { assert } from "chai"; -import * as fs from "fs"; -import { stub } from "sinon"; - -import { LocalPacker } from "../../export/packer/local"; -import { File, Paragraph } from "../../file"; - -describe("LocalPacker", () => { - let packer: LocalPacker; - - beforeEach(() => { - const file = new File({ - creator: "Dolan Miu", - revision: "1", - lastModifiedBy: "Dolan Miu", - }); - const paragraph = new Paragraph("test text"); - const heading = new Paragraph("Hello world").heading1(); - file.addParagraph(new Paragraph("title").title()); - file.addParagraph(heading); - file.addParagraph(new Paragraph("heading 2").heading2()); - file.addParagraph(paragraph); - - packer = new LocalPacker(file); - }); - - describe("#pack()", () => { - it("should create a standard docx file", async function() { - this.timeout(99999999); - await packer.pack("build/tests/test"); - fs.statSync("build/tests/test.docx"); - }); - - it("should handle exception if it throws any", () => { - // tslint:disable-next-line:no-any - const compiler = stub((packer as any).packer, "compile"); - compiler.throwsException(); - return packer.pack("build/tests/test").catch((error) => { - assert.isDefined(error); - }); - }); - }); - - describe("#packPdf", () => { - it("should create a standard PDF file", async function() { - this.timeout(99999999); - - // tslint:disable-next-line:no-any - const pdfConverterConvert = stub((packer as any).pdfConverter, "convert"); - pdfConverterConvert.returns("Test PDF Contents"); - - await packer.packPdf("build/tests/pdf-test"); - fs.statSync("build/tests/pdf-test.pdf"); - }); - - it("should handle exception if it throws any", () => { - // tslint:disable-next-line:no-any - const compiler = stub((packer as any).packer, "compile"); - compiler.throwsException(); - return packer.packPdf("build/tests/pdf-test").catch((error) => { - assert.isDefined(error); - }); - }); - }); -}); diff --git a/src/export/packer/local.ts b/src/export/packer/local.ts deleted file mode 100644 index 892a3422f1..0000000000 --- a/src/export/packer/local.ts +++ /dev/null @@ -1,57 +0,0 @@ -import * as fs from "fs"; -import * as os from "os"; -import * as path from "path"; - -import { File } from "../../file"; -import { Compiler } from "./next-compiler"; -import { IPacker } from "./packer"; -import { PdfConvertWrapper } from "./pdf-convert-wrapper"; - -export class LocalPacker implements IPacker { - private readonly pdfConverter: PdfConvertWrapper; - private readonly packer: Compiler; - - constructor(file: File) { - this.pdfConverter = new PdfConvertWrapper(); - this.packer = new Compiler(file); - } - - public async pack(filePath: string): Promise { - filePath = filePath.replace(/.docx$/, ""); - - const zip = await this.packer.compile(); - const zipData = (await zip.generateAsync({ type: "base64" })) as string; - - await this.writeToFile(`${filePath}.docx`, zipData); - } - - 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`); - - const zip = await this.packer.compile(); - const zipData = (await zip.generateAsync({ type: "base64" })) as string; - await this.writeToFile(tempPath, zipData); - - 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); - - await this.writeToFile(`${filePath}.pdf`, text); - } - - private writeToFile(filePath: string, data: string): Promise { - const file = fs.createWriteStream(filePath); - - return new Promise((resolve, reject) => { - file.write(data, "base64"); - file.end(); - file.on("finish", () => { - resolve(); - }); - file.on("error", reject); - }); - } -} diff --git a/src/export/packer/next-compiler.spec.ts b/src/export/packer/next-compiler.spec.ts new file mode 100644 index 0000000000..fc02d4a814 --- /dev/null +++ b/src/export/packer/next-compiler.spec.ts @@ -0,0 +1,66 @@ +/* tslint:disable:typedef space-before-function-paren */ +import { expect } from "chai"; +import { File } from "../../file"; +import { Compiler } from "./next-compiler"; + +describe("Compiler", () => { + let compiler: Compiler; + let file: File; + + beforeEach(() => { + file = new File(); + compiler = new Compiler(); + }); + + describe("#compile()", () => { + it("should pack all the content", async function() { + this.timeout(99999999); + const zipFile = await compiler.compile(file); + const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); + + expect(fileNames).is.an.instanceof(Array); + expect(fileNames).has.length(17); + expect(fileNames).to.include("word/document.xml"); + expect(fileNames).to.include("word/styles.xml"); + expect(fileNames).to.include("docProps/core.xml"); + expect(fileNames).to.include("docProps/app.xml"); + expect(fileNames).to.include("word/numbering.xml"); + expect(fileNames).to.include("word/header1.xml"); + expect(fileNames).to.include("word/_rels/header1.xml.rels"); + expect(fileNames).to.include("word/footer1.xml"); + expect(fileNames).to.include("word/footnotes.xml"); + expect(fileNames).to.include("word/_rels/footer1.xml.rels"); + expect(fileNames).to.include("word/_rels/document.xml.rels"); + expect(fileNames).to.include("[Content_Types].xml"); + expect(fileNames).to.include("_rels/.rels"); + }); + + it("should pack all additional headers and footers", async function() { + file.createFooter(); + file.createFooter(); + file.createHeader(); + file.createHeader(); + + this.timeout(99999999); + + const zipFile = await compiler.compile(file); + const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); + + expect(fileNames).is.an.instanceof(Array); + expect(fileNames).has.length(25); + + expect(fileNames).to.include("word/header1.xml"); + expect(fileNames).to.include("word/_rels/header1.xml.rels"); + expect(fileNames).to.include("word/header2.xml"); + expect(fileNames).to.include("word/_rels/header2.xml.rels"); + expect(fileNames).to.include("word/header3.xml"); + expect(fileNames).to.include("word/_rels/header3.xml.rels"); + expect(fileNames).to.include("word/footer1.xml"); + expect(fileNames).to.include("word/_rels/footer1.xml.rels"); + expect(fileNames).to.include("word/footer2.xml"); + expect(fileNames).to.include("word/_rels/footer2.xml.rels"); + expect(fileNames).to.include("word/footer3.xml"); + expect(fileNames).to.include("word/_rels/footer3.xml.rels"); + }); + }); +}); diff --git a/src/export/packer/next-compiler.ts b/src/export/packer/next-compiler.ts index 4d26fbd5ea..f06c09e196 100644 --- a/src/export/packer/next-compiler.ts +++ b/src/export/packer/next-compiler.ts @@ -16,37 +16,44 @@ interface IXmlifyedFileMapping { Numbering: IXmlifyedFile; Relationships: IXmlifyedFile; FileRelationships: IXmlifyedFile; - Header: IXmlifyedFile; - Footer: IXmlifyedFile; - HeaderRelationships: IXmlifyedFile; - FooterRelationships: IXmlifyedFile; + Headers: IXmlifyedFile[]; + Footers: IXmlifyedFile[]; + HeaderRelationships: IXmlifyedFile[]; + FooterRelationships: IXmlifyedFile[]; ContentTypes: IXmlifyedFile; AppProperties: IXmlifyedFile; + FootNotes: IXmlifyedFile; } export class Compiler { private readonly formatter: Formatter; - constructor(private readonly file: File) { + constructor() { this.formatter = new Formatter(); } - public async compile(): Promise { + public async compile(file: File): Promise { const zip = new JSZip(); - const xmlifiedFileMapping = this.xmlifyFile(this.file); + const xmlifiedFileMapping = this.xmlifyFile(file); for (const key in xmlifiedFileMapping) { if (!xmlifiedFileMapping[key]) { continue; } - const xmlifiedFile = xmlifiedFileMapping[key]; + const obj = xmlifiedFileMapping[key] as IXmlifyedFile | IXmlifyedFile[]; - zip.file(xmlifiedFile.path, xmlifiedFile.data); + if (Array.isArray(obj)) { + for (const subFile of obj) { + zip.file(subFile.path, subFile.data); + } + } else { + zip.file(obj.path, obj.data); + } } - for (const data of this.file.Media.Array) { + for (const data of file.Media.Array) { const mediaData = data.stream; zip.file(`word/media/${data.fileName}`, mediaData); } @@ -85,22 +92,22 @@ export class Compiler { data: xml(this.formatter.format(file.FileRelationships)), path: "_rels/.rels", }, - Header: { - data: xml(this.formatter.format(file.Header.Header)), - path: "word/header1.xml", - }, - Footer: { - data: xml(this.formatter.format(file.Footer.Footer)), - path: "word/footer1.xml", - }, - HeaderRelationships: { - data: xml(this.formatter.format(file.Header.Relationships)), - path: "word/_rels/header1.xml.rels", - }, - FooterRelationships: { - data: xml(this.formatter.format(file.Footer.Relationships)), - path: "word/_rels/footer1.xml.rels", - }, + Headers: file.Headers.map((headerWrapper, index) => ({ + data: xml(this.formatter.format(headerWrapper.Header)), + path: `word/header${index + 1}.xml`, + })), + Footers: file.Footers.map((footerWrapper, index) => ({ + data: xml(this.formatter.format(footerWrapper.Footer)), + path: `word/footer${index + 1}.xml`, + })), + HeaderRelationships: file.Headers.map((headerWrapper, index) => ({ + data: xml(this.formatter.format(headerWrapper.Relationships)), + path: `word/_rels/header${index + 1}.xml.rels`, + })), + FooterRelationships: file.Footers.map((footerWrapper, index) => ({ + data: xml(this.formatter.format(footerWrapper.Relationships)), + path: `word/_rels/footer${index + 1}.xml.rels`, + })), ContentTypes: { data: xml(this.formatter.format(file.ContentTypes)), path: "[Content_Types].xml", @@ -109,6 +116,10 @@ export class Compiler { data: xml(this.formatter.format(file.AppProperties)), path: "docProps/app.xml", }, + FootNotes: { + data: xml(this.formatter.format(file.FootNotes)), + path: "word/footnotes.xml", + }, }; } } diff --git a/src/export/packer/buffer.spec.ts b/src/export/packer/packer.spec.ts similarity index 72% rename from src/export/packer/buffer.spec.ts rename to src/export/packer/packer.spec.ts index 95eccc5ecc..497e3e46b7 100644 --- a/src/export/packer/buffer.spec.ts +++ b/src/export/packer/packer.spec.ts @@ -2,41 +2,45 @@ import { assert } from "chai"; import { stub } from "sinon"; -import { BufferPacker } from "../../export/packer/buffer"; import { File, Paragraph } from "../../file"; +import { Packer } from "./packer"; -describe("BufferPacker", () => { - let packer: BufferPacker; +describe("Packer", () => { + let packer: Packer; + let file: File; beforeEach(() => { - const file = new File({ + file = new File({ creator: "Dolan Miu", revision: "1", lastModifiedBy: "Dolan Miu", }); const paragraph = new Paragraph("test text"); const heading = new Paragraph("Hello world").heading1(); + file.addParagraph(new Paragraph("title").title()); file.addParagraph(heading); file.addParagraph(new Paragraph("heading 2").heading2()); file.addParagraph(paragraph); - packer = new BufferPacker(file); + packer = new Packer(); }); - describe("#pack()", () => { + describe("#toBuffer()", () => { it("should create a standard docx file", async function() { this.timeout(99999999); - const buffer = await packer.pack(); + const buffer = await packer.toBuffer(file); + assert.isDefined(buffer); assert.isTrue(buffer.byteLength > 0); }); it("should handle exception if it throws any", () => { // tslint:disable-next-line:no-any - const compiler = stub((packer as any).packer, "compile"); + const compiler = stub((packer as any).compiler, "compile"); + compiler.throwsException(); - return packer.pack().catch((error) => { + return packer.toBuffer(file).catch((error) => { assert.isDefined(error); }); }); diff --git a/src/export/packer/packer.ts b/src/export/packer/packer.ts index 848924f457..e85b42c8ea 100644 --- a/src/export/packer/packer.ts +++ b/src/export/packer/packer.ts @@ -1,9 +1,31 @@ -export interface IPacker { - pack(path: string): void; -} +import { File } from "file"; +import { Compiler } from "./next-compiler"; -// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432 -/** - * @ignore - */ -export const WORKAROUND = ""; +export class Packer { + private readonly compiler: Compiler; + + constructor() { + this.compiler = new Compiler(); + } + + public async toBuffer(file: File): Promise { + const zip = await this.compiler.compile(file); + const zipData = (await zip.generateAsync({ type: "nodebuffer" })) as Buffer; + + return zipData; + } + + public async toBase64String(file: File): Promise { + const zip = await this.compiler.compile(file); + const zipData = (await zip.generateAsync({ type: "base64" })) as string; + + return zipData; + } + + public async toBlob(file: File): Promise { + const zip = await this.compiler.compile(file); + const zipData = (await zip.generateAsync({ type: "blob" })) as Blob; + + return zipData; + } +} diff --git a/src/export/packer/pdf-packer.spec.ts b/src/export/packer/pdf-packer.spec.ts new file mode 100644 index 0000000000..b1062b348b --- /dev/null +++ b/src/export/packer/pdf-packer.spec.ts @@ -0,0 +1,48 @@ +/* tslint:disable:typedef space-before-function-paren */ +import { assert, expect } from "chai"; +import { stub } from "sinon"; + +import { File, Paragraph } from "../../file"; +import { PdfPacker } from "./pdf-packer"; + +describe("PdfPacker", () => { + let packer: PdfPacker; + let file: File; + + beforeEach(() => { + file = new File({ + creator: "Dolan Miu", + revision: "1", + lastModifiedBy: "Dolan Miu", + }); + const paragraph = new Paragraph("test text"); + const heading = new Paragraph("Hello world").heading1(); + + file.addParagraph(new Paragraph("title").title()); + file.addParagraph(heading); + file.addParagraph(new Paragraph("heading 2").heading2()); + file.addParagraph(paragraph); + + packer = new PdfPacker(); + }); + + describe("#packPdf", () => { + it("should create a standard PDF file", async function() { + this.timeout(99999999); + // tslint:disable-next-line:no-any + const pdfConverterConvert = stub((packer as any).pdfConverter, "convert"); + pdfConverterConvert.returns(new Buffer("")); + const buffer = await packer.toBuffer(file); + expect(buffer).is.an.instanceof(Buffer); + }); + + it("should handle exception if it throws any", () => { + // tslint:disable-next-line:no-any + const compiler = stub((packer as any).packer, "toBuffer"); + compiler.throwsException(); + return packer.toBuffer(file).catch((error) => { + assert.isDefined(error); + }); + }); + }); +}); diff --git a/src/export/packer/pdf-convert-wrapper.ts b/src/export/packer/pdf-packer.ts similarity index 66% rename from src/export/packer/pdf-convert-wrapper.ts rename to src/export/packer/pdf-packer.ts index e1229cf768..9c3629ed0e 100644 --- a/src/export/packer/pdf-convert-wrapper.ts +++ b/src/export/packer/pdf-packer.ts @@ -1,12 +1,23 @@ -import * as fs from "fs"; import * as request from "request-promise"; -export interface IConvertOutput { - data: string; -} +import { File } from "file"; +import { Packer } from "./packer"; -export class PdfConvertWrapper { - public convert(filePath: string): request.RequestPromise { +export class PdfPacker { + private readonly packer: Packer; + + constructor() { + this.packer = new Packer(); + } + + public async toBuffer(file: File): Promise { + const buffer = await this.packer.toBuffer(file); + const text = await this.convert(buffer); + + return text; + } + + private convert(buffer: Buffer): request.RequestPromise { return request.post({ url: "http://mirror1.convertonlinefree.com", // tslint:disable-next-line:no-null-keyword @@ -20,7 +31,7 @@ export class PdfConvertWrapper { __EVENTARGUMENT: "", __VIEWSTATE: "", ctl00$MainContent$fu: { - value: fs.readFileSync(filePath), + value: buffer, options: { filename: "output.docx", contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", diff --git a/src/export/packer/stream.ts b/src/export/packer/stream.ts deleted file mode 100644 index 1182cd5dcb..0000000000 --- a/src/export/packer/stream.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Readable, Transform } from "stream"; -import { File } from "../../file"; -import { Compiler } from "./compiler"; -import { IPacker } from "./packer"; - -class Pipe extends Transform { - public _transform(chunk: Buffer | string, encoding: string, callback: () => void): void { - this.push(chunk, encoding); - callback(); - } -} - -export class StreamPacker implements IPacker { - private readonly compiler: Compiler; - - constructor(file: File) { - this.compiler = new Compiler(file); - } - - public pack(): Readable { - const pipe = new Pipe(); - this.compiler.compile(pipe); - return pipe; - } -} diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts index cf3f926315..3bbb3f815e 100644 --- a/src/file/drawing/drawing.spec.ts +++ b/src/file/drawing/drawing.spec.ts @@ -10,7 +10,7 @@ function createDrawing(drawingOptions?: IDrawingOptions): Drawing { { fileName: "test.jpg", referenceId: 1, - stream: fs.createReadStream(path), + stream: fs.readFileSync(path), path: path, dimensions: { pixels: {