docx
docx copied to clipboard
30-template-document.ts doesn't work
The created My Document.docx
does not use template.dotx.
It has no cover page, no header or footer.
related #361
I run this demo by npm script demo.
This piece of work wasn't done by me and there were no tests for it, so it was very hard to deal with
I am thinking of re-factoring it all out and write tests
Understood. Thank you for this amazing works!
@dolanmiu : Thanks for your quick reply on the issue raised. Any ideas on what the timeline could be for the fix?
I am trying this too, and can't make it to work.
I don't know the problem, by debugging, the headers and footers are there in the data, but are not shown.
waiting for this feature.
Is somebody already working on this? I would create a pull request and take a shot at this. But it would be my first contribution to this project so bear with me regarding any simple mistakes regarding conventions etc.
Is somebody already working on this? I would create a pull request and take a shot at this. But it would be my first contribution to this project so bear with me regarding any simple mistakes regarding conventions etc.
I'd say go for it @lordpomeroy ! I've had coworkers who have talked about the difficulty of using docx templates provided by our higher-ups which we need to dynamically insert images into. If you helped to get this working, I could make an argument for using this package! I'd be grateful!
Hi, I've run into the problem with headers and footers from template not being utilized in the documents. Apparently the problem is that when a document section is created it won't use the headers/footers that are created in the document based on the template (it stopped working between 4.7.1 and 5.0.0).
I see that the code related to dotx utilization has to be reworked, but probably before the huge job is done it would be worthwhile to just make a (not-very-)hotfix to make the headers/footers work as is?
From the little investigation that I performed I see two straightforward approaches:
- The one Coridyn took - seen in the above two commits, where one has on option of explicitly pulling headers/footers from the imported template document into a section. I neither tested nor explored it deeply, but the general approach of "let us allow to peek into an imported document and take stuff from there" looks appealing and extendable. Could be event expanded to work with other bits of template documents.
- A dirtier, but simpler one that changes the section creation code (
File.addSection
): if no header/footer of a particular type is explicitly specified for the section, take a look at the document-level headers/footers (these are populated from thetemplate
option) and if there is one, just take it. Would look like this (theelse
branches of ternaries are justundefined
at the moment):
private addSection({ headers = {}, footers = {}, children, properties }: ISectionOptions): void {
const findByType = <ItemType extends { readonly type: string }>(where: ItemType[], type: string) => {
return where.find((item) => item.type === type);
};
this.documentWrapper.View.Body.addSection({
...properties,
headerWrapperGroup: {
default: headers.default ? this.createHeader(headers.default) : findByType(this.headers, "default")?.header,
first: headers.first ? this.createHeader(headers.first) : findByType(this.headers, "first")?.header,
even: headers.even ? this.createHeader(headers.even) : findByType(this.headers, "event")?.header,
},
footerWrapperGroup: {
// similar stuff here
},
});
// ...
}
However, it looks like a breaking change, so probably deserves an option in the section options to enable the behavior explicitly.
What would you say? (Personally, I can live with my own hotfix in a fork, but if there is value to fixing it the second way in the lib itself, I could easily create a pull request)
docx now has this feature revamped! Any .dotx
and .docx
files can be editable as of v8.0.0:
https://github.com/dolanmiu/docx/blob/master/demo/85-template-document.ts
This library can now read and write docx files.