Search code examples
javascriptdocxtemplater

How to remove spaces between a string in Docxtemplater?


{#form}{importantFacts}{/form}

The problem is that the string takes over the whole line

For example :

1. This is test

2. This is a longer string so spacing is generated less

However if i give longer strings it works fine , Basically the behaviour is that it covers the whole line.. how to remove these whitepspaces

Please let me know if more code needs to be shown.

Generating doc file

const tempFileName = `output-${new Date().getTime()}.docx`;
    const content = fs.readFileSync(path.resolve(__dirname, formPath), 'binary');
    const zip = new PizZip(content);
    const doc = new Docxtemplater(zip, {nullGetter: () => " ", linebreaks: true});
    doc.setData(data);
    doc.render()
    const buf = doc.getZip().generate({type: 'nodebuffer'});
    fs.writeFileSync(path.resolve(__dirname, tempFileName), buf);

Function used to print array of strings

function formatArrayLines(lines: string[]) {
  let outputString = '';
  let i = 1;
  for (const line of lines) {
    outputString += `${i}. ${line}\n\n`
    i++;
  }
  return outputString;
}

Solution

  • So the solution to this problem is not related to the source code but the alignment in the document

    {#form}{importantFacts}{/form}

    So selecting this and setting text align left solved the problem.