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

New line character inside a variable does not work #143

Closed
lattam opened this issue Jul 29, 2020 · 17 comments
Closed

New line character inside a variable does not work #143

lattam opened this issue Jul 29, 2020 · 17 comments
Labels

Comments

@lattam
Copy link

lattam commented Jul 29, 2020

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!

@jjhbw
Copy link
Collaborator

jjhbw commented Aug 5, 2020

@lattam for your report! Neat repro case as well.

Weirdly, I wasn't able to reproduce this (see commit 4743cba). The rendered document turned out exactly as expected...

Which version of docx-templates and MS Word are you using?

@jjhbw
Copy link
Collaborator

jjhbw commented Aug 18, 2020

As I couldn't reproduce this problem, i'll close this issue for now. Feel free to reopen if it persists. Please make sure you're running a fully updated version of MS Word (or equivalent) and are using the latest version of the library.

@jjhbw jjhbw closed this as completed Aug 18, 2020
@nickdunn
Copy link

nickdunn commented Sep 18, 2020

I am also seeing this same issue.

"docx-templates": "^4.5.0",

cv-debug-clean.docx

(async() => {
  const fs = require('fs');
  const { createReport } = require('docx-templates');
  const template = fs.readFileSync('./cv-debug-clean.docx');

  const headline = "I am a line\n\nAnd so am I!";

  const buffer = await createReport({
    template,
    data: {
      headline: headline,
    },
    processLineBreaks: true
  });

  fs.writeFileSync('cv.docx', buffer);

})();

Word for Mac version 16.41

Screenshot 2020-09-18 at 16 39 40

@nickdunn
Copy link

When I upload the doc to Office 365 online and view in Word I see the same thing:

Screenshot 2020-09-18 at 16 45 36

When I upload to Google Docs I only see the second line:

Screenshot 2020-09-18 at 16 44 07

And when I use a free doc viewer online it shows the correct lines:

Screenshot 2020-09-18 at 16 44 32

@nickdunn
Copy link

I have tried with v4.0.0 and v4.5.0, same result.

@jjhbw jjhbw reopened this Sep 21, 2020
jjhbw added a commit that referenced this issue Sep 21, 2020
@jjhbw
Copy link
Collaborator

jjhbw commented Sep 21, 2020

That is super weird. I get the below result when running a test case based on your example (see 153afe7).

The resulting Word file seems correct... Maybe its because i'm using MS Word for Mac 16.16.26 instead of 16.41 or something, or maybe our runtime envs are different. Which compiler toolchain / runtime environment are you using? (i.e. angular, webpack, browserify, which version, etc.).

I'll flag this as a bug for now because even if the issue is caused by something outside of the library we'll probably want to prevent it somehow.

Screenshot 2020-09-21 at 16 31 12

@jjhbw jjhbw added the bug label Sep 21, 2020
@nickdunn
Copy link

Really weird, today this is no longer happening and line breaks are coming through as expected. No change to package, Node or Word versions. Same test case as above. Genuinely no idea what happened — but I can't recreate it!

For reference I'm using this in Node directly (v12.14.1).

Same result (working) both writing to a file and returning via a buffer from an Express endpoint (res.end(new Buffer(buffer));).

I think maybe this can be closed again and I'll re-open if I hit the issue again and can narrow it down further.

Sorry to have potentially raised a false alarm.

@jjhbw
Copy link
Collaborator

jjhbw commented Sep 22, 2020

Hmmm super strange. No i dont think it was a false alarm. I get the same view as you when opening in Google drive. To me it seems like some viewers interpret the linebreak differently or not at all...

I'll keep it open for a while so i can study it some more when i find the time.

@jjhbw
Copy link
Collaborator

jjhbw commented Sep 22, 2020

AFAIK our implementation is up to 'spec' on this front (http://officeopenxml.com/WPtextSpecialContent-break.php), and I can't imagine there's some non-determinism at play in this library...

My working theory is that it is caused by heterogeneity in the MS Word reader landscape. MS Word (versions?), google drive, etc. aren't created equal, regretfully...

@nickdunn if you find a new example, please let me know. Thanks for your report!

@grinat
Copy link

grinat commented Nov 6, 2020

@jjhbw i replaced <w:br/> with </w:t></w:r><w:r><w:br/><w:t xml:space="preserve"> in processTempalte and it works correct in LibreOffice and Ms office 2007, but breaks next line formatting
2020-11-06_13-32

With <w:br/> correct work only in ms office 2007.

@jvikstedt
Copy link

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

@jalik
Copy link

jalik commented Apr 6, 2021

As a complement, I noticed that \r are replaced by a space, and \n are replaced by nothing when opening in libre office 7.1.2.2.
@jjhbw would you mind checking if that solution from a similar PHP lib is working ? PHPOffice/PHPWord#838 (comment)

(edit:) The referenced issue proposes to fix the bug like this:

You need to replace \n with </w:t><w:br/><w:t> the replacement string is without the \n

@intenser
Copy link

intenser commented Apr 8, 2021

the same issue here. New lines don't work.
Using latest [email protected], macOS Big Sur 11.2.3, Node.js v15.12.0

@kracas
Copy link

kracas commented Aug 11, 2021

Fix for this issue is mentioned in #68

@fcpunk
Copy link

fcpunk commented Aug 26, 2021

Sadly it still doesn't work in LibreOffice 7.1.5.2 (Arch Linux) with latest [email protected], nodejs v16.5.0. LibreOffice's wrapper unoconv is widely used to convert docx to PDF automatically in docker containers and API wrapers like gotenberg.

jjhbw added a commit that referenced this issue Feb 23, 2023
jjhbw added a commit that referenced this issue Feb 23, 2023
jjhbw added a commit that referenced this issue Feb 23, 2023
…lt (i.e. processLineBreaksAsNewText is now always set to true).
@jjhbw
Copy link
Collaborator

jjhbw commented Feb 23, 2023

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.

@jjhbw jjhbw closed this as completed Feb 23, 2023
jjhbw added a commit that referenced this issue Mar 2, 2023
…lt (i.e. processLineBreaksAsNewText is now always set to true).
@jjhbw
Copy link
Collaborator

jjhbw commented Mar 2, 2023

The NPM permissions issue has been resolved. I just published v4.10.0 and v4.11.0 to npm. Thanks all for your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants