Search code examples
javascriptjspdfjustify

Why using jsPDF justify for text keep the word spacing for other texts?


Program for generating the pdf:

const doc = new jsPDF('p', 'pt', 'a4', true);

doc.setFontSize(14);
doc.setDrawColor(0, 0, 0);
doc.text(testText, 30, 30, {maxWidth: 200, align: 'justify'});

doc.text('10 de dezembro', 30, 220, {maxWidth: 200, align: 'left'});

doc.save('testing.pdf');

Problem is with the result, by what i see, it keep the last word spacing for the next texts

Image with result


Solution

  • I've faced the same issue, and to solve it I had to reset the word spacing to the default (0) by manually doing doc.internal.write(0, "Tw") right after using a justified text (had to look into the source code to find it), so your code would look like this:

    const doc = new jsPDF('p', 'pt', 'a4', true);
    
    doc.setFontSize(14);
    doc.setDrawColor(0, 0, 0);
    doc.text(testText, 30, 30, {maxWidth: 200, align: 'justify'});
    doc.internal.write(0, "Tw") // <- add this
    
    doc.text('10 de dezembro', 30, 220, {maxWidth: 200, align: 'left'});
    
    doc.save('testing.pdf');
    

    Hope this helps.