docx icon indicating copy to clipboard operation
docx copied to clipboard

30-template-document.ts doesn't work

Open kiyoshih opened this issue 5 years ago • 8 comments

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.

file-info-properties

kiyoshih avatar Jan 11 '20 02:01 kiyoshih

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

dolanmiu avatar Jan 11 '20 04:01 dolanmiu

Understood. Thank you for this amazing works!

kiyoshih avatar Jan 17 '20 01:01 kiyoshih

@dolanmiu : Thanks for your quick reply on the issue raised. Any ideas on what the timeline could be for the fix?

adityamantha avatar Jan 29 '20 02:01 adityamantha

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.

rickgrana avatar Apr 13 '20 23:04 rickgrana

waiting for this feature.

leohxj avatar Jun 09 '20 09:06 leohxj

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.

lordpomeroy avatar Oct 17 '20 11:10 lordpomeroy

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!

dyllandry avatar Oct 23 '20 13:10 dyllandry

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:

  1. 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.
  2. 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 the template option) and if there is one, just take it. Would look like this (the else branches of ternaries are just undefined 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)

aturok avatar Sep 12 '22 09:09 aturok

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.

dolanmiu avatar Mar 19 '23 03:03 dolanmiu