-
Notifications
You must be signed in to change notification settings - Fork 350
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
Insert New line character (\n) in word document #144
Comments
To see how to insert new line, look at #26 |
None of these apply, I get the document but the paragraphs become one long string. It worked on an earlier version of my code so |
Your question is very unclear. What exactly doesn't work ? Can you give a script with it's template, output and your expected output ? |
I have string values that have '\n' breaks. When the string is generated in the .docx it becomes one long string with no breaks. I tried replacing the '\n' with '<w:br/>' and placing '<w:p><w:r><w:t>' in the beginng of the paragraph and '</w:t></w:r></w:p>' at the end. This did not work either, still one long string. My template has: My source: |
It's actually expected to work this way, \n is not replaced by a new line, everything is escaped by default (it is also the case for things like Here's the solution to this : Found here: #92 (comment)
|
This works on single variable like {@test} but I am using a loop. How would I use the @ symbol in my loop on the template? |
|
Where you have as json:
|
Thank you. This works. |
@edi9999 I followed the solution you give for replacing the I'm writing an app that will parse user created templates, so I don't know at build time what the inputs will be. When I have multiple fields such as:
I get this error:
What I'm ideally looking for is an easy way to let the user add new lines from any field at any time. Is that possible? Do I just need to tell users to put fields with \n on their own paragraph? Thanks and docxtemplater is a great. |
When your template contains
It means that the paragraph is :
The {@user} means replace the whole paragraph, so this means everything else before or after the tag in that paragraph will be wiped. I prefer to give a real error than just deleting the text silently. One solution to that is to use the paid word run module, which can replace only the tag that you use : https://modules.docxtemplater.com/modules/word-run/ Or you can also build your paragraph content yourself, eg add "User is" to the user data. |
It seems like changing
Does what I need. I know nothing about the docx .xml format, but this worked in my simple test cases. But I can't help but imagine you didn't do this for a reason. Is there something obvious I'm missing. |
That's interesting indeed... However, it has a few problems : The first one is that it may work in some word versions, but the open xml spec says that Secondly, it will work for docx but not pptx Thirdly, this map is for decoding xml characters, this shouldn't be done here. Lastly this would be a breaking change because for now we just ignore |
I tried the pre/post/linebreak suggestion - but couldn't get it to work inside a table that is generated within a loop. So in the end I used : JS and in the template :
This solution seems obvious and simple - is there a gotcha that I am missing ? |
You can even simplify it a little bit with : JS and in the template :
|
For docxtemplater v4, we will automatically add newlines when the text contains |
You can do this starting from docxtemplater 3.8.0 :
https://docxtemplater.readthedocs.io/en/latest/configuration.html#linebreaks |
Hey @edi9999 How does the template look like? I have in my JSON:
And in my code:
And in template:
But it doesn't work. Output is: I saw https://docxtemplater.readthedocs.io/en/latest/configuration.html#linebreaks it here. |
That is strange, can you open a new issue and send me the template that you use ? |
Yes, I opened the issue I'll send you the template on your email: contact@javascript-ninja.fr |
Ok, thanks |
I logged the value of the string which shows the new line char but when the document generates in Word, the paragraphs are combined.
The text was updated successfully, but these errors were encountered: