From c99b2940c612021497fbe6e77bff98f8aa83449f Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 24 Apr 2018 21:12:09 +0100 Subject: [PATCH 01/19] Add next gen compiler using JSZip --- demo/demo13.js | 16 ++++ package.json | 2 + src/export/packer/local.ts | 31 +++++--- src/export/packer/next-compiler.ts | 117 +++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 12 deletions(-) create mode 100644 demo/demo13.js create mode 100644 src/export/packer/next-compiler.ts diff --git a/demo/demo13.js b/demo/demo13.js new file mode 100644 index 0000000000..b68348b1c8 --- /dev/null +++ b/demo/demo13.js @@ -0,0 +1,16 @@ +const docx = require('../build'); + +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 exporter = new docx.LocalPacker(doc); +exporter.packPdf('My Document'); + +console.log('Document created successfully at project root!'); diff --git a/package.json b/package.json index 74e591431c..99104a157d 100644 --- a/package.json +++ b/package.json @@ -43,9 +43,11 @@ "@types/archiver": "^2.1.0", "@types/express": "^4.0.35", "@types/image-size": "0.0.29", + "@types/jszip": "^3.1.3", "@types/request-promise": "^4.1.41", "archiver": "^2.1.1", "image-size": "^0.6.2", + "jszip": "^3.1.5", "request": "^2.83.0", "request-promise": "^4.2.2", "xml": "^1.0.1" diff --git a/src/export/packer/local.ts b/src/export/packer/local.ts index 5d0c05fe81..1ebf8787ec 100644 --- a/src/export/packer/local.ts +++ b/src/export/packer/local.ts @@ -3,12 +3,11 @@ import * as os from "os"; import * as path from "path"; import { File } from "../../file"; -import { Compiler } from "./compiler"; +import { Compiler } from "./next-compiler"; import { IPacker } from "./packer"; import { PdfConvertWrapper } from "./pdf-convert-wrapper"; export class LocalPacker implements IPacker { - private stream: fs.WriteStream; private readonly pdfConverter: PdfConvertWrapper; private readonly packer: Compiler; @@ -20,8 +19,8 @@ export class LocalPacker implements IPacker { public async pack(filePath: string): Promise { filePath = filePath.replace(/.docx$/, ""); - this.stream = fs.createWriteStream(`${filePath}.docx`); - await this.packer.compile(this.stream); + const zipData = await this.packer.compile().generateAsync({ type: "base64" }) as string; + await this.writeToFile(`${filePath}.docx`, zipData); } public async packPdf(filePath: string): Promise { @@ -29,19 +28,27 @@ export class LocalPacker implements IPacker { 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 zipData = await this.packer.compile().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); - return new Promise((resolve, reject) => { - fs.writeFile(`${filePath}.pdf`, text, (err) => { - if (err) { - reject(err); - return; - } + + 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.ts b/src/export/packer/next-compiler.ts new file mode 100644 index 0000000000..baecd72ae7 --- /dev/null +++ b/src/export/packer/next-compiler.ts @@ -0,0 +1,117 @@ +import * as JSZip from "jszip"; +import * as xml from "xml"; + +import { File } from "file"; +import { Formatter } from "../formatter"; + +interface IXmlifyedFile { + data: string; + path: string; +} + +interface IXmlifyedFileMapping { + Document: IXmlifyedFile; + Styles: IXmlifyedFile; + Properties: IXmlifyedFile; + Numbering: IXmlifyedFile; + Relationships: IXmlifyedFile; + FileRelationships: IXmlifyedFile; + Header: IXmlifyedFile; + Footer: IXmlifyedFile; + HeaderRelationships: IXmlifyedFile; + FooterRelationships: IXmlifyedFile; + ContentTypes: IXmlifyedFile; + AppProperties: IXmlifyedFile; +} + +export class Compiler { + private formatter: Formatter; + + constructor(private file: File) { + this.formatter = new Formatter(); + } + + public compile(): JSZip { + const zip = new JSZip(); + + const xmlifiedFileMapping = this.xmlifyFile(this.file); + + for (const key in xmlifiedFileMapping) { + if (!xmlifiedFileMapping[key]) { + continue; + } + + const xmlifiedFile = xmlifiedFileMapping[key]; + + zip.file(xmlifiedFile.path, xmlifiedFile.data); + } + + // for (const data of file.Media.array) { + // this.archive.append(data.stream, { + // name: `word/media/${data.fileName}`, + // }); + + // zip.file(`word/media/${data.fileName}`, ) + // } + + return zip; + } + + private xmlifyFile(file: File): IXmlifyedFileMapping { + return { + Document: { + data: xml(this.formatter.format(file.Document), true), + path: "word/document.xml", + }, + Styles: { + data: xml(this.formatter.format(file.Styles)), + path: "word/styles.xml", + }, + Properties: { + data: xml(this.formatter.format(file.CoreProperties), { + declaration: { + standalone: "yes", + encoding: "UTF-8", + }, + }), + path: "docProps/core.xml", + }, + Numbering: { + data: xml(this.formatter.format(file.Numbering)), + path: "word/numbering.xml", + }, + Relationships: { + data: xml(this.formatter.format(file.DocumentRelationships)), + path: "word/_rels/document.xml.rels", + }, + FileRelationships: { + 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", + }, + ContentTypes: { + data: xml(this.formatter.format(file.ContentTypes)), + path: "[Content_Types].xml", + }, + AppProperties: { + data: xml(this.formatter.format(file.AppProperties)), + path: "docProps/app.xml", + }, + }; + } +} From 06418655c0f24b85958d9f5bf28136e9e9eebf8e Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 24 Apr 2018 22:35:31 +0100 Subject: [PATCH 02/19] Add image support to new packer --- demo/demo13.js | 5 +++-- src/export/packer/local.ts | 7 +++++-- src/export/packer/next-compiler.ts | 27 +++++++++++++++++++-------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/demo/demo13.js b/demo/demo13.js index b68348b1c8..6ce1ddd04e 100644 --- a/demo/demo13.js +++ b/demo/demo13.js @@ -11,6 +11,7 @@ paragraph.addRun(dateText); doc.addParagraph(paragraph); var exporter = new docx.LocalPacker(doc); -exporter.packPdf('My Document'); +exporter.packPdf('My Document').then(() => { + console.log('Document created successfully at project root!'); +}); -console.log('Document created successfully at project root!'); diff --git a/src/export/packer/local.ts b/src/export/packer/local.ts index 1ebf8787ec..27bba0ae69 100644 --- a/src/export/packer/local.ts +++ b/src/export/packer/local.ts @@ -19,7 +19,9 @@ export class LocalPacker implements IPacker { public async pack(filePath: string): Promise { filePath = filePath.replace(/.docx$/, ""); - const zipData = await this.packer.compile().generateAsync({ type: "base64" }) as string; + const zip = await this.packer.compile(); + const zipData = await zip.generateAsync({ type: "base64" }) as string; + await this.writeToFile(`${filePath}.docx`, zipData); } @@ -29,7 +31,8 @@ export class LocalPacker implements IPacker { const fileName = path.basename(filePath, path.extname(filePath)); const tempPath = path.join(os.tmpdir(), `${fileName}.docx`); - const zipData = await this.packer.compile().generateAsync({ type: "base64" }) as string; + 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); diff --git a/src/export/packer/next-compiler.ts b/src/export/packer/next-compiler.ts index baecd72ae7..b8ddaf2b82 100644 --- a/src/export/packer/next-compiler.ts +++ b/src/export/packer/next-compiler.ts @@ -1,3 +1,4 @@ +import * as fs from "fs"; import * as JSZip from "jszip"; import * as xml from "xml"; @@ -31,7 +32,7 @@ export class Compiler { this.formatter = new Formatter(); } - public compile(): JSZip { + public async compile(): Promise { const zip = new JSZip(); const xmlifiedFileMapping = this.xmlifyFile(this.file); @@ -46,13 +47,10 @@ export class Compiler { zip.file(xmlifiedFile.path, xmlifiedFile.data); } - // for (const data of file.Media.array) { - // this.archive.append(data.stream, { - // name: `word/media/${data.fileName}`, - // }); - - // zip.file(`word/media/${data.fileName}`, ) - // } + for (const data of this.file.Media.array) { + const mediaData = await this.readFile(data.path); + zip.file(`word/media/${data.fileName}`, mediaData); + } return zip; } @@ -114,4 +112,17 @@ export class Compiler { }, }; } + + private readFile(path: string): Promise { + return new Promise((resolve, reject) => { + fs.readFile(path, (err, data) => { + if (err) { + reject(); + return; + } + + resolve(data); + }); + }); + } } From e8bc7952db0e98e5b0b44cc01d523075dee09c8a Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 24 Apr 2018 22:56:56 +0100 Subject: [PATCH 03/19] Move fs to exporter and add browser packer --- demo/browser-demo.html | 22 ++++++++++++++++++++++ package.json | 1 + src/export/packer/browser.ts | 17 +++++++++++++++++ src/export/packer/compiler.ts | 3 ++- src/file/drawing/drawing.spec.ts | 1 - src/file/media/data.ts | 3 --- src/file/media/media.ts | 2 -- webpack.web.config.js | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 demo/browser-demo.html create mode 100644 src/export/packer/browser.ts create mode 100644 webpack.web.config.js diff --git a/demo/browser-demo.html b/demo/browser-demo.html new file mode 100644 index 0000000000..17508b74eb --- /dev/null +++ b/demo/browser-demo.html @@ -0,0 +1,22 @@ + + + + + + + + + +

DOCX browser Word document generation

+ + + + + + + + diff --git a/package.json b/package.json index 99104a157d..cc5394462b 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "build": "npm run webpack && npm run fix-types", "tsc": "rimraf ./build && tsc -p .", "webpack": "rimraf ./build && webpack", + "build.web": "webpack --config webpack.web.config.js", "demo": "npm run build && node ./demo", "typedoc": "typedoc --out docs/ src/ --module commonjs --target ES6 --disableOutputCheck --excludePrivate --externalPattern \"**/*.spec.ts\"", "style": "prettier -l \"src/**/*.ts\"", diff --git a/src/export/packer/browser.ts b/src/export/packer/browser.ts new file mode 100644 index 0000000000..ad88a5552c --- /dev/null +++ b/src/export/packer/browser.ts @@ -0,0 +1,17 @@ +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/compiler.ts b/src/export/packer/compiler.ts index 44c7817249..410ecc669b 100644 --- a/src/export/packer/compiler.ts +++ b/src/export/packer/compiler.ts @@ -1,5 +1,6 @@ import * as archiver from "archiver"; import * as express from "express"; +import * as fs from "fs"; import { Writable } from "stream"; import * as xml from "xml"; @@ -89,7 +90,7 @@ export class Compiler { }); for (const data of this.file.Media.array) { - this.archive.append(data.stream, { + this.archive.append(fs.createReadStream(data.path), { name: `word/media/${data.fileName}`, }); } diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts index 9b113da0bf..6ca09faa4d 100644 --- a/src/file/drawing/drawing.spec.ts +++ b/src/file/drawing/drawing.spec.ts @@ -12,7 +12,6 @@ describe("Drawing", () => { currentBreak = new Drawing({ fileName: "test.jpg", referenceId: 1, - stream: fs.createReadStream(path), path: path, dimensions: { pixels: { diff --git a/src/file/media/data.ts b/src/file/media/data.ts index 00836ed962..b4a349a967 100644 --- a/src/file/media/data.ts +++ b/src/file/media/data.ts @@ -1,5 +1,3 @@ -import * as fs from "fs"; - export interface IMediaDataDimensions { pixels: { x: number; @@ -13,7 +11,6 @@ export interface IMediaDataDimensions { export interface IMediaData { referenceId: number; - stream: fs.ReadStream; path: string; fileName: string; dimensions: IMediaDataDimensions; diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 5ac8905bc7..3f23e50b38 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -1,4 +1,3 @@ -import * as fs from "fs"; import * as sizeOf from "image-size"; import * as path from "path"; @@ -27,7 +26,6 @@ export class Media { const imageData = { referenceId: this.map.size + relationshipsCount + 1, - stream: fs.createReadStream(filePath), path: filePath, fileName: key, dimensions: { diff --git a/webpack.web.config.js b/webpack.web.config.js new file mode 100644 index 0000000000..9672df789c --- /dev/null +++ b/webpack.web.config.js @@ -0,0 +1,32 @@ +const path = require('path'); + +module.exports = { + entry: './src/index.ts', + + output: { + path: path.resolve('build'), + filename: 'index.web.js', + libraryTarget: 'umd' + }, + + resolve: { + extensions: ['.tsx', '.ts', '.js'], + modules: [path.resolve('./src'), "node_modules"] + }, + + module: { + rules: [ + { + test: /\.ts$/, + loaders: ["awesome-typescript-loader"], + } + ], + }, + + node: { + __dirname: true, + fs: "empty", + tls: "empty", + net: "empty" + } +}; From d850f379047222a3cb177163a4a236c1a9e3db38 Mon Sep 17 00:00:00 2001 From: Dolan Date: Fri, 10 Aug 2018 01:49:37 +0100 Subject: [PATCH 04/19] Remove unused import --- src/file/drawing/drawing.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts index 70d36f2110..208be2991a 100644 --- a/src/file/drawing/drawing.spec.ts +++ b/src/file/drawing/drawing.spec.ts @@ -1,5 +1,4 @@ import { assert } from "chai"; -import * as fs from "fs"; import { Utility } from "../../tests/utility"; import { Drawing, IDrawingOptions, PlacementPosition } from "./"; From bc1132146f6911e2d630d06623d3e923105ff6ed Mon Sep 17 00:00:00 2001 From: Dolan Date: Fri, 10 Aug 2018 02:46:15 +0100 Subject: [PATCH 05/19] Revert test changes --- src/file/drawing/drawing.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts index 208be2991a..cf3f926315 100644 --- a/src/file/drawing/drawing.spec.ts +++ b/src/file/drawing/drawing.spec.ts @@ -1,4 +1,5 @@ import { assert } from "chai"; +import * as fs from "fs"; import { Utility } from "../../tests/utility"; import { Drawing, IDrawingOptions, PlacementPosition } from "./"; @@ -9,6 +10,7 @@ function createDrawing(drawingOptions?: IDrawingOptions): Drawing { { fileName: "test.jpg", referenceId: 1, + stream: fs.createReadStream(path), path: path, dimensions: { pixels: { From 675192b86fc5cad868442a18c33f1725f597dd59 Mon Sep 17 00:00:00 2001 From: Dolan Date: Sun, 12 Aug 2018 23:07:31 +0100 Subject: [PATCH 06/19] Refactor image to accept Buffer only --- demo/demo11.js | 5 +++-- demo/demo12.js | 9 ++++---- demo/demo18.js | 4 ++-- demo/demo5.js | 11 +++++----- demo/demo9.js | 5 +++-- docs/usage/headers-and-footers.md | 4 ++-- docs/usage/images.md | 4 ++-- src/file/file.ts | 11 ++-------- src/file/footer-wrapper.ts | 4 ++-- src/file/header-wrapper.ts | 4 ++-- src/file/media/data.ts | 4 +--- src/file/media/media.ts | 36 +++++-------------------------- 12 files changed, 35 insertions(+), 66 deletions(-) diff --git a/demo/demo11.js b/demo/demo11.js index 2c6305106e..f0597e0da4 100644 --- a/demo/demo11.js +++ b/demo/demo11.js @@ -1,4 +1,5 @@ const docx = require("../build"); +const fs = require('fs'); const doc = new docx.Document(undefined, { top: 700, @@ -80,7 +81,7 @@ doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph") .quickFormat() .basedOn("Normal"); -doc.createImage("./demo/images/pizza.gif"); +doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); doc .createParagraph("HEADING") .heading1() @@ -122,7 +123,7 @@ var arrboth = [{ arrboth.forEach(function(item) { const para = doc.createParagraph(); - para.createTextRun(doc.createImage(item.image)); + para.createTextRun(doc.createImage(fs.readFileSync(item.image))); para.properties.width = 60; para.properties.height = 90; doc.createParagraph(item.comment).style("normalPara2"); diff --git a/demo/demo12.js b/demo/demo12.js index 7fd5eb5ffc..87c7a405e0 100644 --- a/demo/demo12.js +++ b/demo/demo12.js @@ -1,14 +1,15 @@ const docx = require("../build"); +const fs = require('fs'); var doc = new docx.Document(); var paragraph = new docx.Paragraph("Hello World"); doc.addParagraph(paragraph); -const image = doc.createImage("./demo/images/pizza.gif"); -const image2 = doc.createImage("./demo/images/pizza.gif"); -const image3 = doc.createImage("./demo/images/pizza.gif"); -const image4 = doc.createImage("./demo/images/pizza.gif"); +const image = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); +const image2 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); +const image3 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); +const image4 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); image.scale(0.5); image2.scale(1) diff --git a/demo/demo18.js b/demo/demo18.js index 9035d19e7a..73a778a6c3 100644 --- a/demo/demo18.js +++ b/demo/demo18.js @@ -6,8 +6,8 @@ var doc = new docx.Document(); const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHi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XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC` -// doc.createImageFromBuffer(Buffer.from(imageBase64Data, 'base64')); -doc.createImageFromBuffer(Buffer.from(imageBase64Data, 'base64'), 100, 100); +// doc.createImage(Buffer.from(imageBase64Data, 'base64')); +doc.createImage(Buffer.from(imageBase64Data, 'base64'), 100, 100); var exporter = new docx.LocalPacker(doc); exporter.pack('My Document'); diff --git a/demo/demo5.js b/demo/demo5.js index 89638ad49b..fbb2cd0eaf 100644 --- a/demo/demo5.js +++ b/demo/demo5.js @@ -1,15 +1,16 @@ const docx = require("../build"); +const fs = require('fs'); var doc = new docx.Document(); var paragraph = new docx.Paragraph("Hello World"); doc.addParagraph(paragraph); -doc.createImage("./demo/images/image1.jpeg"); -doc.createImage("./demo/images/dog.png"); -doc.createImage("./demo/images/cat.jpg"); -doc.createImage("./demo/images/parrots.bmp"); -doc.createImage("./demo/images/pizza.gif"); +doc.createImage(fs.readFileSync("./demo/images/image1.jpeg")); +doc.createImage(fs.readFileSync("./demo/images/dog.png")); +doc.createImage(fs.readFileSync("./demo/images/cat.jpg")); +doc.createImage(fs.readFileSync("./demo/images/parrots.bmp")); +doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); var exporter = new docx.LocalPacker(doc); exporter.pack("My Document"); diff --git a/demo/demo9.js b/demo/demo9.js index c93d8fa63c..c0a57cf8f6 100644 --- a/demo/demo9.js +++ b/demo/demo9.js @@ -1,11 +1,12 @@ const docx = require('../build'); +const fs = require('fs'); var doc = new docx.Document(); doc.createParagraph("Hello World"); -doc.Header.createImage("./demo/images/pizza.gif"); -doc.Footer.createImage("./demo/images/pizza.gif"); +doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif")); +doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif")); var exporter = new docx.LocalPacker(doc); exporter.pack('My Document'); diff --git a/docs/usage/headers-and-footers.md b/docs/usage/headers-and-footers.md index bfe626a04c..1489576dda 100644 --- a/docs/usage/headers-and-footers.md +++ b/docs/usage/headers-and-footers.md @@ -19,8 +19,8 @@ doc.Footer.createParagraph("Footer text"); Even add images: ```js -doc.Header.createImage([PATH_TO_YOUR_IMAGE]); -doc.Footer.createImage([PATH_TO_YOUR_IMAGE]); +doc.Header.createImage([BUFFER_OF_YOUR_IMAGE]); +doc.Footer.createImage([BUFFER_OF_YOUR_IMAGE]); ``` Refer to `demo8.js` for more information diff --git a/docs/usage/images.md b/docs/usage/images.md index 2cc0339507..a89fa893cd 100644 --- a/docs/usage/images.md +++ b/docs/usage/images.md @@ -7,7 +7,7 @@ Adding images is very simple Simply call the `createImage` method: ```js -const image = doc.createImage([PATH_TO_YOUR_IMAGE]); +const image = doc.createImage([BUFFER_OF_YOUR_IMAGE]); ``` `docx` supports `jpeg`, `jpg`, `bmp`, `gif` and `png` @@ -43,7 +43,7 @@ interface DrawingOptions { can be passed when creating `PictureRun()` for example: ```js -const imageData = document.createImageData(filename, buffer, 903, 1149); +const imageData = document.createImage(buffer, 903, 1149); new docx.PictureRun(imageData, { position: docx.PlacementPosition.FLOATING, diff --git a/src/file/file.ts b/src/file/file.ts index d7ef02b520..becc713176 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -124,20 +124,13 @@ export class File { return this.document.createTable(rows, cols); } - public createImage(filePath: string): Image { - const image = Media.addImage(this, filePath); - this.document.addParagraph(image.Paragraph); - - return image; - } - public addImage(image: Image): File { this.document.addParagraph(image.Paragraph); return this; } - public createImageFromBuffer(buffer: Buffer, width?: number, height?: number): Image { - const image = Media.addImageFromBuffer(this, buffer, width, height); + public createImage(buffer: Buffer, width?: number, height?: number): Image { + const image = Media.addImage(this, buffer, width, height); this.document.addParagraph(image.Paragraph); return image; diff --git a/src/file/footer-wrapper.ts b/src/file/footer-wrapper.ts index c1080bbd4e..afd673c2b2 100644 --- a/src/file/footer-wrapper.ts +++ b/src/file/footer-wrapper.ts @@ -36,8 +36,8 @@ export class FooterWrapper { this.footer.addChildElement(childElement); } - public createImage(image: string): void { - const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount); + public createImage(image: Buffer, width?: number, height?: number): void { + const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount, width, height); this.relationships.createRelationship( mediaData.referenceId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", diff --git a/src/file/header-wrapper.ts b/src/file/header-wrapper.ts index 1053bc2096..79a216610a 100644 --- a/src/file/header-wrapper.ts +++ b/src/file/header-wrapper.ts @@ -36,8 +36,8 @@ export class HeaderWrapper { this.header.addChildElement(childElement); } - public createImage(image: string): void { - const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount); + public createImage(image: Buffer, width?: number, height?: number): void { + const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount, width, height); this.relationships.createRelationship( mediaData.referenceId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", diff --git a/src/file/media/data.ts b/src/file/media/data.ts index dfc7a664a0..9ac7ab5eee 100644 --- a/src/file/media/data.ts +++ b/src/file/media/data.ts @@ -1,5 +1,3 @@ -import * as fs from "fs"; - export interface IMediaDataDimensions { pixels: { x: number; @@ -13,7 +11,7 @@ export interface IMediaDataDimensions { export interface IMediaData { referenceId: number; - stream: fs.ReadStream | Buffer; + stream: Buffer; path?: string; fileName: string; dimensions: IMediaDataDimensions; diff --git a/src/file/media/media.ts b/src/file/media/media.ts index b0fb4019d8..f2cb71aa2f 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -1,6 +1,4 @@ -import * as fs from "fs"; import * as sizeOf from "image-size"; -import * as path from "path"; import { File } from "../file"; import { ImageParagraph } from "../paragraph"; @@ -12,28 +10,10 @@ interface IHackedFile { } export class Media { - public static addImage(file: File, filePath: string): Image { + public static addImage(file: File, buffer: Buffer, width?: number, height?: number): Image { // Workaround to expose id without exposing to API const exposedFile = (file as {}) as IHackedFile; - const mediaData = file.Media.addMedia(filePath, exposedFile.currentRelationshipId++); - file.DocumentRelationships.createRelationship( - mediaData.referenceId, - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - `media/${mediaData.fileName}`, - ); - return new Image(new ImageParagraph(mediaData)); - } - - public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): Image { - // Workaround to expose id without exposing to API - const exposedFile = (file as {}) as IHackedFile; - const mediaData = file.Media.addMediaFromBuffer( - `${Media.generateId()}.png`, - buffer, - exposedFile.currentRelationshipId++, - width, - height, - ); + const mediaData = file.Media.addMedia(buffer, exposedFile.currentRelationshipId++, width, height); file.DocumentRelationships.createRelationship( mediaData.referenceId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", @@ -71,14 +51,8 @@ export class Media { return data; } - public addMedia(filePath: string, referenceId: number): IMediaData { - const key = path.basename(filePath); - const dimensions = sizeOf(filePath); - return this.createMedia(key, referenceId, dimensions, fs.createReadStream(filePath), filePath); - } - - public addMediaFromBuffer(fileName: string, buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData { - const key = fileName; + public addMedia(buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData { + const key = `${Media.generateId()}.png`; let dimensions; if (width && height) { dimensions = { @@ -96,7 +70,7 @@ export class Media { key: string, relationshipsCount: number, dimensions: { width: number; height: number }, - data: fs.ReadStream | Buffer, + data: Buffer, filePath?: string, ): IMediaData { const imageData = { From a38abeb4c2c018e69def58ff49d79ec30421be5e Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 14 Aug 2018 01:46:48 +0100 Subject: [PATCH 07/19] Huge refactoring to use new compiler and deprecate all other Packers Add new PdfPacker --- My Document.pdf | 519 ++++++++++++++++++ demo/demo1.js | 8 +- demo/demo26.js | 21 + src/export/index.ts | 5 +- src/export/packer/browser.ts | 17 - src/export/packer/buffer-stream.ts | 28 - src/export/packer/buffer.ts | 20 - src/export/packer/express.spec.ts | 43 -- src/export/packer/express.ts | 32 -- src/export/packer/local.spec.ts | 66 --- src/export/packer/local.ts | 57 -- src/export/packer/next-compiler.spec.ts | 66 +++ src/export/packer/next-compiler.ts | 63 ++- .../packer/{buffer.spec.ts => packer.spec.ts} | 22 +- src/export/packer/packer.ts | 38 +- src/export/packer/pdf-packer.spec.ts | 48 ++ .../{pdf-convert-wrapper.ts => pdf-packer.ts} | 25 +- src/export/packer/stream.ts | 25 - src/file/drawing/drawing.spec.ts | 2 +- 19 files changed, 760 insertions(+), 345 deletions(-) create mode 100644 My Document.pdf create mode 100644 demo/demo26.js delete mode 100644 src/export/packer/browser.ts delete mode 100644 src/export/packer/buffer-stream.ts delete mode 100644 src/export/packer/buffer.ts delete mode 100644 src/export/packer/express.spec.ts delete mode 100644 src/export/packer/express.ts delete mode 100644 src/export/packer/local.spec.ts delete mode 100644 src/export/packer/local.ts create mode 100644 src/export/packer/next-compiler.spec.ts rename src/export/packer/{buffer.spec.ts => packer.spec.ts} (72%) create mode 100644 src/export/packer/pdf-packer.spec.ts rename src/export/packer/{pdf-convert-wrapper.ts => pdf-packer.ts} (66%) delete mode 100644 src/export/packer/stream.ts 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: { From bfa0edeb09c6659fbea2f2c280e7fa4c2292f61a Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 14 Aug 2018 21:56:38 +0100 Subject: [PATCH 08/19] Remove pdf packer --- package.json | 2 -- src/export/index.ts | 1 - src/export/packer/pdf-packer.spec.ts | 48 ---------------------------- src/export/packer/pdf-packer.ts | 45 -------------------------- 4 files changed, 96 deletions(-) delete mode 100644 src/export/packer/pdf-packer.spec.ts delete mode 100644 src/export/packer/pdf-packer.ts diff --git a/package.json b/package.json index 5d4bd2e7a8..dbffae25cb 100644 --- a/package.json +++ b/package.json @@ -51,13 +51,11 @@ "@types/express": "^4.0.35", "@types/image-size": "0.0.29", "@types/jszip": "^3.1.3", - "@types/request-promise": "^4.1.42", "archiver": "^2.1.1", "fast-xml-parser": "^3.3.6", "image-size": "^0.6.2", "jszip": "^3.1.5", "request": "^2.83.0", - "request-promise": "^4.2.2", "xml": "^1.0.1" }, "author": "Dolan Miu", diff --git a/src/export/index.ts b/src/export/index.ts index 1a661debc6..042cb4366d 100644 --- a/src/export/index.ts +++ b/src/export/index.ts @@ -1,2 +1 @@ export * from "./packer/packer"; -export * from "./packer/pdf-packer"; diff --git a/src/export/packer/pdf-packer.spec.ts b/src/export/packer/pdf-packer.spec.ts deleted file mode 100644 index b1062b348b..0000000000 --- a/src/export/packer/pdf-packer.spec.ts +++ /dev/null @@ -1,48 +0,0 @@ -/* 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-packer.ts b/src/export/packer/pdf-packer.ts deleted file mode 100644 index 9c3629ed0e..0000000000 --- a/src/export/packer/pdf-packer.ts +++ /dev/null @@ -1,45 +0,0 @@ -import * as request from "request-promise"; - -import { File } from "file"; -import { Packer } from "./packer"; - -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 - 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: buffer, - options: { - filename: "output.docx", - contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - }, - }, - ctl00$MainContent$btnConvert: "Convert", - ctl00$MainContent$fuZip: "", - }, - }); - } -} From 2018f41368136709e5b2c5731211b923c5d88146 Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 14 Aug 2018 22:05:04 +0100 Subject: [PATCH 09/19] Discard pdf --- My Document.pdf | 519 ------------------------------------------------ 1 file changed, 519 deletions(-) delete mode 100644 My Document.pdf diff --git a/My Document.pdf b/My Document.pdf deleted file mode 100644 index 0a2b5bf7bf..0000000000 --- a/My Document.pdf +++ /dev/null @@ -1,519 +0,0 @@ - - - - - - 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
-
- -
-
-
-
-
-
- - - - -
-
- -
-
- - - - - - - -
- - - - - - - - - - - - - From 07261232ffd2b6f85e48d76715810d914f9f8022 Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 15 Aug 2018 01:22:22 +0100 Subject: [PATCH 10/19] Removed old compiler and unused dependencies --- package.json | 5 -- src/export/packer/compiler.spec.ts | 76 ------------------- src/export/packer/compiler.ts | 114 ----------------------------- 3 files changed, 195 deletions(-) delete mode 100644 src/export/packer/compiler.spec.ts delete mode 100644 src/export/packer/compiler.ts diff --git a/package.json b/package.json index dbffae25cb..98acfbfe79 100644 --- a/package.json +++ b/package.json @@ -46,16 +46,11 @@ ], "types": "./build/index.d.ts", "dependencies": { - "@types/archiver": "^2.1.0", - "@types/bluebird": "3.5.20", - "@types/express": "^4.0.35", "@types/image-size": "0.0.29", "@types/jszip": "^3.1.3", - "archiver": "^2.1.1", "fast-xml-parser": "^3.3.6", "image-size": "^0.6.2", "jszip": "^3.1.5", - "request": "^2.83.0", "xml": "^1.0.1" }, "author": "Dolan Miu", diff --git a/src/export/packer/compiler.spec.ts b/src/export/packer/compiler.spec.ts deleted file mode 100644 index 78d2339b11..0000000000 --- a/src/export/packer/compiler.spec.ts +++ /dev/null @@ -1,76 +0,0 @@ -/* tslint:disable:typedef space-before-function-paren */ -import * as fs from "fs"; -import * as JSZip from "jszip"; - -import { expect } from "chai"; -import { File } from "../../file"; -import { Compiler } from "./compiler"; - -describe("Compiler", () => { - let compiler: Compiler; - let file: File; - - beforeEach(() => { - file = new File(); - compiler = new Compiler(file); - }); - - describe("#compile()", () => { - it("should pack all the content", async function() { - this.timeout(99999999); - const fileName = "build/tests/test.docx"; - await compiler.compile(fs.createWriteStream(fileName)); - - const docxFile = fs.readFileSync(fileName); - const zipFile: JSZip = await JSZip.loadAsync(docxFile); - const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); - - expect(fileNames).is.an.instanceof(Array); - expect(fileNames).has.length(13); - 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 fileName = "build/tests/test2.docx"; - await compiler.compile(fs.createWriteStream(fileName)); - - const docxFile = fs.readFileSync(fileName); - const zipFile: JSZip = await JSZip.loadAsync(docxFile); - const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); - - expect(fileNames).is.an.instanceof(Array); - expect(fileNames).has.length(21); - - 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/compiler.ts b/src/export/packer/compiler.ts deleted file mode 100644 index a845833a34..0000000000 --- a/src/export/packer/compiler.ts +++ /dev/null @@ -1,114 +0,0 @@ -import * as archiver from "archiver"; -import * as express from "express"; -import { Writable } from "stream"; -import * as xml from "xml"; - -import { File } from "file"; -import { Formatter } from "../formatter"; - -export class Compiler { - protected archive: archiver.Archiver; - private readonly formatter: Formatter; - - constructor(private readonly file: File) { - this.formatter = new Formatter(); - this.archive = archiver.create("zip", {}); - - this.archive.on("error", (err) => { - throw err; - }); - } - - public async compile(output: Writable | express.Response): Promise { - this.archive.pipe(output); - - const xmlDocument = xml(this.formatter.format(this.file.Document)); - const xmlStyles = xml(this.formatter.format(this.file.Styles)); - const xmlProperties = xml(this.formatter.format(this.file.CoreProperties), { - declaration: { - standalone: "yes", - encoding: "UTF-8", - }, - }); - const xmlNumbering = xml(this.formatter.format(this.file.Numbering)); - const xmlRelationships = xml(this.formatter.format(this.file.DocumentRelationships)); - const xmlFileRelationships = xml(this.formatter.format(this.file.FileRelationships)); - const xmlContentTypes = xml(this.formatter.format(this.file.ContentTypes)); - const xmlAppProperties = xml(this.formatter.format(this.file.AppProperties)); - const xmlFootnotes = xml(this.formatter.format(this.file.FootNotes)); - - 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(xmlAppProperties, { - name: "docProps/app.xml", - }); - - this.archive.append(xmlNumbering, { - name: "word/numbering.xml", - }); - - // headers - for (let i = 0; i < this.file.Headers.length; i++) { - const element = this.file.Headers[i]; - this.archive.append(xml(this.formatter.format(element.Header)), { - name: `word/header${i + 1}.xml`, - }); - - this.archive.append(xml(this.formatter.format(element.Relationships)), { - name: `word/_rels/header${i + 1}.xml.rels`, - }); - } - - // footers - for (let i = 0; i < this.file.Footers.length; i++) { - const element = this.file.Footers[i]; - this.archive.append(xml(this.formatter.format(element.Footer)), { - name: `word/footer${i + 1}.xml`, - }); - - this.archive.append(xml(this.formatter.format(element.Relationships)), { - name: `word/_rels/footer${i + 1}.xml.rels`, - }); - } - - this.archive.append(xmlFootnotes, { - name: "word/footnotes.xml", - }); - - this.archive.append(xmlRelationships, { - name: "word/_rels/document.xml.rels", - }); - - this.archive.append(xmlContentTypes, { - name: "[Content_Types].xml", - }); - - this.archive.append(xmlFileRelationships, { - name: "_rels/.rels", - }); - - for (const data of this.file.Media.Array) { - this.archive.append(data.stream, { - name: `word/media/${data.fileName}`, - }); - } - - this.archive.finalize(); - - return new Promise((resolve) => { - output.on("close", () => { - resolve(); - }); - }); - } -} From f969c866a78ae40b04d1e97117f7a9b7046327e4 Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 15 Aug 2018 01:38:00 +0100 Subject: [PATCH 11/19] Update target for library --- webpack.config.js | 23 ++++++++++------------- webpack.web.config.js | 32 -------------------------------- 2 files changed, 10 insertions(+), 45 deletions(-) delete mode 100644 webpack.web.config.js diff --git a/webpack.config.js b/webpack.config.js index a8000404c1..01ecff1540 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,17 +1,17 @@ -const path = require('path'); +const path = require("path"); module.exports = { - entry: './src/index.ts', + entry: "./src/index.ts", output: { - path: path.resolve('build'), - filename: 'index.js', - libraryTarget: 'umd' + path: path.resolve("build"), + filename: "index.js", + libraryTarget: "umd", }, resolve: { - extensions: ['.tsx', '.ts', '.js'], - modules: [path.resolve('./src'), "node_modules"] + extensions: [".tsx", ".ts", ".js"], + modules: [path.resolve("./src"), "node_modules"], }, module: { @@ -19,13 +19,10 @@ module.exports = { { test: /\.ts$/, loaders: ["awesome-typescript-loader"], - } + }, ], }, - target: 'node', - - node: { - __dirname: true - } + // Because docx is now targetting web + // target: 'node', }; diff --git a/webpack.web.config.js b/webpack.web.config.js deleted file mode 100644 index 9672df789c..0000000000 --- a/webpack.web.config.js +++ /dev/null @@ -1,32 +0,0 @@ -const path = require('path'); - -module.exports = { - entry: './src/index.ts', - - output: { - path: path.resolve('build'), - filename: 'index.web.js', - libraryTarget: 'umd' - }, - - resolve: { - extensions: ['.tsx', '.ts', '.js'], - modules: [path.resolve('./src'), "node_modules"] - }, - - module: { - rules: [ - { - test: /\.ts$/, - loaders: ["awesome-typescript-loader"], - } - ], - }, - - node: { - __dirname: true, - fs: "empty", - tls: "empty", - net: "empty" - } -}; From bf5bcea607a14d5e81a5be4c42f21fe5f0ced3cd Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 15 Aug 2018 22:20:43 +0100 Subject: [PATCH 12/19] Make add image support base64 strings --- src/file/file.ts | 2 +- src/file/media/data.ts | 2 +- src/file/media/media.ts | 53 ++++++++++++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/file/file.ts b/src/file/file.ts index becc713176..9ab305fd57 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -129,7 +129,7 @@ export class File { return this; } - public createImage(buffer: Buffer, width?: number, height?: number): Image { + public createImage(buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): Image { const image = Media.addImage(this, buffer, width, height); this.document.addParagraph(image.Paragraph); diff --git a/src/file/media/data.ts b/src/file/media/data.ts index 9ac7ab5eee..42e5874f99 100644 --- a/src/file/media/data.ts +++ b/src/file/media/data.ts @@ -11,7 +11,7 @@ export interface IMediaDataDimensions { export interface IMediaData { referenceId: number; - stream: Buffer; + stream: Buffer | Uint8Array | ArrayBuffer; path?: string; fileName: string; dimensions: IMediaDataDimensions; diff --git a/src/file/media/media.ts b/src/file/media/media.ts index f2cb71aa2f..1c8e0609dc 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -1,5 +1,3 @@ -import * as sizeOf from "image-size"; - import { File } from "../file"; import { ImageParagraph } from "../paragraph"; import { IMediaData } from "./data"; @@ -10,7 +8,7 @@ interface IHackedFile { } export class Media { - public static addImage(file: File, buffer: Buffer, width?: number, height?: number): Image { + public static addImage(file: File, buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): Image { // Workaround to expose id without exposing to API const exposedFile = (file as {}) as IHackedFile; const mediaData = file.Media.addMedia(buffer, exposedFile.currentRelationshipId++, width, height); @@ -51,28 +49,36 @@ export class Media { return data; } - public addMedia(buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData { + public addMedia( + buffer: Buffer | string | Uint8Array | ArrayBuffer, + referenceId: number, + width: number = 100, + height: number = 100, + ): IMediaData { const key = `${Media.generateId()}.png`; - let dimensions; - if (width && height) { - dimensions = { + + return this.createMedia( + key, + referenceId, + { width: width, height: height, - }; - } else { - dimensions = sizeOf(buffer); - } - - return this.createMedia(key, referenceId, dimensions, buffer); + }, + buffer, + ); } private createMedia( key: string, relationshipsCount: number, dimensions: { width: number; height: number }, - data: Buffer, + data: Buffer | string | Uint8Array | ArrayBuffer, filePath?: string, ): IMediaData { + if (typeof data === "string") { + data = this.convertDataURIToBinary(data); + } + const imageData = { referenceId: this.map.size + relationshipsCount + 1, stream: data, @@ -104,4 +110,23 @@ export class Media { return array; } + + private convertDataURIToBinary(dataURI: string): Uint8Array { + // https://gist.github.com/borismus/1032746 + // https://github.com/mafintosh/base64-to-uint8array + const BASE64_MARKER = ";base64,"; + + const base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length; + + if (typeof atob === "function") { + return new Uint8Array( + atob(dataURI.substring(base64Index)) + .split("") + .map((c) => c.charCodeAt(0)), + ); + } else { + const b = require("buf" + "fer"); + return new b.Buffer(dataURI, "base64"); + } + } } From f9a0d1b3881459347b97f34a4110bc750d35a5df Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 15 Aug 2018 23:06:48 +0100 Subject: [PATCH 13/19] Fix tests --- src/file/drawing/drawing.spec.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts index 3bbb3f815e..87ea5e9e44 100644 --- a/src/file/drawing/drawing.spec.ts +++ b/src/file/drawing/drawing.spec.ts @@ -1,16 +1,17 @@ import { assert } from "chai"; -import * as fs from "fs"; import { Utility } from "../../tests/utility"; import { Drawing, IDrawingOptions, PlacementPosition } from "./"; +const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8j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Y9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`; + function createDrawing(drawingOptions?: IDrawingOptions): Drawing { const path = "./demo/images/image1.jpeg"; return new Drawing( { fileName: "test.jpg", referenceId: 1, - stream: fs.readFileSync(path), + stream: Buffer.from(imageBase64Data, "base64"), path: path, dimensions: { pixels: { From a7f6224fb2df29dc620c25718a486b61f0b05164 Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 21 Aug 2018 02:46:21 +0100 Subject: [PATCH 14/19] Made demo files into typescript files --- demo/demo1.js | 20 ------- demo/demo1.ts | 20 +++++++ demo/{demo10.js => demo10.ts} | 62 +++++++++++--------- demo/{demo11.js => demo11.ts} | 52 ++++++++++------- demo/{demo12.js => demo12.ts} | 19 +++--- demo/demo13.js | 26 --------- demo/demo13.ts | 28 +++++++++ demo/demo14.js | 24 -------- demo/demo14.ts | 28 +++++++++ demo/demo15.js | 14 ----- demo/demo15.ts | 18 ++++++ demo/demo16.js | 36 ------------ demo/demo16.ts | 40 +++++++++++++ demo/demo17.js | 17 ------ demo/demo17.ts | 21 +++++++ demo/{demo18.js => demo18.ts} | 18 +++--- demo/demo19.js | 20 ------- demo/demo19.ts | 20 +++++++ demo/demo2.js | 74 ----------------------- demo/demo2.ts | 78 +++++++++++++++++++++++++ demo/demo20.js | 17 ------ demo/demo20.ts | 21 +++++++ demo/demo21.js | 31 ---------- demo/demo21.ts | 37 ++++++++++++ demo/demo22.js | 26 --------- demo/demo22.ts | 27 +++++++++ demo/{demo23.js => demo23.ts} | 29 ++++----- demo/demo24.js | 15 ----- demo/demo24.ts | 18 ++++++ demo/demo25.js | 15 ----- demo/demo26.js | 21 ------- demo/demo3.js | 45 -------------- demo/demo3.ts | 46 +++++++++++++++ demo/demo4.js | 12 ---- demo/demo4.ts | 15 +++++ demo/demo5.js | 18 ------ demo/demo5.ts | 21 +++++++ demo/demo6.js | 25 -------- demo/demo6.ts | 29 +++++++++ demo/demo7.js | 14 ----- demo/demo7.ts | 18 ++++++ demo/demo8.js | 13 ----- demo/demo8.ts | 17 ++++++ demo/demo9.js | 14 ----- demo/demo9.ts | 17 ++++++ demo/index.js | 8 ++- package.json | 4 +- src/file/paragraph/formatting/indent.ts | 4 +- src/file/paragraph/paragraph.ts | 8 ++- tsconfig.json | 3 +- 50 files changed, 638 insertions(+), 585 deletions(-) delete mode 100644 demo/demo1.js create mode 100644 demo/demo1.ts rename demo/{demo10.js => demo10.ts} (83%) rename demo/{demo11.js => demo11.ts} (75%) rename demo/{demo12.js => demo12.ts} (50%) delete mode 100644 demo/demo13.js create mode 100644 demo/demo13.ts delete mode 100644 demo/demo14.js create mode 100644 demo/demo14.ts delete mode 100644 demo/demo15.js create mode 100644 demo/demo15.ts delete mode 100644 demo/demo16.js create mode 100644 demo/demo16.ts delete mode 100644 demo/demo17.js create mode 100644 demo/demo17.ts rename demo/{demo18.js => demo18.ts} (89%) delete mode 100644 demo/demo19.js create mode 100644 demo/demo19.ts delete mode 100644 demo/demo2.js create mode 100644 demo/demo2.ts delete mode 100644 demo/demo20.js create mode 100644 demo/demo20.ts delete mode 100644 demo/demo21.js create mode 100644 demo/demo21.ts delete mode 100644 demo/demo22.js create mode 100644 demo/demo22.ts rename demo/{demo23.js => demo23.ts} (82%) delete mode 100644 demo/demo24.js create mode 100644 demo/demo24.ts delete mode 100644 demo/demo25.js delete mode 100644 demo/demo26.js delete mode 100644 demo/demo3.js create mode 100644 demo/demo3.ts delete mode 100644 demo/demo4.js create mode 100644 demo/demo4.ts delete mode 100644 demo/demo5.js create mode 100644 demo/demo5.ts delete mode 100644 demo/demo6.js create mode 100644 demo/demo6.ts delete mode 100644 demo/demo7.js create mode 100644 demo/demo7.ts delete mode 100644 demo/demo8.js create mode 100644 demo/demo8.ts delete mode 100644 demo/demo9.js create mode 100644 demo/demo9.ts diff --git a/demo/demo1.js b/demo/demo1.js deleted file mode 100644 index fc8d4c31a1..0000000000 --- a/demo/demo1.js +++ /dev/null @@ -1,20 +0,0 @@ -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.Packer(); - -packer.toBuffer(doc).then((buffer) => { - fs.writeFileSync('My Document.docx', buffer); -}); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo1.ts b/demo/demo1.ts new file mode 100644 index 0000000000..ba3a0bcc2f --- /dev/null +++ b/demo/demo1.ts @@ -0,0 +1,20 @@ +// Simple example to add text to a document +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World"); +const institutionText = new TextRun("Foo Bar").bold(); +const dateText = new TextRun("Github is the best").tab().bold(); +paragraph.addRun(institutionText); +paragraph.addRun(dateText); + +doc.addParagraph(paragraph); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo10.js b/demo/demo10.ts similarity index 83% rename from demo/demo10.js rename to demo/demo10.ts index 2b986dbf65..b55542b866 100644 --- a/demo/demo10.js +++ b/demo/demo10.ts @@ -1,8 +1,11 @@ -const docx = require("../build"); +// Add images to header and footer +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; const PHONE_NUMBER = "07534563401"; const PROFILE_URL = "https://www.linkedin.com/in/dolan1"; -const EMAIL = "docx@docx.com"; +const EMAIL = "docx@com"; const experiences = [ { @@ -122,13 +125,13 @@ const achievements = [ ]; class DocumentCreator { - create(data) { + public create(data): Document { const experiences = data[0]; const educations = data[1]; const skills = data[2]; const achivements = data[3]; - const document = new docx.Document(); - document.addParagraph(new docx.Paragraph("Dolan Miu").title()); + const document = new Document(); + document.addParagraph(new Paragraph("Dolan Miu").title()); document.addParagraph(this.createContactInfo(PHONE_NUMBER, PROFILE_URL, EMAIL)); document.addParagraph(this.createHeading("Education")); @@ -181,23 +184,23 @@ class DocumentCreator { document.addParagraph(this.createHeading("References")); document.addParagraph( - new docx.Paragraph( + new Paragraph( "Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk", ), ); - document.addParagraph(new docx.Paragraph("More references upon request")); + document.addParagraph(new Paragraph("More references upon request")); document.addParagraph( - new docx.Paragraph( + new Paragraph( "This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.", ).center(), ); return document; } - createContactInfo(phoneNumber, profileUrl, email) { - const paragraph = new docx.Paragraph().center(); - const contactInfo = new docx.TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`); - const address = new docx.TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break(); + createContactInfo(phoneNumber: string, profileUrl: string, email: string) { + const paragraph = new Paragraph().center(); + const contactInfo = new TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`); + const address = new TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break(); paragraph.addRun(contactInfo); paragraph.addRun(address); @@ -206,17 +209,17 @@ class DocumentCreator { } createHeading(text) { - return new docx.Paragraph(text).heading1().thematicBreak(); + return new Paragraph(text).heading1().thematicBreak(); } createSubHeading(text) { - return new docx.Paragraph(text).heading2(); + return new Paragraph(text).heading2(); } createInstitutionHeader(institutionName, dateText) { - const paragraph = new docx.Paragraph().maxRightTabStop(); - const institution = new docx.TextRun(institutionName).bold(); - const date = new docx.TextRun(dateText).tab().bold(); + const paragraph = new Paragraph().maxRightTabStop(); + const institution = new TextRun(institutionName).bold(); + const date = new TextRun(dateText).tab().bold(); paragraph.addRun(institution); paragraph.addRun(date); @@ -225,8 +228,8 @@ class DocumentCreator { } createRoleText(roleText) { - const paragraph = new docx.Paragraph(); - const role = new docx.TextRun(roleText).italic(); + const paragraph = new Paragraph(); + const role = new TextRun(roleText).italic(); paragraph.addRun(role); @@ -234,32 +237,32 @@ class DocumentCreator { } createBullet(text) { - return new docx.Paragraph(text).bullet(); + return new Paragraph(text).bullet(); } createSkillList(skills) { - const paragraph = new docx.Paragraph(); + const paragraph = new Paragraph(); const skillConcat = skills.map((skill) => skill.name).join(", ") + "."; - paragraph.addRun(new docx.TextRun(skillConcat)); + paragraph.addRun(new TextRun(skillConcat)); return paragraph; } - createAchivementsList(achivements) { + public createAchivementsList(achivements): Paragraph { const arr = []; for (const achievement of achivements) { - arr.push(new docx.Paragraph(achievement.name).bullet()); + arr.push(new Paragraph(achievement.name).bullet()); } return arr; } createInterests(interests) { - const paragraph = new docx.Paragraph(); + const paragraph = new Paragraph(); - paragraph.addRun(new docx.TextRun(interests)); + paragraph.addRun(new TextRun(interests)); return paragraph; } @@ -308,7 +311,8 @@ const documentCreator = new DocumentCreator(); const doc = documentCreator.create([experiences, education, skills, achievements]); -var exporter = new docx.LocalPacker(doc); -exporter.pack("Dolan Miu CV"); +const packer = new Packer(); -console.log("Document created successfully at project root!"); +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo11.js b/demo/demo11.ts similarity index 75% rename from demo/demo11.js rename to demo/demo11.ts index f0597e0da4..3b0cf0b01f 100644 --- a/demo/demo11.js +++ b/demo/demo11.ts @@ -1,7 +1,9 @@ -const docx = require("../build"); -const fs = require('fs'); +// Setting styles with JavaScript configuration +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, Table } from "../build"; -const doc = new docx.Document(undefined, { +const doc = new Document(undefined, { top: 700, right: 700, bottom: 700, @@ -52,7 +54,7 @@ doc.Styles.createParagraphStyle("normalPara", "Normal Para") .font("Calibri") .quickFormat() .leftTabStop(453.543307087) - .maxRightTabStop(453.543307087) + .maxRightTabStop() .size(26) .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); @@ -70,7 +72,7 @@ doc.Styles.createParagraphStyle("aside", "Aside") .next("Normal") .color("999999") .italics() - .indent(720) + .indent({ left: 720 }) .spacing({ line: 276 }); doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced") @@ -105,29 +107,35 @@ doc.createParagraph("Sir,").style("normalPara"); doc.createParagraph("BRIEF DESCRIPTION").style("normalPara"); -var table = new docx.Table(4, 4); -var contentParagraph = table +const table = new Table(4, 4); +table .getRow(0) .getCell(0) - .addContent(new docx.Paragraph("Pole No.")); -table.properties.width = 10000; + .addContent(new Paragraph("Pole No.")); +table.Properties.width = 10000; doc.addTable(table); -var arrboth = [{ - image: "./demo/images/pizza.gif", - comment: "Test" -}, { - image: "./demo/images/pizza.gif", - comment: "Test 2" -}]; +const arrboth = [ + { + image: "./demo/images/pizza.gif", + comment: "Test", + }, + { + image: "./demo/images/pizza.gif", + comment: "Test 2", + }, +]; -arrboth.forEach(function(item) { +arrboth.forEach((item) => { const para = doc.createParagraph(); - para.createTextRun(doc.createImage(fs.readFileSync(item.image))); - para.properties.width = 60; - para.properties.height = 90; + para.addImage(doc.createImage(fs.readFileSync(item.image))); + para.Properties.width = 60; + para.Properties.height = 90; doc.createParagraph(item.comment).style("normalPara2"); }); -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo12.js b/demo/demo12.ts similarity index 50% rename from demo/demo12.js rename to demo/demo12.ts index 87c7a405e0..a3479c3cbf 100644 --- a/demo/demo12.js +++ b/demo/demo12.ts @@ -1,9 +1,11 @@ -const docx = require("../build"); -const fs = require('fs'); +// Scaling images +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; -var doc = new docx.Document(); +const doc = new Document(); -var paragraph = new docx.Paragraph("Hello World"); +const paragraph = new Paragraph("Hello World"); doc.addParagraph(paragraph); const image = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); @@ -12,11 +14,12 @@ const image3 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); const image4 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); image.scale(0.5); -image2.scale(1) +image2.scale(1); image3.scale(2.5); image4.scale(4); -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); +const packer = new Packer(); -console.log("Document created successfully at project root!"); +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo13.js b/demo/demo13.js deleted file mode 100644 index f141547d7a..0000000000 --- a/demo/demo13.js +++ /dev/null @@ -1,26 +0,0 @@ -// This example shows 3 styles -const fs = require('fs'); -const docx = require('../build'); - -const styles = fs.readFileSync('./demo/assets/custom-styles.xml', 'utf-8'); -const doc = new docx.Document({ - title: 'Title', - externalStyles: styles -}); - -doc.createParagraph('Cool Heading Text').heading1(); - -let paragraph = new docx.Paragraph('This is a custom named style from the template "MyFancyStyle"'); -paragraph.style('MyFancyStyle'); -doc.addParagraph(paragraph); - -doc.createParagraph('Some normal text') - -doc.createParagraph('MyFancyStyle again').style('MyFancyStyle'); -paragraph.style('MyFancyStyle'); -doc.addParagraph(paragraph); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo13.ts b/demo/demo13.ts new file mode 100644 index 0000000000..b80b9fdc22 --- /dev/null +++ b/demo/demo13.ts @@ -0,0 +1,28 @@ +// This example shows 3 styles using XML styles +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8"); +const doc = new Document({ + title: "Title", + externalStyles: styles, +}); + +doc.createParagraph("Cool Heading Text").heading1(); + +const paragraph = new Paragraph('This is a custom named style from the template "MyFancyStyle"'); +paragraph.style("MyFancyStyle"); +doc.addParagraph(paragraph); + +doc.createParagraph("Some normal text"); + +doc.createParagraph("MyFancyStyle again").style("MyFancyStyle"); +paragraph.style("MyFancyStyle"); +doc.addParagraph(paragraph); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo14.js b/demo/demo14.js deleted file mode 100644 index b2262f106a..0000000000 --- a/demo/demo14.js +++ /dev/null @@ -1,24 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -doc.createParagraph("First Page").pageBreak() -doc.createParagraph("Second Page"); - -var pageNumber = new docx.TextRun().pageNumber() - -var pageoneheader = new docx.Paragraph("First Page Header ").right(); - -pageoneheader.addRun(pageNumber); -var firstPageHeader = doc.createFirstPageHeader(); -firstPageHeader.addParagraph(pageoneheader); - -var pagetwoheader = new docx.Paragraph("My Title ").right(); - -pagetwoheader.addRun(pageNumber) -doc.Header.addParagraph(pagetwoheader) - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo14.ts b/demo/demo14.ts new file mode 100644 index 0000000000..94d45a66a6 --- /dev/null +++ b/demo/demo14.ts @@ -0,0 +1,28 @@ +// Page numbers +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +doc.createParagraph("First Page").pageBreak(); +doc.createParagraph("Second Page"); + +const pageNumber = new TextRun("Page ").pageNumber(); + +const pageoneheader = new Paragraph("First Page Header ").right(); + +pageoneheader.addRun(pageNumber); +const firstPageHeader = doc.createFirstPageHeader(); +firstPageHeader.addParagraph(pageoneheader); + +const pagetwoheader = new Paragraph("My Title ").right(); + +pagetwoheader.addRun(pageNumber); +doc.Header.addParagraph(pagetwoheader); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo15.js b/demo/demo15.js deleted file mode 100644 index 61d9351817..0000000000 --- a/demo/demo15.js +++ /dev/null @@ -1,14 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -var paragraph2 = new docx.Paragraph("Hello World on another page").pageBreakBefore(); - -doc.addParagraph(paragraph); -doc.addParagraph(paragraph2); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo15.ts b/demo/demo15.ts new file mode 100644 index 0000000000..5da6c826b5 --- /dev/null +++ b/demo/demo15.ts @@ -0,0 +1,18 @@ +// Page break before example +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World"); +const paragraph2 = new Paragraph("Hello World on another page").pageBreakBefore(); + +doc.addParagraph(paragraph); +doc.addParagraph(paragraph2); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo16.js b/demo/demo16.js deleted file mode 100644 index f850bc9b4b..0000000000 --- a/demo/demo16.js +++ /dev/null @@ -1,36 +0,0 @@ -const docx = require("../build"); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World").pageBreak(); - -doc.addParagraph(paragraph); - -var header = doc.createHeader(); -header.createParagraph("Header on another page"); -var footer = doc.createFooter(); -footer.createParagraph("Footer on another page"); - -doc.addSection({ - headerId: header.Header.ReferenceId, - footerId: footer.Footer.ReferenceId, - pageNumberStart: 1, - pageNumberFormatType: docx.PageNumberFormat.DECIMAL, -}); - -doc.createParagraph("hello"); - -doc.addSection({ - headerId: header.Header.ReferenceId, - footerId: footer.Footer.ReferenceId, - pageNumberStart: 1, - pageNumberFormatType: docx.PageNumberFormat.DECIMAL, - orientation: docx.PageOrientation.LANDSCAPE, -}); - -doc.createParagraph("hello in landscape"); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo16.ts b/demo/demo16.ts new file mode 100644 index 0000000000..4b92dc0d7e --- /dev/null +++ b/demo/demo16.ts @@ -0,0 +1,40 @@ +// Multiple sections and headers +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, PageNumberFormat, PageOrientation, Paragraph } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World").pageBreak(); + +doc.addParagraph(paragraph); + +const header = doc.createHeader(); +header.createParagraph("Header on another page"); +const footer = doc.createFooter(); +footer.createParagraph("Footer on another page"); + +doc.addSection({ + headerId: header.Header.ReferenceId, + footerId: footer.Footer.ReferenceId, + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, +}); + +doc.createParagraph("hello"); + +doc.addSection({ + headerId: header.Header.ReferenceId, + footerId: footer.Footer.ReferenceId, + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, + orientation: PageOrientation.LANDSCAPE, +}); + +doc.createParagraph("hello in landscape"); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo17.js b/demo/demo17.js deleted file mode 100644 index 266e556b7d..0000000000 --- a/demo/demo17.js +++ /dev/null @@ -1,17 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World").referenceFootnote(1); -var paragraph2 = new docx.Paragraph("Hello World").referenceFootnote(2); - -doc.addParagraph(paragraph); -doc.addParagraph(paragraph2); - -doc.createFootnote(new docx.Paragraph("Test")); -doc.createFootnote(new docx.Paragraph("My amazing reference")); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo17.ts b/demo/demo17.ts new file mode 100644 index 0000000000..c48a2e67e8 --- /dev/null +++ b/demo/demo17.ts @@ -0,0 +1,21 @@ +// Footnotes +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World").referenceFootnote(1); +const paragraph2 = new Paragraph("Hello World").referenceFootnote(2); + +doc.addParagraph(paragraph); +doc.addParagraph(paragraph2); + +doc.createFootnote(new Paragraph("Test")); +doc.createFootnote(new Paragraph("My amazing reference")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo18.js b/demo/demo18.ts similarity index 89% rename from demo/demo18.js rename to demo/demo18.ts index 73a778a6c3..4a61488c1f 100644 --- a/demo/demo18.js +++ b/demo/demo18.ts @@ -1,15 +1,17 @@ // Insert image from a buffer -const docx = require('../build'); +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; -var doc = new docx.Document(); +const doc = new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XiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`; // doc.createImage(Buffer.from(imageBase64Data, 'base64')); -doc.createImage(Buffer.from(imageBase64Data, 'base64'), 100, 100); +doc.createImage(Buffer.from(imageBase64Data, "base64"), 100, 100); -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); +const packer = new Packer(); -console.log('Document created successfully at project root!'); +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo19.js b/demo/demo19.js deleted file mode 100644 index 3aafebc142..0000000000 --- a/demo/demo19.js +++ /dev/null @@ -1,20 +0,0 @@ -const fs = require("fs"); -const docx = require("../build"); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -var institutionText = new docx.TextRun("Foo").bold(); -var dateText = new docx.TextRun("Bar").tab().bold(); -paragraph.addRun(institutionText); -paragraph.addRun(dateText); - -doc.addParagraph(paragraph); - -var exporter = new docx.BufferPacker(doc); -exporter.pack("My Document").then((buffer) => { - // At this point, you can do anything with the buffer, including casting it to a string etc. - console.log(buffer); - fs.writeFileSync('My Document.docx', buffer); - console.log("Document created successfully at project root!"); -}); diff --git a/demo/demo19.ts b/demo/demo19.ts new file mode 100644 index 0000000000..52b5fd6694 --- /dev/null +++ b/demo/demo19.ts @@ -0,0 +1,20 @@ +// Export to base64 string - Useful in a browser environment. +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World"); +const institutionText = new TextRun("Foo").bold(); +const dateText = new TextRun("Bar").tab().bold(); +paragraph.addRun(institutionText); +paragraph.addRun(dateText); + +doc.addParagraph(paragraph); + +const packer = new Packer(); + +packer.toBase64String(doc).then((str) => { + fs.writeFileSync("My Document.docx", str); +}); diff --git a/demo/demo2.js b/demo/demo2.js deleted file mode 100644 index 841e31b6bf..0000000000 --- a/demo/demo2.js +++ /dev/null @@ -1,74 +0,0 @@ -const docx = require('../build'); - -const doc = new docx.Document({ - creator: 'Clippy', - title: 'Sample Document', - description: 'A brief example of using docx', -}); - -doc.Styles.createParagraphStyle('Heading1', 'Heading 1') - .basedOn("Normal") - .next("Normal") - .quickFormat() - .size(28) - .bold() - .italics() - .spacing({after: 120}); - -doc.Styles.createParagraphStyle('Heading2', 'Heading 2') - .basedOn("Normal") - .next("Normal") - .quickFormat() - .size(26) - .bold() - .underline('double', 'FF0000') - .spacing({before: 240, after: 120}); - -doc.Styles.createParagraphStyle('aside', 'Aside') - .basedOn('Normal') - .next('Normal') - .color('999999') - .italics() - .indent(720) - .spacing({line: 276}); - -doc.Styles.createParagraphStyle('wellSpaced', 'Well Spaced') - .basedOn('Normal') - .spacing({line: 276, before: 20 * 72 * .1, after: 20 * 72 * .05}); - -doc.Styles.createParagraphStyle('ListParagraph', 'List Paragraph') - .quickFormat() - .basedOn('Normal'); - - -const numberedAbstract = doc.Numbering.createAbstractNumbering(); -numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left"); - -doc.createParagraph('Test heading1, bold and italicized').heading1(); -doc.createParagraph('Some simple content'); -doc.createParagraph('Test heading2 with double red underline').heading2(); - -const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract); -const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract); -letterNumbering5.overrideLevel(0, 5); - -doc.createParagraph('Option1').setNumbering(letterNumbering, 0); -doc.createParagraph('Option5 -- override 2 to 5').setNumbering(letterNumbering5, 0); -doc.createParagraph('Option3').setNumbering(letterNumbering, 0); - -doc.createParagraph() - .createTextRun('Some monospaced content') - .font('Monospace'); - -doc.createParagraph('An aside, in light gray italics and indented').style('aside'); -doc.createParagraph('This is normal, but well-spaced text').style('wellSpaced'); -const para = doc.createParagraph(); -para.createTextRun('This is a bold run,').bold(); -para.createTextRun(' switching to normal '); -para.createTextRun('and then underlined ').underline(); -para.createTextRun('and back to normal.'); - -const exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo2.ts b/demo/demo2.ts new file mode 100644 index 0000000000..f32779fa06 --- /dev/null +++ b/demo/demo2.ts @@ -0,0 +1,78 @@ +// Example on how to customise the look at feel using Styles +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; + +const doc = new Document({ + creator: "Clippy", + title: "Sample Document", + description: "A brief example of using docx", +}); + +doc.Styles.createParagraphStyle("Heading1", "Heading 1") + .basedOn("Normal") + .next("Normal") + .quickFormat() + .size(28) + .bold() + .italics() + .spacing({ after: 120 }); + +doc.Styles.createParagraphStyle("Heading2", "Heading 2") + .basedOn("Normal") + .next("Normal") + .quickFormat() + .size(26) + .bold() + .underline("double", "FF0000") + .spacing({ before: 240, after: 120 }); + +doc.Styles.createParagraphStyle("aside", "Aside") + .basedOn("Normal") + .next("Normal") + .color("999999") + .italics() + .indent({ left: 720 }) + .spacing({ line: 276 }); + +doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced") + .basedOn("Normal") + .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); + +doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph") + .quickFormat() + .basedOn("Normal"); + +const numberedAbstract = doc.Numbering.createAbstractNumbering(); +numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left"); + +doc.createParagraph("Test heading1, bold and italicized").heading1(); +doc.createParagraph("Some simple content"); +doc.createParagraph("Test heading2 with double red underline").heading2(); + +const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract); +const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract); +letterNumbering5.overrideLevel(0, 5); + +doc.createParagraph("Option1").setNumbering(letterNumbering, 0); +doc.createParagraph("Option5 -- override 2 to 5").setNumbering(letterNumbering5, 0); +doc.createParagraph("Option3").setNumbering(letterNumbering, 0); + +doc + .createParagraph() + .createTextRun("Some monospaced content") + .font("Monospace"); + +doc.createParagraph("An aside, in light gray italics and indented").style("aside"); +doc.createParagraph("This is normal, but well-spaced text").style("wellSpaced"); +const para = doc.createParagraph(); +para.createTextRun("This is a bold run,").bold(); +para.createTextRun(" switching to normal "); +para.createTextRun("and then underlined ").underline(); +para.createTextRun("and back to normal."); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo20.js b/demo/demo20.js deleted file mode 100644 index eef332c1ae..0000000000 --- a/demo/demo20.js +++ /dev/null @@ -1,17 +0,0 @@ -const docx = require("../build"); - -var doc = new docx.Document(); - -const table = doc.createTable(4, 4); -table - .getCell(2, 2) - .addContent(new docx.Paragraph("Hello")) - .CellProperties.Borders.addTopBorder(docx.BorderStyle.DASH_DOT_STROKED, 3, "red") - .addBottomBorder(docx.BorderStyle.DOUBLE, 3, "blue") - .addStartBorder(docx.BorderStyle.DOT_DOT_DASH, 3, "green") - .addEndBorder(docx.BorderStyle.DOT_DOT_DASH, 3, "#ff8000"); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo20.ts b/demo/demo20.ts new file mode 100644 index 0000000000..f3f43edb26 --- /dev/null +++ b/demo/demo20.ts @@ -0,0 +1,21 @@ +// Add custom borders to table cell +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { BorderStyle, Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const table = doc.createTable(4, 4); +table + .getCell(2, 2) + .addContent(new Paragraph("Hello")) + .CellProperties.Borders.addTopBorder(BorderStyle.DASH_DOT_STROKED, 3, "red") + .addBottomBorder(BorderStyle.DOUBLE, 3, "blue") + .addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green") + .addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000"); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo21.js b/demo/demo21.js deleted file mode 100644 index fd74e61b31..0000000000 --- a/demo/demo21.js +++ /dev/null @@ -1,31 +0,0 @@ -/** This demo shows how to create bookmarks then link to them with internal hyperlinks */ - -const docx = require("../build"); - -const loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante."; - -const doc = new docx.Document({ - creator: 'Clippy', - title: 'Sample Document', - description: 'A brief example of using docx with bookmarks and internal hyperlinks', -}); - -const anchorId = "anchorID"; - -// First create the bookmark -const bookmark = doc.createBookmark(anchorId, "Lorem Ipsum"); -// That has header styling -doc.createParagraph().addBookmark(bookmark).heading1(); -doc.createParagraph("\n"); - -doc.createParagraph(loremIpsum); -doc.createParagraph().pageBreak(); - -// Now the link back up to the bookmark -const hyperlink = doc.createInternalHyperLink(anchorId, `Click me!`); -doc.createParagraph().addHyperLink(hyperlink); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo21.ts b/demo/demo21.ts new file mode 100644 index 0000000000..b02da56065 --- /dev/null +++ b/demo/demo21.ts @@ -0,0 +1,37 @@ +// This demo shows how to create bookmarks then link to them with internal hyperlinks +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; + +const loremIpsum = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante."; + +const doc = new Document({ + creator: "Clippy", + title: "Sample Document", + description: "A brief example of using docx with bookmarks and internal hyperlinks", +}); + +const anchorId = "anchorID"; + +// First create the bookmark +const bookmark = doc.createBookmark(anchorId, "Lorem Ipsum"); +// That has header styling +doc + .createParagraph() + .addBookmark(bookmark) + .heading1(); +doc.createParagraph("\n"); + +doc.createParagraph(loremIpsum); +doc.createParagraph().pageBreak(); + +// Now the link back up to the bookmark +const hyperlink = doc.createInternalHyperLink(anchorId, `Click me!`); +doc.createParagraph().addHyperLink(hyperlink); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo22.js b/demo/demo22.js deleted file mode 100644 index 9bc7e9ac54..0000000000 --- a/demo/demo22.js +++ /dev/null @@ -1,26 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - - - -var paragraph1 = new docx.Paragraph().bidirectional(); -var textRun1 = new docx.TextRun("שלום עולם").rightToLeft(); -paragraph1.addRun(textRun1); -doc.addParagraph(paragraph1); - -var paragraph2 = new docx.Paragraph().bidirectional(); -var textRun2 = new docx.TextRun("שלום עולם").bold().rightToLeft(); -paragraph2.addRun(textRun2); -doc.addParagraph(paragraph2); - -var paragraph3 = new docx.Paragraph().bidirectional(); -var textRun3 = new docx.TextRun("שלום עולם").italic().rightToLeft(); -paragraph3.addRun(textRun3); -doc.addParagraph(paragraph3); - - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo22.ts b/demo/demo22.ts new file mode 100644 index 0000000000..c5051ab019 --- /dev/null +++ b/demo/demo22.ts @@ -0,0 +1,27 @@ +// This demo shows right to left for special languages +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +const paragraph1 = new Paragraph().bidirectional(); +const textRun1 = new TextRun("שלום עולם").rightToLeft(); +paragraph1.addRun(textRun1); +doc.addParagraph(paragraph1); + +const paragraph2 = new Paragraph().bidirectional(); +const textRun2 = new TextRun("שלום עולם").bold().rightToLeft(); +paragraph2.addRun(textRun2); +doc.addParagraph(paragraph2); + +const paragraph3 = new Paragraph().bidirectional(); +const textRun3 = new TextRun("שלום עולם").italic().rightToLeft(); +paragraph3.addRun(textRun3); +doc.addParagraph(paragraph3); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo23.js b/demo/demo23.ts similarity index 82% rename from demo/demo23.js rename to demo/demo23.ts index 7fd5ebb6fb..47ee26a6f9 100644 --- a/demo/demo23.js +++ b/demo/demo23.ts @@ -1,19 +1,21 @@ // This demo adds an image to the Media cache, and then insert to the document afterwards -const docx = require("../build"); +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Media, Packer, Paragraph } from "../build"; -var doc = new docx.Document(); +const doc = new Document(); -var paragraph = new docx.Paragraph("Hello World"); +const paragraph = new Paragraph("Hello World"); doc.addParagraph(paragraph); -const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg"); -const image2 = docx.Media.addImage(doc, "./demo/images/dog.png"); -const image3 = docx.Media.addImage(doc, "./demo/images/cat.jpg"); -const image4 = docx.Media.addImage(doc, "./demo/images/parrots.bmp"); -const image5 = docx.Media.addImage(doc, "./demo/images/pizza.gif"); +const image = Media.addImage(doc, "./demo/images/image1.jpeg"); +const image2 = Media.addImage(doc, "./demo/images/dog.png"); +const image3 = Media.addImage(doc, "./demo/images/cat.jpg"); +const image4 = Media.addImage(doc, "./demo/images/parrots.bmp"); +const image5 = Media.addImage(doc, "./demo/images/pizza.gif"); -const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiA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Ug4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC` -const image6 = docx.Media.addImageFromBuffer(doc, Buffer.from(imageBase64Data, 'base64'), 100, 100); +const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSE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joE//eoqDCXBSTO6f1xd+O0iyNRY6jaWi1ALNYCocZROj4JdEikroVkjFk9DcStXxpdfCD2MoXodu4RUU9ptxxmXssOfxnvDVcxRTod9FxyhqLoAqis5aPhwTDp9spRgEH2Q6KLbYoKqlaKTm6Isp0C/sJMnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`; +const image6 = Media.addImage(doc, Buffer.from(imageBase64Data, "base64"), 100, 100); // I am adding an image to the paragraph rather than the document to make the image inline paragraph.addImage(image5); @@ -25,7 +27,8 @@ doc.addImage(image4); doc.addImage(image5); doc.addImage(image6); -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); +const packer = new Packer(); -console.log("Document created successfully at project root!"); +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo24.js b/demo/demo24.js deleted file mode 100644 index a95189a6ed..0000000000 --- a/demo/demo24.js +++ /dev/null @@ -1,15 +0,0 @@ -// Add image to table cell -const docx = require('../build'); - -var doc = new docx.Document(); - -const table = doc.createTable(4, 4); -table.getCell(2, 2).addContent(new docx.Paragraph('Hello')); - -const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg"); -table.getCell(1, 1).addContent(image); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo24.ts b/demo/demo24.ts new file mode 100644 index 0000000000..23828aec65 --- /dev/null +++ b/demo/demo24.ts @@ -0,0 +1,18 @@ +// Add image to table cell +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Media, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const table = doc.createTable(4, 4); +table.getCell(2, 2).addContent(new Paragraph("Hello")); + +const image = Media.addImage(doc, "./demo/images/image1.jpeg"); +table.getCell(1, 1).addContent(image.Paragraph); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo25.js b/demo/demo25.js deleted file mode 100644 index 85dd29febe..0000000000 --- a/demo/demo25.js +++ /dev/null @@ -1,15 +0,0 @@ -const fs = require("fs"); -const docx = require("../build"); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -var institutionText = new docx.TextRun("Foo").bold(); -var dateText = new docx.TextRun("Bar").tab().bold(); -paragraph.addRun(institutionText); -paragraph.addRun(dateText); - -doc.addParagraph(paragraph); - -var exporter = new docx.LocalPacker(doc); -exporter.packPdf("My Document"); diff --git a/demo/demo26.js b/demo/demo26.js deleted file mode 100644 index c88fdaea8e..0000000000 --- a/demo/demo26.js +++ /dev/null @@ -1,21 +0,0 @@ -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/demo/demo3.js b/demo/demo3.js deleted file mode 100644 index accd597905..0000000000 --- a/demo/demo3.js +++ /dev/null @@ -1,45 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -const numbering = new docx.Numbering(); - -const abstractNum = numbering.createAbstractNumbering(); -abstractNum.createLevel(0, "upperRoman", "%1", "start") - .addParagraphProperty(new docx.Indent(720, 260)); -abstractNum.createLevel(1, "decimal", "%2.", "start") - .addParagraphProperty(new docx.Indent(1440, 980)); -abstractNum.createLevel(2, "lowerLetter", "%3)", "start") - .addParagraphProperty(new docx.Indent(2160, 1700)); - -const concrete = numbering.createConcreteNumbering(abstractNum); - -var topLevelP = new docx.Paragraph("Hey you"); -var subP = new docx.Paragraph("What's up fam"); -var secondSubP = new docx.Paragraph("Hello World 2"); -var subSubP = new docx.Paragraph("Yeah boi"); - -topLevelP.setNumbering(concrete, 0); -subP.setNumbering(concrete, 1); -secondSubP.setNumbering(concrete, 1); -subSubP.setNumbering(concrete, 2); - -doc.addParagraph(topLevelP); -doc.addParagraph(subP); -doc.addParagraph(secondSubP); -doc.addParagraph(subSubP); - -var bullet1 = new docx.Paragraph("Hey you").bullet(); -var bullet2 = new docx.Paragraph("What's up fam").bullet(1); -var bullet3 = new docx.Paragraph("Hello World 2").bullet(2); -var bullet4 = new docx.Paragraph("Yeah boi").bullet(3); - -doc.addParagraph(bullet1); -doc.addParagraph(bullet2); -doc.addParagraph(bullet3); -doc.addParagraph(bullet4); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo3.ts b/demo/demo3.ts new file mode 100644 index 0000000000..ac3680ef32 --- /dev/null +++ b/demo/demo3.ts @@ -0,0 +1,46 @@ +// Numbering and bullet points example +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Indent, Numbering, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const numbering = new Numbering(); + +const abstractNum = numbering.createAbstractNumbering(); +abstractNum.createLevel(0, "upperRoman", "%1", "start").addParagraphProperty(new Indent({ left: 720, hanging: 260 })); +abstractNum.createLevel(1, "decimal", "%2.", "start").addParagraphProperty(new Indent({ left: 1440, hanging: 980 })); +abstractNum.createLevel(2, "lowerLetter", "%3)", "start").addParagraphProperty(new Indent({ left: 14402160, hanging: 1700 })); + +const concrete = numbering.createConcreteNumbering(abstractNum); + +const topLevelP = new Paragraph("Hey you"); +const subP = new Paragraph("What's up fam"); +const secondSubP = new Paragraph("Hello World 2"); +const subSubP = new Paragraph("Yeah boi"); + +topLevelP.setNumbering(concrete, 0); +subP.setNumbering(concrete, 1); +secondSubP.setNumbering(concrete, 1); +subSubP.setNumbering(concrete, 2); + +doc.addParagraph(topLevelP); +doc.addParagraph(subP); +doc.addParagraph(secondSubP); +doc.addParagraph(subSubP); + +const bullet1 = new Paragraph("Hey you").bullet(); +const bullet2 = new Paragraph("What's up fam").bullet(1); +const bullet3 = new Paragraph("Hello World 2").bullet(2); +const bullet4 = new Paragraph("Yeah boi").bullet(3); + +doc.addParagraph(bullet1); +doc.addParagraph(bullet2); +doc.addParagraph(bullet3); +doc.addParagraph(bullet4); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo4.js b/demo/demo4.js deleted file mode 100644 index 6ded74254c..0000000000 --- a/demo/demo4.js +++ /dev/null @@ -1,12 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -const table = doc.createTable(4, 4); -table.getCell(2, 2).addContent(new docx.Paragraph('Hello')); - - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo4.ts b/demo/demo4.ts new file mode 100644 index 0000000000..eb6f5b8c09 --- /dev/null +++ b/demo/demo4.ts @@ -0,0 +1,15 @@ +// Example of how you would create a table and add data to it +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const table = doc.createTable(4, 4); +table.getCell(2, 2).addContent(new Paragraph("Hello")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo5.js b/demo/demo5.js deleted file mode 100644 index fbb2cd0eaf..0000000000 --- a/demo/demo5.js +++ /dev/null @@ -1,18 +0,0 @@ -const docx = require("../build"); -const fs = require('fs'); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -doc.addParagraph(paragraph); - -doc.createImage(fs.readFileSync("./demo/images/image1.jpeg")); -doc.createImage(fs.readFileSync("./demo/images/dog.png")); -doc.createImage(fs.readFileSync("./demo/images/cat.jpg")); -doc.createImage(fs.readFileSync("./demo/images/parrots.bmp")); -doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo5.ts b/demo/demo5.ts new file mode 100644 index 0000000000..e56cdfc01e --- /dev/null +++ b/demo/demo5.ts @@ -0,0 +1,21 @@ +// Example of how to add images to the document - You can use Buffers, UInt8Arrays or Base64 strings +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World"); +doc.addParagraph(paragraph); + +doc.createImage(fs.readFileSync("./demo/images/image1.jpeg")); +doc.createImage(fs.readFileSync("./demo/images/dog.png").toString("base64")); +doc.createImage(fs.readFileSync("./demo/images/cat.jpg")); +doc.createImage(fs.readFileSync("./demo/images/parrots.bmp")); +doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo6.js b/demo/demo6.js deleted file mode 100644 index 939f590d05..0000000000 --- a/demo/demo6.js +++ /dev/null @@ -1,25 +0,0 @@ -const docx = require("../build"); - -var doc = new docx.Document(undefined, { - top: 0, - right: 0, - bottom: 0, - left: 0, -}); - -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); - -doc.createParagraph("Hello World").heading1(); -doc.createParagraph("University College London"); -doc.createParagraph("5th Dec 2015"); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo6.ts b/demo/demo6.ts new file mode 100644 index 0000000000..5c9ff2798f --- /dev/null +++ b/demo/demo6.ts @@ -0,0 +1,29 @@ +// Example of how to change page borders +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(undefined, { + top: 0, + right: 0, + bottom: 0, + left: 0, +}); + +const paragraph = new Paragraph("Hello World"); +const institutionText = new TextRun("Foo bar").bold(); +const dateText = new TextRun("Github is the best").tab().bold(); +paragraph.addRun(institutionText); +paragraph.addRun(dateText); + +doc.addParagraph(paragraph); + +doc.createParagraph("Hello World").heading1(); +doc.createParagraph("Foo bar"); +doc.createParagraph("Github is the best"); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo7.js b/demo/demo7.js deleted file mode 100644 index f60dcd1911..0000000000 --- a/demo/demo7.js +++ /dev/null @@ -1,14 +0,0 @@ -const docx = require("../build"); - -var doc = new docx.Document(undefined, { - orientation: "landscape", -}); - -var paragraph = new docx.Paragraph("Hello World"); - -doc.addParagraph(paragraph); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo7.ts b/demo/demo7.ts new file mode 100644 index 0000000000..fe9fccf928 --- /dev/null +++ b/demo/demo7.ts @@ -0,0 +1,18 @@ +// Example of how to set the document to landscape +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, PageOrientation, Paragraph } from "../build"; + +const doc = new Document(undefined, { + orientation: PageOrientation.LANDSCAPE, +}); + +const paragraph = new Paragraph("Hello World"); + +doc.addParagraph(paragraph); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo8.js b/demo/demo8.js deleted file mode 100644 index 03ba944473..0000000000 --- a/demo/demo8.js +++ /dev/null @@ -1,13 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -doc.createParagraph("Hello World"); - -doc.Header.createParagraph("Header text"); -doc.Footer.createParagraph("Footer text"); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); \ No newline at end of file diff --git a/demo/demo8.ts b/demo/demo8.ts new file mode 100644 index 0000000000..c4fb96a78c --- /dev/null +++ b/demo/demo8.ts @@ -0,0 +1,17 @@ +// Add text to header and footer +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; + +const doc = new Document(); + +doc.createParagraph("Hello World"); + +doc.Header.createParagraph("Header text"); +doc.Footer.createParagraph("Footer text"); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo9.js b/demo/demo9.js deleted file mode 100644 index c0a57cf8f6..0000000000 --- a/demo/demo9.js +++ /dev/null @@ -1,14 +0,0 @@ -const docx = require('../build'); -const fs = require('fs'); - -var doc = new docx.Document(); - -doc.createParagraph("Hello World"); - -doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif")); -doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif")); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo9.ts b/demo/demo9.ts new file mode 100644 index 0000000000..87fe9b5192 --- /dev/null +++ b/demo/demo9.ts @@ -0,0 +1,17 @@ +// Add images to header and footer +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; + +const doc = new Document(); + +doc.createParagraph("Hello World"); + +doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif")); +doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/index.js b/demo/index.js index 8ae9aac536..e6c7d926d0 100644 --- a/demo/index.js +++ b/demo/index.js @@ -18,12 +18,16 @@ prompt.start(); prompt.get(schema, function (err, result) { var demoNumber = result.number; - var filePath = `./demo/demo${demoNumber}.js`; + var filePath = `./demo/demo${demoNumber}.ts`; if (!fs.existsSync(filePath)) { console.error(`demo${demoNumber} does not exist: ${filePath}`); return; } console.log(`Running demo ${demoNumber}`); - shelljs.exec(`node ${filePath}`); + if (shelljs.exec(`npm run ts-node -- ${filePath}`).code === 0) { + console.log("Document created successfully"); + } else { + console.error('Something went wrong with the demo'); + } }); diff --git a/package.json b/package.json index 98acfbfe79..3132ae6445 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "typedoc": "typedoc src/index.ts", "style": "prettier -l \"src/**/*.ts\"", "style.fix": "prettier \"src/**/*.ts\" --write", - "fix-types": "node types-absolute-fixer.js" + "fix-types": "node types-absolute-fixer.js", + "ts-node": "ts-node" }, "pre-commit": [ "style", @@ -76,6 +77,7 @@ "rimraf": "^2.5.2", "shelljs": "^0.7.7", "sinon": "^5.0.7", + "ts-node": "^7.0.1", "tslint": "^5.11.0", "typedoc": "^0.11.1", "typescript": "2.9.2", diff --git a/src/file/paragraph/formatting/indent.ts b/src/file/paragraph/formatting/indent.ts index 0b42246689..cd7528d2b8 100644 --- a/src/file/paragraph/formatting/indent.ts +++ b/src/file/paragraph/formatting/indent.ts @@ -1,7 +1,7 @@ // http://officeopenxml.com/WPindentation.php import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; -interface IIndentAttributesProperties { +export interface IIndentAttributesProperties { left?: number; hanging?: number; firstLine?: number; @@ -20,7 +20,7 @@ class IndentAttributes extends XmlAttributeComponent Date: Tue, 21 Aug 2018 10:27:46 +0100 Subject: [PATCH 15/19] Update .travis.yml --- .travis.yml | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 986233743d..373070eeb8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,18 +8,30 @@ script: - npm test - npm run style - npm run build - - node ./demo/demo1.js - - node ./demo/demo2.js - - node ./demo/demo3.js - - node ./demo/demo4.js - - node ./demo/demo5.js - - node ./demo/demo6.js - - node ./demo/demo7.js - - node ./demo/demo8.js - - node ./demo/demo9.js - - node ./demo/demo10.js - - node ./demo/demo11.js - - node ./demo/demo12.js + - npm run ts-node -- ./demo/demo1.ts + - npm run ts-node -- ./demo/demo2.ts + - npm run ts-node -- ./demo/demo3.ts + - npm run ts-node -- ./demo/demo4.ts + - npm run ts-node -- ./demo/demo5.ts + - npm run ts-node -- ./demo/demo6.ts + - npm run ts-node -- ./demo/demo7.ts + - npm run ts-node -- ./demo/demo8.ts + - npm run ts-node -- ./demo/demo9.ts + - npm run ts-node -- ./demo/demo10.ts + - npm run ts-node -- ./demo/demo11.ts + - npm run ts-node -- ./demo/demo12.ts + - npm run ts-node -- ./demo/demo13.ts + - npm run ts-node -- ./demo/demo14.ts + - npm run ts-node -- ./demo/demo15.ts + - npm run ts-node -- ./demo/demo16.ts + - npm run ts-node -- ./demo/demo17.ts + - npm run ts-node -- ./demo/demo18.ts + - npm run ts-node -- ./demo/demo19.ts + - npm run ts-node -- ./demo/demo20.ts + - npm run ts-node -- ./demo/demo21.ts + - npm run ts-node -- ./demo/demo22.ts + - npm run ts-node -- ./demo/demo23.ts + - npm run ts-node -- ./demo/demo24.ts after_failure: - "cat /home/travis/builds/dolanmiu/docx/npm-debug.log" after_success: From a777842f9d3488562770f202e663df069bd5e290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?= Date: Tue, 21 Aug 2018 07:16:27 -0300 Subject: [PATCH 16/19] updated mocha to version 5 due to vulnerabilities found by npm audit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 52b8d3fc56..c89bbd2de6 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "chai": "^3.5.0", "glob": "^7.1.2", "jszip": "^3.1.5", - "mocha": "^3.2.0", + "mocha": "^5.2.0", "mocha-webpack": "^1.0.1", "pre-commit": "^1.2.2", "prettier": "^1.12.1", From 410080a8a3ccb6bd843ae85a0587fa4e8fe53b07 Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 21 Aug 2018 19:27:07 +0100 Subject: [PATCH 17/19] Fix tests --- demo/demo10.ts | 48 +++++++++++++++++++++++++---------------- demo/demo11.ts | 8 +++---- src/file/table/table.ts | 4 ++++ 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/demo/demo10.ts b/demo/demo10.ts index b55542b866..84713e777f 100644 --- a/demo/demo10.ts +++ b/demo/demo10.ts @@ -3,6 +3,8 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, TextRun } from "../build"; +// tslint:disable:no-shadowed-variable + const PHONE_NUMBER = "07534563401"; const PROFILE_URL = "https://www.linkedin.com/in/dolan1"; const EMAIL = "docx@com"; @@ -125,11 +127,13 @@ const achievements = [ ]; class DocumentCreator { - public create(data): Document { - const experiences = data[0]; - const educations = data[1]; - const skills = data[2]; - const achivements = data[3]; + public create(data: object[]): Document { + // tslint:disable-next-line:no-any + const experiences = data[0] as any[]; + // tslint:disable-next-line:no-any + const educations = data[1] as any[]; + const skills = data[2] as object[]; + const achivements = data[3] as object[]; const document = new Document(); document.addParagraph(new Paragraph("Dolan Miu").title()); @@ -197,7 +201,7 @@ class DocumentCreator { return document; } - createContactInfo(phoneNumber: string, profileUrl: string, email: string) { + public createContactInfo(phoneNumber: string, profileUrl: string, email: string): Paragraph { const paragraph = new Paragraph().center(); const contactInfo = new TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`); const address = new TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break(); @@ -208,15 +212,15 @@ class DocumentCreator { return paragraph; } - createHeading(text) { + public createHeading(text: string): Paragraph { return new Paragraph(text).heading1().thematicBreak(); } - createSubHeading(text) { + public createSubHeading(text: string): Paragraph { return new Paragraph(text).heading2(); } - createInstitutionHeader(institutionName, dateText) { + public createInstitutionHeader(institutionName: string, dateText: string): Paragraph { const paragraph = new Paragraph().maxRightTabStop(); const institution = new TextRun(institutionName).bold(); const date = new TextRun(dateText).tab().bold(); @@ -227,7 +231,7 @@ class DocumentCreator { return paragraph; } - createRoleText(roleText) { + public createRoleText(roleText: string): Paragraph { const paragraph = new Paragraph(); const role = new TextRun(roleText).italic(); @@ -236,11 +240,12 @@ class DocumentCreator { return paragraph; } - createBullet(text) { + public createBullet(text: string): Paragraph { return new Paragraph(text).bullet(); } - createSkillList(skills) { + // tslint:disable-next-line:no-any + public createSkillList(skills: any[]): Paragraph { const paragraph = new Paragraph(); const skillConcat = skills.map((skill) => skill.name).join(", ") + "."; @@ -249,35 +254,38 @@ class DocumentCreator { return paragraph; } - public createAchivementsList(achivements): Paragraph { - const arr = []; + // tslint:disable-next-line:no-any + public createAchivementsList(achivements: any[]): Paragraph[] { + const arr: Paragraph[] = []; for (const achievement of achivements) { - arr.push(new Paragraph(achievement.name).bullet()); + const paragraph = new Paragraph(achievement.name).bullet(); + arr.push(paragraph); } return arr; } - createInterests(interests) { + public createInterests(interests: string): Paragraph { const paragraph = new Paragraph(); paragraph.addRun(new TextRun(interests)); return paragraph; } - splitParagraphIntoBullets(text) { + public splitParagraphIntoBullets(text: string): string[] { return text.split("\n\n"); } - createPositionDateText(startDate, endDate, isCurrent) { + // tslint:disable-next-line:no-any + public createPositionDateText(startDate: any, endDate: any, isCurrent: boolean): string { const startDateText = this.getMonthFromInt(startDate.month) + ". " + startDate.year; const endDateText = isCurrent ? "Present" : `${this.getMonthFromInt(endDate.month)}. ${endDate.year}`; return `${startDateText} - ${endDateText}`; } - getMonthFromInt(value) { + public getMonthFromInt(value: number): string { switch (value) { case 1: return "Jan"; @@ -303,6 +311,8 @@ class DocumentCreator { return "Nov"; case 12: return "Dec"; + default: + return "N/A"; } } } diff --git a/demo/demo11.ts b/demo/demo11.ts index 3b0cf0b01f..76ec915060 100644 --- a/demo/demo11.ts +++ b/demo/demo11.ts @@ -18,7 +18,7 @@ doc.Styles.createParagraphStyle("Heading1", "Heading 1") .size(52) .center() .bold() - .color(000000) + .color("000000") .spacing({ line: 340 }) .underline("single", "000000"); @@ -112,7 +112,7 @@ table .getRow(0) .getCell(0) .addContent(new Paragraph("Pole No.")); -table.Properties.width = 10000; +// table.Properties.width = 10000; doc.addTable(table); const arrboth = [ @@ -129,8 +129,8 @@ const arrboth = [ arrboth.forEach((item) => { const para = doc.createParagraph(); para.addImage(doc.createImage(fs.readFileSync(item.image))); - para.Properties.width = 60; - para.Properties.height = 90; + // para.Properties.width = 60; + // para.Properties.height = 90; doc.createParagraph(item.comment).style("normalPara2"); }); diff --git a/src/file/table/table.ts b/src/file/table/table.ts index d82da34ff2..335f5fd1c2 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -76,6 +76,10 @@ export class Table extends XmlComponent { this.properties.setFixedWidthLayout(); return this; } + + public get Properties(): TableProperties { + return this.properties; + } } export class TableRow extends XmlComponent { From 6f0f3ccca737360fd466c7a5fc73a6c42aae704a Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 21 Aug 2018 21:46:12 +0100 Subject: [PATCH 18/19] Update documentation --- docs/usage/packers.md | 57 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/docs/usage/packers.md b/docs/usage/packers.md index db837702de..11f107390d 100644 --- a/docs/usage/packers.md +++ b/docs/usage/packers.md @@ -2,7 +2,48 @@ > Packers are the way in which `docx` turns your code into `.docx` format. It is completely decoupled from the `docx.Document`. -## File System Packer +## Version 4 + +Packers in `version 4` and above are now one single `Packer`. It works in both a node and browser environment (Angular etc). Now, the packer returns a `Buffer`, `Blob` or `base64 string`. It is up to you to take that and persist it with node's `fs`, send it down as a downloadable file, or anything else you wish. As of version 4, this library will not have options to export to PDF. + +### Export as Buffer + +This will return a NodeJS `Buffer`. If this is used in the browser, it will return a `UInt8Array` instead. + +```js +const packer = new docx.Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); +``` + +### Export as a `base64` string + +```js +const packer = new docx.Packer(); + +packer.toBase64String(doc).then((string) => { + console.log(string); +}); +``` + +### Export as Blob + +This is useful if you want to send it as an downloadable in a browser environment. + +```js +const packer = new docx.Packer(); + +packer.toBlob(doc).then((blob) => { + // saveAs from FileSaver will download the file + saveAs(blob, "example.docx"); +}); +``` + +## Version 3 and below + +### File System Packer ```js const docx = require("docx"); @@ -13,7 +54,7 @@ exporter.pack("My Document"); // Word Document is in file system ``` -## Buffer Packer +### Buffer Packer ```js const docx = require("docx"); @@ -23,7 +64,7 @@ const exporter = new docx.BufferPacker(doc); const buffer = exporter.pack(); ``` -## Stream Packer +### Stream Packer Creates a `node` `Readable` stream @@ -35,7 +76,7 @@ const exporter = new docx.StreamPacker(doc); const stream = exporter.pack(); ``` -## Express Packer +### Express Packer The old express packer is now deprecated and may disappear soon, so you should upgrade. @@ -56,13 +97,13 @@ const exporter = new docx.StreamPacker(doc); const stream = exporter.pack(); // Express' response object -res.attachment('yourfile.xlsx'); +res.attachment("yourfile.xlsx"); stream.pipe(res); ``` where `res` is the response object obtained through the Express router. It is that simple. The file will begin downloading in the browser. -## PDF Exporting +### PDF Exporting You can export your word document as a PDF file like so: @@ -74,7 +115,3 @@ exporter.packPdf("My Document"); const exporter = new docx.ExpressPacker(doc, res); exporter.packPdf("My Document"); ``` - -## Browser based docx exporting - -It is on the bucket list. It has been requested by a few, and work is already on it From 6c2f335a318d6a71b15f2bfa8dd169ead50bde4b Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 21 Aug 2018 22:29:40 +0100 Subject: [PATCH 19/19] Add new slogan --- README.md | 2 +- docs/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 84b4a75e0b..7e25666ee0 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

- Easily generate .docx files with JS/TS. + Easily generate .docx files with JS/TS. Works for Node and on the Browser.

--- diff --git a/docs/README.md b/docs/README.md index 557bcbad65..f3134b8fa3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,7 +3,7 @@

- Easily generate .docx files with JS/TS. :100: + Easily generate .docx files with JS/TS. Works for Node and on the Browser. :100:

---