Insert New line character (\n) in word document
I logged the value of the string which shows the new line char but when the document generates in Word, the paragraphs are combined.
To see how to insert new line, look at #26
There's also #92 #73 and #56
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: {#exceptions} {num}. {exception} {/exceptions}
My source: var docx = require('docxtemplater'); var worddoc = { docpath: 'path to folder', docname: 'name.docx' }; var docs = { createDoc: function(req, res) { var exceptions = []; var doc = new docx(worddoc.docpath); doc.setData = { exceptions: req.body.exceptions } doc.render(); var buf = doc.getZip().generate({ type: 'nodebuffer' }); try { fs.writeFileSync(worddoc.docpath + worddoc.docname, buf); } catch(exception) { console.log(exception); } } The string value is "this is one line.\nThis is another." It comes through as one line.
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 < , > and others)
Here's the solution to this : Found here: https://github.com/open-xml-templating/docxtemplater/issues/92#issuecomment-61423460
Thank you, got it working, for anyone else struggling with this here is what I did:
pre = '<w:p><w:r><w:t>'; post = '</w:t></w:r></w:p>'; lineBreak = '<w:br/>'; test = pre + 'testing line 1' + lineBreak + 'testing line 2' + post;
then in your template, just put {@test} instead of the usual {test}
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?
{#exceptions}
{@exception}
{/exceptions}
Where
you have as json:
pre = '<w:p><w:r><w:t>';
post = '</w:t></w:r></w:p>';
lineBreak = '<w:br/>';
{exceptions:[
{exception:pre + 'testing line 1' + lineBreak + 'testing line 2' + post},
{exception:pre + 'testing line 3' + lineBreak + 'testing line 4' + post},
]}
Thank you. This works.
@edi9999 I followed the solution you give for replacing the \n with <w:br/> and using {@field_name}
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:
{#page}
{@address}
{@name} Total Owed: ${@total}
{@line_items}
{/page}
I get this error:
[TemplateError: Raw tag should be the only text in paragraph]
name: 'TemplateError',
message: 'Raw tag should be the only text in paragraph',
stack: 'Error: Raw tag should be the only text in paragraph
at Error.XTTemplateError....
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
User Is {@user}
It means that the paragraph is :
<w:p>User Is {@user}</w:p>
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 DocUtils.charMap to
DocUtils.charMap = {
"&": "&",
"'": "'",
"<": "<",
">": ">",
"\n":"<w:br/>"
};
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 <w:br/> must be in the <w:r>, but not in the <w:t>.
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 \n and this could be adding unwanted new lines.
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
obj.lines = inputText.split('\n').map(function (lineItem) { return : { line : lineItem} } );
and in the template :
{#lines}{line}
{/lines}
This solution seems obvious and simple - is there a gotcha that I am missing ?
You can even simplify it a little bit with :
JS
obj.lines = inputText.split('\n')
and in the template :
{#lines}{.}
{/lines}
For docxtemplater v4, we will automatically add newlines when the text contains \n (see https://github.com/open-xml-templating/docxtemplater/issues/340 point 8.)
You can do this starting from docxtemplater 3.8.0 :
doc.setOptions({linebreaks:true});
doc.setData({text: "My text,\nmultiline"});
doc.render();
https://docxtemplater.readthedocs.io/en/latest/configuration.html#linebreaks
Hey @edi9999
How does the template look like?
I have in my JSON:
{
"description": "This is first line.\n This is second line"
}
And in my code:
// Set options
doc.setOptions({linebreaks: true});
doc.setData({
description: employee.description + ',\nmultiline'
});
And in template:
{description}
But it doesn't work. Output is:
This is first line. This is second line, multiline
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 ?