diff --git a/src/export/packer/compiler.ts b/src/export/packer/compiler.ts index 7a62dc0dff..d0b0172cde 100644 --- a/src/export/packer/compiler.ts +++ b/src/export/packer/compiler.ts @@ -46,6 +46,7 @@ export class Compiler { const xmlFooter = xml(this.formatter.format(this.file.Footer.Footer)); const xmlHeaderRelationships = xml(this.formatter.format(this.file.Header.Relationships)); const xmlFooterRelationships = xml(this.formatter.format(this.file.Footer.Relationships)); + const xmlContentTypes = xml(this.formatter.format(this.file.ContentTypes)); this.archive.append(xmlDocument, { name: "word/document.xml", @@ -83,6 +84,10 @@ export class Compiler { name: "word/_rels/footer1.xml.rels", }); + this.archive.append(xmlContentTypes, { + name: "[Content_Types].xml", + }); + for (const data of this.file.Media.array) { this.archive.append(data.stream, { name: `word/media/${data.fileName}`, diff --git a/src/file/content-types/content-types.ts b/src/file/content-types/content-types.ts index 2eca99b864..c7246be57c 100644 --- a/src/file/content-types/content-types.ts +++ b/src/file/content-types/content-types.ts @@ -1,5 +1,6 @@ import { XmlComponent } from "file/xml-components"; import { ContentTypeAttributes } from "./content-types-attributes"; +import { Default } from "./default/default"; export class ContentTypes extends XmlComponent { constructor() { @@ -10,5 +11,22 @@ export class ContentTypes extends XmlComponent { xmlns: "http://schemas.openxmlformats.org/package/2006/content-types", }), ); + + this.root.push(new Default("image/png", "png")); + this.root.push(new Default("image/jpeg", "jpeg")); + this.root.push(new Default("image/jpeg", "jpg")); + this.root.push(new Default("image/bmp", "bmp")); + this.root.push(new Default("image/gif", "gif")); + this.root.push(new Default("application/vnd.openxmlformats-package.relationships+xml", "rels")); + this.root.push(new Default("application/xml", "xml")); + + this.root.push(new Default("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", undefined, "/word/document.xml")); + this.root.push(new Default("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", undefined, "/word/header1.xml")); + this.root.push(new Default("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", undefined, "/word/footer1.xml")); + this.root.push(new Default("application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", undefined, "/word/styles.xml")); + this.root.push(new Default("application/vnd.openxmlformats-package.core-properties+xml", undefined, "/docProps/core.xml")); + this.root.push(new Default("application/vnd.openxmlformats-officedocument.extended-properties+xml", undefined, "/docProps/app.xml")); + this.root.push(new Default("application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", undefined, "/word/numbering.xml")); + } } diff --git a/src/file/content-types/default/default-attributes.ts b/src/file/content-types/default/default-attributes.ts new file mode 100644 index 0000000000..5ed4c40936 --- /dev/null +++ b/src/file/content-types/default/default-attributes.ts @@ -0,0 +1,15 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IDefaultAttributes { + contentType: string; + extension?: string; + partName?: string; +} + +export class DefaultAttributes extends XmlAttributeComponent { + protected xmlKeys = { + contentType: "ContentType", + extension: "Extension", + partName: "PartName", + }; +} diff --git a/src/file/content-types/default/default.ts b/src/file/content-types/default/default.ts index 1897e85e93..17fb595784 100644 --- a/src/file/content-types/default/default.ts +++ b/src/file/content-types/default/default.ts @@ -1,9 +1,16 @@ import { XmlComponent } from "file/xml-components"; +import { DefaultAttributes } from "./default-attributes"; export class Default extends XmlComponent { - constructor() { - super("Types"); + constructor(contentType: string, extension?: string, partName?: string) { + super("Default"); - this.root.push(); + this.root.push( + new DefaultAttributes({ + contentType: contentType, + extension: extension, + partName: partName, + }), + ); } } diff --git a/src/file/file.ts b/src/file/file.ts index b761874ab7..172424717c 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -1,3 +1,4 @@ +import { ContentTypes } from "./content-types/content-types"; import { Document } from "./document"; import { SectionPropertiesOptions } from "./document/body/section-properties/section-properties"; import { FooterWrapper } from "./footer-wrapper"; @@ -20,6 +21,7 @@ export class File { private readonly relationships: Relationships; private readonly headerWrapper: HeaderWrapper; private readonly footerWrapper: FooterWrapper; + private readonly contentTypes: ContentTypes; constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) { this.document = new Document(sectionPropertiesOptions); @@ -40,6 +42,7 @@ export class File { this.media = new Media(); this.headerWrapper = new HeaderWrapper(this.media); this.footerWrapper = new FooterWrapper(this.media); + this.contentTypes = new ContentTypes(); } public addParagraph(paragraph: Paragraph): void { @@ -99,4 +102,8 @@ export class File { public get Footer(): FooterWrapper { return this.footerWrapper; } + + public get ContentTypes(): ContentTypes { + return this.contentTypes; + } }