Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a text box inside a for-loop #296

Closed
SuchiraD opened this issue Feb 20, 2023 · 6 comments
Closed

Adding a text box inside a for-loop #296

SuchiraD opened this issue Feb 20, 2023 · 6 comments

Comments

@SuchiraD
Copy link
Contributor

Hi,

I have tried to put a text-box inside a loop. But after the conversion, I am receiving following "corrupted file" error.
image
Once clicked yes, the document recovered as expected.

Following is a simple reproducible example.
DOCX-Template - ISSUE - TextBox in for loop.docx

JS code snippet

const createReport = require('docx-templates').default;
const fs = require('fs')

const template = fs.readFileSync('ISSUE - TextBox in for loop.docx');

const data = {
    items: [
        {
            name: 'Item 1'
        },
        {
            name: 'Item 2'
        },
        {
            name: 'Item 3'
        },
    ]
}

createReport({
    template,
    data,
    // cmdDelimiter: ['{{', '}}'],
}).then(
    rendered => fs.writeFileSync(
        'ISSUE-converted.docx',
        rendered
    ))
    .catch(console.log);

Following is the expected result.
ISSUE-converted-expected-result.docx

Runtime environment - Node v16.19.0

@jjhbw jjhbw added the triage label Mar 3, 2023
@jjhbw
Copy link
Collaborator

jjhbw commented Mar 3, 2023

Sorry for the late reply.

Hmm that's weird, but not totally unexpected as there is a lot going on in the XML when you add a text box.

Can you try with https://github.com/guigrpa/docx-templates/releases/tag/v4.9.3-beta.2 ?

It contains an experimental change to the way the XML is structured when using FOR loops. This release is not on NPM, but you can add it to your package.json by referring to the commit or tag.

@SuchiraD
Copy link
Contributor Author

SuchiraD commented Mar 3, 2023

@jjhbw Thanks for looking into this and the given suggestion. I have tried this beta version and the latest version 4.11. Still the issue is not fixed in both.
BTW i have extracted the docx file and saw the following differences in document.xml
this is a part of shape related xml in template docx.

<v:shape id="Text Box 1" o:spid="_x0000_s1026" type="#_x0000_t202" style="position:absolute;margin-left:.6pt;margin-top:2.6pt;width:171.7pt;height:36.3pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:top" 
							o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF&#xD;&#xA;90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZGx5TGhvj5O2G0SRWNoz/78nu9wcxkFMGNg6quQqL6RA&#xD;&#xA;0s5Y6ir5vt9lD1JwBDIwOMJKHpHlpr69KfdHjyxSmriSfYz+USnWPY7AufNIadK6MEJMx9ApD/oD&#xD;&#xA;OlTrorhX2lFEilmcO2RdNtjC5xDF9pCuTyYBB5bi6bQ4syoJ3g9WQ0ymaiLzg5KdCXlKLjvcW893&#xD;&#xA;SUOqXwnz5DrgnHtJTxOsQfEKIT7DmDSUCaxw7Rqn8787ZsmRM9e2VmPeBN4uqYvTtW7jvijg9N/y&#xD;&#xA;JsXecLq0q+WD6m8AAAD//wMAUEsDBBQABgAIAAAAIQA4/SH/1gAAAJQBAAALAAAAX3JlbHMvLnJl&#xD;&#xA;bHOkkMFqwzAMhu+DvYPRfXGawxijTi+j0GvpHsDYimMaW0Yy2fr2M4PBMnrbUb/Q94l/f/hMi1qR&#xD;&#xA;JVI2sOt6UJgd+ZiDgffL8ekFlFSbvV0oo4EbChzGx4f9GRdb25HMsYhqlCwG5lrLq9biZkxWOiqY&#xD;&#xA;22YiTra2kYMu1l1tQD30/bPm3wwYN0x18gb45AdQl1tp5j/sFB2T0FQ7R0nTNEV3j6o9feQzro1i&#xD;&#xA;OWA14Fm+Q8a1a8+Bvu/d/dMb2JY5uiPbhG/ktn4cqGU/er3pcvwCAAD//wMAUEsDBBQABgAIAAAA&#xD;&#xA;IQBmnp8nOAIAAHwEAAAOAA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6D4QKvjGBJvm/KzWlQknesNxl61g&#xD;&#xA;EUqVVtDlPFRSprZDr9MsDEjMHUL0OjOMVpqoTyzuezkviqX02hE7dHrAxw7br93RK9g+2Dvbljp2&#xD;&#xA;29I4N06fh1f7otTlxfS05nG/BpFxyn8f8NuB80PDwfbhSCaJnvGcDxXc8mL2ZrFYgtgrWK1KkE0t&#xD;&#xA;/3dofgAAAP//AwBQSwECLQAUAAYACAAAACEAtoM4kv4AAADhAQAAEwAAAAAAAAAAAAAAAAAAAAAA&#xD;&#xA;W0NvbnRlbnRfVHlwZXNdLnhtbFBLAQItABQABgAIAAAAIQA4/SH/1gAAAJQBAAALAAAAAAAAAAAA&#xD;&#xA;AAAAAC8BAABfcmVscy8ucmVsc1BLAQItABQABgAIAAAAIQBmnp8nOAIAAHwEAAAOAAAAAAAAAAAA&#xD;&#xA;AAAAAC4CAABkcnMvZTJvRG9jLnhtbFBLAQItABQABgAIAAAAIQBngL1v3QAAAAsBAAAPAAAAAAAA&#xD;&#xA;AAAAAAAAAJIEAABkcnMvZG93bnJldi54bWxQSwUGAAAAAAQABADzAAAAnAUAAAAA&#xD;&#xA;" fillcolor="white [3201]" strokeweight=".5pt">

This is the shape related XML in converted docx.

<v:shape id="Text Box 1" o:spid="_x0000_s1026" type="#_x0000_t202"
                style="position:absolute;margin-left:.6pt;margin-top:2.6pt;width:171.7pt;height:36.3pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:top"
                o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF
90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZGx5TGhvj5O2G0SRWNoz/78nu9wcxkFMGNg6quQqL6R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A
"
                fillcolor="white [3201]" strokeweight=".5pt">

With this, I think the issue arises from the new-line syntax conversion on &#xD;&#xA;

Do you have any idea on this?

@SuchiraD
Copy link
Contributor Author

SuchiraD commented Mar 4, 2023

ohh silly me.. No. Its not due to new line syntax change. The reason is an ID duplication on shape related XML

Inside <w:drawing> there is a <wp:docPr id="1" name="Text Box 1"/> tag and also, shape starts with <v:shape id="Text Box 1" .......>. In a loop, these two IDs should be changed according to the iteration. But on conversion, these were duplicated as it is. This was the cause for the error.

@jjhbw
Copy link
Collaborator

jjhbw commented Mar 6, 2023

Nice find!

This is not an easy fix, though, as it would probably require scanning the entire XML tree that has been generated for ID conflicts like this... Or do you think there is another solution?

@SuchiraD
Copy link
Contributor Author

Rather scanning the whole XML, isn't it possible to make the change while processing the for loop syntax?
I am not sure how the looping process happen in the library. But assuming while scanning the initial template's xml, if the scanner found a looping condition, then later it will repeat the content inside the loop. At this moment, while duplicating xml, can't we change the ID in each iteration?

SuchiraD added a commit to SuchiraD/docx-templates that referenced this issue Jul 8, 2023
SuchiraD added a commit to SuchiraD/docx-templates that referenced this issue Jul 8, 2023
@jjhbw
Copy link
Collaborator

jjhbw commented Jul 14, 2023

Fixed by #315

@jjhbw jjhbw closed this as completed Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants