diff --git a/demo/demo23.ts b/demo/demo23.ts index 47ee26a6f9..2e230e0cb7 100644 --- a/demo/demo23.ts +++ b/demo/demo23.ts @@ -8,11 +8,11 @@ const doc = new Document(); const paragraph = new Paragraph("Hello World"); doc.addParagraph(paragraph); -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 image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); +const image2 = Media.addImage(doc, fs.readFileSync("./demo/images/dog.png")); +const image3 = Media.addImage(doc, fs.readFileSync("./demo/images/cat.jpg")); +const image4 = Media.addImage(doc, fs.readFileSync("./demo/images/parrots.bmp")); +const image5 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif")); const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx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plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+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); diff --git a/demo/demo24.ts b/demo/demo24.ts index 23828aec65..4901477ced 100644 --- a/demo/demo24.ts +++ b/demo/demo24.ts @@ -8,7 +8,7 @@ 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"); +const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); table.getCell(1, 1).addContent(image.Paragraph); const packer = new Packer(); diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts index 9ab0621ad9..8e422f425c 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -3,12 +3,15 @@ import { expect } from "chai"; import { Formatter } from "../../../../export/formatter"; import { FooterWrapper } from "../../../footer-wrapper"; import { HeaderWrapper } from "../../../header-wrapper"; +import { Media } from "../../../media"; import { PageBorderOffsetFrom, PageNumberFormat } from "./"; import { SectionProperties } from "./section-properties"; describe("SectionProperties", () => { describe("#constructor()", () => { it("should create section properties with options", () => { + const media = new Media(); + const properties = new SectionProperties({ width: 11906, height: 16838, @@ -23,10 +26,10 @@ describe("SectionProperties", () => { space: 708, linePitch: 360, headers: { - default: new HeaderWrapper(100), + default: new HeaderWrapper(media, 100), }, footers: { - even: new FooterWrapper(200), + even: new FooterWrapper(media, 200), }, pageNumberStart: 10, pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT, diff --git a/src/file/file.ts b/src/file/file.ts index fd2b5746b7..e18b536490 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -173,13 +173,13 @@ export class File { } public createHeader(): HeaderWrapper { - const header = new HeaderWrapper(this.currentRelationshipId++); + const header = new HeaderWrapper(this.media, this.currentRelationshipId++); this.addHeaderToDocument(header); return header; } public createFooter(): FooterWrapper { - const footer = new FooterWrapper(this.currentRelationshipId++); + const footer = new FooterWrapper(this.media, this.currentRelationshipId++); this.addFooterToDocument(footer); return footer; } diff --git a/src/file/footer-wrapper.ts b/src/file/footer-wrapper.ts index 785f2724e4..2c51cf5072 100644 --- a/src/file/footer-wrapper.ts +++ b/src/file/footer-wrapper.ts @@ -1,8 +1,8 @@ -import { IMediaData } from "file/media"; import { XmlComponent } from "file/xml-components"; + import { FooterReferenceType } from "./document"; import { Footer } from "./footer/footer"; -import { Image, Media } from "./media"; +import { Image, IMediaData, Media } from "./media"; import { ImageParagraph, Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; @@ -15,10 +15,8 @@ export interface IDocumentFooter { export class FooterWrapper { private readonly footer: Footer; private readonly relationships: Relationships; - private readonly media: Media; - constructor(referenceId: number, initContent?: XmlComponent) { - this.media = new Media(); + constructor(private readonly media: Media, referenceId: number, initContent?: XmlComponent) { this.footer = new Footer(referenceId, initContent); this.relationships = new Relationships(); } @@ -48,7 +46,7 @@ export class FooterWrapper { public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData { const mediaData = this.media.addMedia(image, refId, width, height); this.relationships.createRelationship( - refId, + mediaData.referenceId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", `media/${mediaData.fileName}`, ); @@ -64,8 +62,10 @@ export class FooterWrapper { ); } - public createImage(image: Buffer, width?: number, height?: number): void { - const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height); + public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void { + // TODO + // tslint:disable-next-line:no-any + const mediaData = this.addImageRelationship(image as any, this.relationships.RelationshipCount, width, height); this.addImage(new Image(new ImageParagraph(mediaData))); } diff --git a/src/file/header-wrapper.ts b/src/file/header-wrapper.ts index beb7903cf5..f4a403ce27 100644 --- a/src/file/header-wrapper.ts +++ b/src/file/header-wrapper.ts @@ -1,8 +1,8 @@ -import { IMediaData } from "file/media"; import { XmlComponent } from "file/xml-components"; + import { HeaderReferenceType } from "./document"; import { Header } from "./header/header"; -import { Image, Media } from "./media"; +import { Image, IMediaData, Media } from "./media"; import { ImageParagraph, Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; @@ -15,10 +15,8 @@ export interface IDocumentHeader { export class HeaderWrapper { private readonly header: Header; private readonly relationships: Relationships; - private readonly media: Media; - constructor(referenceId: number, initContent?: XmlComponent) { - this.media = new Media(); + constructor(private readonly media: Media, referenceId: number, initContent?: XmlComponent) { this.header = new Header(referenceId, initContent); this.relationships = new Relationships(); } @@ -48,7 +46,7 @@ export class HeaderWrapper { public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData { const mediaData = this.media.addMedia(image, refId, width, height); this.relationships.createRelationship( - refId, + mediaData.referenceId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", `media/${mediaData.fileName}`, ); @@ -64,8 +62,10 @@ export class HeaderWrapper { ); } - public createImage(image: Buffer, width?: number, height?: number): void { - const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height); + public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void { + // TODO + // tslint:disable-next-line:no-any + const mediaData = this.addImageRelationship(image as any, this.relationships.RelationshipCount, width, height); this.addImage(new Image(new ImageParagraph(mediaData))); } diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 284ca8806d..fdeb21b931 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -87,7 +87,7 @@ export class Media { } const imageData = { - referenceId: this.map.size + relationshipsCount + 1, + referenceId: relationshipsCount, stream: data, path: filePath, fileName: key, diff --git a/src/import-dotx/import-dotx.ts b/src/import-dotx/import-dotx.ts index 4dbe01be56..0b368099ac 100644 --- a/src/import-dotx/import-dotx.ts +++ b/src/import-dotx/import-dotx.ts @@ -5,10 +5,10 @@ import { FooterReferenceType } from "file/document/body/section-properties/foote import { HeaderReferenceType } from "file/document/body/section-properties/header-reference"; import { FooterWrapper, IDocumentFooter } from "file/footer-wrapper"; import { HeaderWrapper, IDocumentHeader } from "file/header-wrapper"; -import { convertToXmlComponent, ImportedXmlComponent, parseOptions } from "file/xml-components"; - +import { Media } from "file/Media"; import { Styles } from "file/styles"; import { ExternalStylesFactory } from "file/styles/external-styles-factory"; +import { convertToXmlComponent, ImportedXmlComponent, parseOptions } from "file/xml-components"; const importParseOptions = { ...parseOptions, @@ -65,37 +65,39 @@ export class ImportDotx { const relationshipContent = zipContent.files["word/_rels/document.xml.rels"]; const documentRelationships: IRelationshipFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text")); + const media = new Media(); + const headers: IDocumentHeader[] = []; for (const headerRef of documentRefs.headers) { const headerKey = "w:hdr"; - const relationFileInfo = documentRelationships.find((rel) => rel.id === headerRef.id); - if (relationFileInfo === null || !relationFileInfo) { + const relationshipFileInfo = documentRelationships.find((rel) => rel.id === headerRef.id); + if (!relationshipFileInfo) { throw new Error(`Can not find target file for id ${headerRef.id}`); } - const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text"); + const xmlData = await zipContent.files[`word/${relationshipFileInfo.target}`].async("text"); const xmlObj = fastXmlParser.parse(xmlData, importParseOptions); const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent; - const header = new HeaderWrapper(this.currentRelationshipId++, importedComp); - await this.addRelationToWrapper(relationFileInfo, zipContent, header); + const header = new HeaderWrapper(media, this.currentRelationshipId++, importedComp); + await this.addRelationToWrapper(relationshipFileInfo, zipContent, header); headers.push({ type: headerRef.type, header }); } const footers: IDocumentFooter[] = []; for (const footerRef of documentRefs.footers) { const footerKey = "w:ftr"; - const relationFileInfo = documentRelationships.find((rel) => rel.id === footerRef.id); - if (relationFileInfo === null || !relationFileInfo) { + const relationshipFileInfo = documentRelationships.find((rel) => rel.id === footerRef.id); + if (!relationshipFileInfo) { throw new Error(`Can not find target file for id ${footerRef.id}`); } - const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text"); + const xmlData = await zipContent.files[`word/${relationshipFileInfo.target}`].async("text"); const xmlObj = fastXmlParser.parse(xmlData, importParseOptions); const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent; - const footer = new FooterWrapper(this.currentRelationshipId++, importedComp); - await this.addRelationToWrapper(relationFileInfo, zipContent, footer); + const footer = new FooterWrapper(media, this.currentRelationshipId++, importedComp); + await this.addRelationToWrapper(relationshipFileInfo, zipContent, footer); footers.push({ type: footerRef.type, footer }); } @@ -110,13 +112,13 @@ export class ImportDotx { } public async addRelationToWrapper( - relationFile: IRelationshipFileInfo, + relationhipFile: IRelationshipFileInfo, zipContent: JSZip, wrapper: HeaderWrapper | FooterWrapper, ): Promise { let wrapperImagesReferences: IRelationshipFileInfo[] = []; let hyperLinkReferences: IRelationshipFileInfo[] = []; - const refFile = zipContent.files[`word/_rels/${relationFile.target}.rels`]; + const refFile = zipContent.files[`word/_rels/${relationhipFile.target}.rels`]; if (refFile) { const xmlRef = await refFile.async("text"); wrapperImagesReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "image"); @@ -133,10 +135,10 @@ export class ImportDotx { public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] { const xmlObj = fastXmlParser.parse(xmlData, importParseOptions); - const relationXmlArray = Array.isArray(xmlObj.Relationships.Relationship) + const relationshipXmlArray = Array.isArray(xmlObj.Relationships.Relationship) ? xmlObj.Relationships.Relationship : [xmlObj.Relationships.Relationship]; - const relations: IRelationshipFileInfo[] = relationXmlArray + const relationships: IRelationshipFileInfo[] = relationshipXmlArray .map((item) => { return { id: this.parseRefId(item._attr.Id), @@ -145,7 +147,7 @@ export class ImportDotx { }; }) .filter((item) => item.type !== null); - return relations; + return relationships; } public extractDocumentRefs(xmlData: string): IDocumentRefs {