Refactor image to accept Buffer only

This commit is contained in:
Dolan
2018-08-12 23:07:31 +01:00
parent bc1132146f
commit 675192b86f
12 changed files with 35 additions and 66 deletions

View File

@ -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");

View File

@ -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)

View File

@ -6,8 +6,8 @@ var doc = new docx.Document();
const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lH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hT5LNUfAspZFiTwyC72KK7+DNg1SsRvCNp3gZXq2k4iEEXSHFJHgVXUlxejCCbTvFAHiXdIJiXxyCK7KJ5FHoMZGK9xBcwyg2QpdlVMxEUM2iyIMuXXZQNF+HswxMsSAAJRQjoE//eoqDCXBSTO6f1xd+O0iyNRY6jaWi1ALNYCocZROj4JdEikroVkjFk9DcStXxpdfCD2MoXodu4RUU9ptxxmXssOfxnvDVcxRTod9FxyhqLoAqis5aPhwTDp9spRgEH2Q6KLbYoKqlaKTm6Isp0C/sJMnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+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');

View File

@ -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");

View File

@ -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');

View File

@ -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

View File

@ -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,

View File

@ -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;

View File

@ -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",

View File

@ -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",

View File

@ -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;

View File

@ -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 = {