Search code examples
javacsspdftext-alignflying-saucer

Flying-saucer generates PDF with badly justified text


Using <p style="text-align: justify;">...</p>to justify text generates a paragraph that is not properly justified. It seems that the problem is in the empty space that is left at the end of line.

As generated by flying-saucer: (download simple maven project)

Paragraph generated by flying-saucer

In contrast to the expected rendering as in a browser: (see jsfiddle)

Justified in browser

Is there any known workaround for this? Thanks


Solution

  • The problem seems to be that flying-soucer does not use the Paragraph of the underlying itext, but splits the text into the individual lines itself. While doing it, flying-soucer does not remove the trailing empty spaces.

    A quick fix is to insert currentLine.trimTrailingSpace(c); before this call to save().