open-xml-templating / docxtemplater

Generate docx, pptx, and xlsx from templates (Word, Powerpoint and Excel documents), from Node.js or the browser. Demo: https://www.docxtemplater.com/demo. #docx #office #generator #templating #report #json #generate #generation #template #create #pptx #docx #xlsx #react #vuejs #angularjs #browser #typescript #image #html #table #chart
https://www.docxtemplater.com
Other
3.07k stars 350 forks source link

Broken template is generated #758

Closed boris-petrov closed 3 months ago

boris-petrov commented 3 months ago

Environment

How to reproduce my problem :

My template is the following : broken.zip

With the following js file :

const fs = require('fs');
const PizZip = require('pizzip');
const Docxtemplater = require('docxtemplater');

const content = fs.readFileSync("broken.docx", "binary");

const zip = new PizZip(content);
const options = {
    linebreaks: true,
    paragraphLoop: true,
    delimiters: { start: '{{', end: '}}' },
    nullGetter: function () {
        return '';
    },
};
const doc = new Docxtemplater(zip, options);

doc.render({ items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] });

const buf = doc.getZip().generate({type:"nodebuffer"});

fs.writeFileSync("output.docx",buf);

Check the resulting document. It doesn't have the "a", "b" and "c" static texts in the table. Also, the table doesn't have 10 rows with indexes shown. There's something strange going on. Please try it out.

edi9999 commented 3 months ago

I think you have to use : {{#items}} instead of {{#model.items}.

Also, in order to use expressions such as "model.items" or "$index + 1" you need to enable the angular expressions feature :

https://docxtemplater.com/docs/angular-parse/#setup

Like this :

const expressionParser = require("docxtemplater/expressions.js");

const options = {
    parser: expressionParser,
    linebreaks: true,
    paragraphLoop: true,
    delimiters: { start: '{{', end: '}}' },
    nullGetter: function () {
        return '';
    },
};
const doc = new Docxtemplater(zip, options);

doc.render({ items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] });
boris-petrov commented 3 months ago

@edi9999 right, thank you for the answer. Sorry, I've given a wrong reproduction. My use-case is a bit more complex than that and that's why I made a mistake. The real issue is as follows. Change the template to just {{#items}} as you mentioned (no model and no $index + 1). And the important part is to change the JavaScript file to have MORE than 10 items - so add 11 in the end of the array for example. That breaks. With 10 or less items it works fine. With 11 or more it breaks. Please check it out. That's the real issue.

edi9999 commented 3 months ago

Can you please make sure to make a reproduction ?

This issue seems very strange and I would like if you could send me the template + the output with 8, 9, 10, and 15 items, in docx format.

boris-petrov commented 3 months ago

Indeed it's very strange, believe me, I spent a few hours today to narrow it down to this template and that data.

broken.zip

That's the right template. This is the code:

const fs = require('fs');
const PizZip = require('pizzip');
const Docxtemplater = require('docxtemplater');

const content = fs.readFileSync("/home/boris/broken.zip", "binary");

const zip = new PizZip(content);
const options = {
    linebreaks: true,
    paragraphLoop: true,
    delimiters: { start: '{{', end: '}}' },
    nullGetter: function () {
        return '';
    },
};
const doc = new Docxtemplater(zip, options);

doc.render({ items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] });

const buf = doc.getZip().generate({type:"nodebuffer"});

fs.writeFileSync("/home/boris/output.docx",buf);

Output with 11: output-11.zip Output with 10: output-10.zip

Note the missing a, b and c. In my real template most of the resulting file was missing. But I guess that's enough for you to debug the issue.

boris-petrov commented 3 months ago

Also, note that pretty much everything in this template is important - the image, the bold ., the two-line text below it... if any of these things is not exactly like that, it works fine. :smile:

edi9999 commented 3 months ago

What kind of viewer are you using ? libreoffice, wps, microsoft office on Windows ?

Usually, I try to use different viewers to see if the bug could come from the viewer itself (I've seen bug like that for libreoffice).

See the output here :

https://view.officeapps.live.com/op/view.aspx?src=https://docxtemplater.com/broken-docx_cptmp_file_tmp_ZdMZn3.docx

https://view.officeapps.live.com/op/view.aspx?src=https://docxtemplater.com/output-10-docx_cptmp_file_tmp_HJY7jS.docx

https://view.officeapps.live.com/op/view.aspx?src=https://docxtemplater.com/output-11-docx_cptmp_file_tmp_trmysU.docx

Output 10 and output 11 seem very similar (one line of difference).

boris-petrov commented 3 months ago

I see. Yes, I use LibreOffice, but I'll also check on MS Office. Indeed it seems like a bug in the viewer. Thanks for the support!

edi9999 commented 3 months ago

I have seen bugs like those and opened an issue once (on the libreoffice bug tracker), but I don't think it ever was fixed in libreoffice. The issue was also with tables that sometimes did not show correctly (completely hidden table in some rare cases).

I will try looking for that opened issue and send it here and also ping back libreoffice.

Thanks for your detailed issue.

edi9999 commented 3 months ago

The issue in libreoffice that I was referring to was : https://bugs.documentfoundation.org/show_bug.cgi?id=155793

But I think yours is a bit different, your issue is about tables changing the way they look, while the issue I was refering to was when you add images in a footer.

I think you could create a new bug on the https://bugs.documentfoundation.org website

edi9999 commented 3 months ago

Please link the bug here in order to document the issue if you open it :)

boris-petrov commented 3 months ago

https://bugs.documentfoundation.org/show_bug.cgi?id=162537

Thanks for all the help! :)

edi9999 commented 3 months ago

Thanks :)