docx-templates: New line character inside a variable does not work

Hello, I’m trying to replace a variable that contains newline character \n. But this line break is not rendered in the output file, it’s just ignored.

address.docx

const content = fs.readFileSync('/tmp/address.docx');

const wordBuffer = await createReport({
  template: content,
  cmdDelimiter: ['{{ ', ' }}'],
  data: { address: 'City\nCountry'},
  // processLineBreaks: true,
});

fs.writeFileSync('/tmp/test.docx', wordBuffer);

Expected output

City
Country

Actual output

CityCountry

I even tried to specify processLineBreaks: true (though it should be a default value) but without any change.

Thanks for help!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 17

Commits related to this issue

Most upvoted comments

I have merged #182 into master. You can now add set the processLineBreaksAsNewText option to true to use the new line break injection behaviour. If no issues arise, we can probably make this the default behaviour in 5.x as it seems to improve compatibility with LibreOffice and Google Docs.

I can’t push a new release to NPM now, because @guigrpa and I are working out some NPM permissions issues. Pulling the latest master commit into your package.json from GitHub is an alternative until the release is pushed.

I faced the same issue. LibreOffice completely ignored line breaks and Google drive lost some of the text. As a workaround I did this:

{{ FOR line in user.description.split("\n") }}
{{INS $line }}
{{ END-FOR line }}

With settings: cmdDelimiter: [‘{{’, ‘}}’] processLineBreaks: false