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

Implement line breaks #159

Closed
natcohen opened this issue Aug 11, 2015 · 8 comments
Closed

Implement line breaks #159

natcohen opened this issue Aug 11, 2015 · 8 comments
Labels

Comments

@natcohen
Copy link

I know, it's a frequent posted issue and I already found the solutions but I think we should be able to implement it so it becomes transparent to the user. Here is what I do to keep the format and add break lines:

{#multilines}{line}
{\multilines}

And in my JavaScript code, I break my text as follow:

var multilines = [];
for(var int = 0; int < text.split('\n').length; int++){
    multilines.push({'line': text.split('\n')[int]});
}

So my question is, if we can do it like that, would it be possible to use normal variable in the docx {text} and detect line breaks in the JavaScript library and implement it transparently so the user doesn't need to take care of this? The line break is so useful that I really think we should find a way to make it work by default...

What do you think?

@edi9999
Copy link
Member

edi9999 commented Aug 16, 2015

I have just added a section about this problem in the new FAQ : http://docxtemplater.readthedocs.org/en/latest/faq.html#how-to-insert-linebreaks

It permits you to write {@text|rawHtml} when text is 'line1\nline2'

What do you think ?

@natcohen
Copy link
Author

@edi9999 I don't really like this solution because it doesn't keep the formatting from the docx. Imagine you have different docx with different formatting and you want to add some text with line breaks... The solution I propose keeps the formatting.
I was just wondering if we can make it transparent to the user meaning that if the text has line breaks, we automatically change the docx from a normal {text} to what I proposed...is that possible?

@edi9999
Copy link
Member

edi9999 commented Aug 18, 2015

The problem is that it is not possible to do the transformation :

{text}
to:

{#multilines}{line}
{\multilines}

because {text} is an inline element. And

{#multilines}{line}
{\multilines} 

are inline elements separated by a block element.

I think it would be possible to keep the formatting when inserting linebreaks only if you implement some logic to copy the style from one paragraph to the other. However, I don't wish that to be in docxtemplater core. But I think it would be a great opportunity to create a module : https://github.com/open-xml-templating/docxtemplater#modules

@edi9999
Copy link
Member

edi9999 commented Aug 18, 2015

If the docx was html (which is similar to xml), it would like to be the following:

<p>
<span>Hello <b>{text}</b>
</span>
</p>
<p>
<span>Hello <b>{text}</b>
</span>
</p>
<p>
<span>Hello <b>{text}</b>
</span>
</p>

with text = "john \n mary \n henry" should be transformed into :

<p>
<span>Hello <b>John</b>
</span>
</p>
<p>
<span><b>Mary</b>
</span>
</p>
<p>
<span><b>Henry</b>
</span>
</p>

I think that shows you the complexity.

@araneta
Copy link

araneta commented Jul 18, 2018

i tried this http://docxtemplater.readthedocs.io/en/latest/faq.html#how-to-insert-linebreaks it works but the font family changed to times new roman instead of arial. and the text location is changed to the left side instead of on the right side.
what should i do?

@edi9999
Copy link
Member

edi9999 commented Jul 18, 2018

Can you open a new issue for this @araneta ?

@araneta
Copy link

araneta commented Jul 18, 2018

@edi9999 i opened new issue line break bug #407

@edi9999
Copy link
Member

edi9999 commented Jul 20, 2018

This is now fixed in version 3.8.0 with the option :

doc.setOptions({linebreaks: true});

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

3 participants