Docx: Breaks include undesired spaces

Created on 3 Dec 2018  路  6Comments  路  Source: dolanmiu/docx

paragraph.createTextRun().break(); is adding a bunch of spaces on the line of the break.

_I think_ it might be because a linebreak is added to the document.xml like this:

                <w:br>
                </w:br>

and should be like this:

                <w:br></w:br>

or

                <w:br />
bug

Most helpful comment

Never mind. I was using paragraph.createTextRun().break() which is what was causing this issue. paragraph.addRun(new Run().break()) is the correct way to add breaks in a paragraph.

All 6 comments

It appears that the xml module has a bug where it does not handle empty arrays correctly. It turns an empty array into an array with 2 empty strings, so { "w:br": [] } turns into { "name": "w:br", content: ["", ""] } which prevents it from coming out as <w:br/>.

A PR fix is here for xml is here https://github.com/dylang/node-xml/pull/41. Not sure if it will be accepted as this project looks unmaintained.

The other options are to:

  1. set empty children to null instead of [] or
  2. let certain nodes specify they are empty, so Break could have this.isEmpty = true in the constructor and then prepForXml() can set null as the content when isEmpty is true.

I am willing to help with this if you let me know which solution you would prefer.

Never mind. I was using paragraph.createTextRun().break() which is what was causing this issue. paragraph.addRun(new Run().break()) is the correct way to add breaks in a paragraph.

馃憤

Both paragraph.createTextRun().break() and paragraph.addRun(new Run().break()) create two empty lines, one for the Paragraph and another for the Run

I simply have the following function, which I call after I've created the Document object

// Helper function creates the general Document layout
  function createPret4Document (doc, jbName, fmName, pdValue, pdText) {
    doc.createParagraph(null).addRun(new Run().break())// eslint-disable-line
    createTable(doc, jbName, fmName, pdValue, pdText)
  }

Any suggestions?

I'll need to look into this

Thank you, @dolanmiu.

For now, I have solved the problem with the following line:

doc.createParagraph(null).addRun(new Run(null).tab())

Was this page helpful?
0 / 5 - 0 ratings