I have a visualforce page that renders a pdf where the controller makes a call to an external service to retrieve legal content from a single source of truth.

Someone, in their infinite wisdom, decided that this legal content should be formatted in this single source of truth, so the content returns things that look like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

What can I do to make the visualforce page go to a newline at that point? I've tried replacing the \n with a linebreak tag, and it just displays the line break tag, but doesn't render it. I've also tried replacing \n with \r\n, to no avail.

1 Answer 1


Use CSS to address the problem:

<div style="white-space: pre-line">

This will honor newlines, but still collapses extra whitespace. If you need the whitespace preserved, too, use pre-wrap instead.

  • That worked wonderfully, thanks! Any chance you have an answer for text-align: justify? It doesn't seem to be doing anything in the pdf?
    – Zach
    May 12, 2021 at 15:25
  • @Zach I tested it, and it definitely does work. Try: <div style="white-space: pre-line; text-align: justify"> (note the semi-colon). However, keep in mind that justify will not justify the last line; the text must be longer than what fits on one line to justify. There's a justify-all option, but it's not supported in PDF mode, as it is a new experimental CSS feature.
    – sfdcfox
    May 12, 2021 at 15:37
  • Seems to not like the text-align: justify when it's inside of an apex:repeat tag; testing with hardcoded values outside of that tag renders the justification as expected, however once I move it inside that tag it fails. Oh well, design is just going to have to live with it. Thanks again.
    – Zach
    May 12, 2021 at 15:56
  • @Zach it should be fixable, we'd have to see the code.
    – sfdcfox
    May 12, 2021 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .