function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AUEagleAUEagle 

Replace <br /> with newline character while rendering PDF's

I am trying to render a PDF page using a text field value that currently has <br /> as part of the text field and still maintain the newlines breaks where the existing breaks exist.   For example, I have a field named Other_Contact_Name__c with a value:  My Name<br />3312 Hard Rock Court<br />Richmond, VA  23230

 

I want to render the field on a PDF so that the output apears as:

My Name

3312 Hard Rock Court

Richmond, VA  23230

 

I have tried this:

<apex:outputText value="{!SUBSTITUTE(Case.Other_contact_name__c,'<br />', '\r')}/>

 

I have also tried replacing with '\n', '\r\n' but the output simply is displaying whatever value I try to substitue.  Any help would be greatly appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I'd expect the PDF renderer to handle <br/> components without any additional effort on your part.

 

I think you need to stop outputtext escaping the html, e.g.

 

 

<apex:outputText value="{!Case.Other_contact_name__c)}" escape="false"/>

 

 

All Answers

bob_buzzardbob_buzzard

I'd expect the PDF renderer to handle <br/> components without any additional effort on your part.

 

I think you need to stop outputtext escaping the html, e.g.

 

 

<apex:outputText value="{!Case.Other_contact_name__c)}" escape="false"/>

 

 

This was selected as the best answer
Cory CowgillCory Cowgill

Have you tried to use <apex:outputField> instead of <apex:outputText>. This will automatically add the linebreaks for you.

AUEagleAUEagle

Thank you, that worked perfectly.

SDFC_kittySDFC_kitty

Hello bob buzzard,

bob_buzzardbob_buzzard

I think you'll have to find another way to do this - something that parses the field and removes any HTML tags.  The problem is that if you specify the content type as text, then any HTML wouldn't be interpreted by the browser, as it is displaying everything as raw text.

SDFC_kittySDFC_kitty

Hello bob buzzard, 

 

Big thank for your reply!!! 

 

I am just new to saleforce so I would be very happy if you can share me some idear to aviod this problem. i just want to render data in text format but i don need <br/>tag appear in it the text. 

I wonder if there are any apex code to solve this problem??? or in order to go new line, we can only use these tags \r, \n, <br/> ??? 

 

Thank you in adavance. 

Best, 

bob_buzzardbob_buzzard

Is this in an HTML(Visualforce) page, or are you setting the content type of the page to text?

SDFC_kittySDFC_kitty

Hello bub,

 

I set the content type like this:  contentType="application/csv#DomainList.csv;charset=utf8". 

 

here is the sample of my code: 

 

<apex:page standardController="Object__c" showHeader="false" sidebar="false" extensions="theController"
id="Thepage" contentType="application/csv#FileExport.csv;charset=utf8" cache="true" readOnly="true">

<apex:repeat value="{!FieldsList}" var="fields" id="theFieldsLoop1">"{!mapFieldLabel[fields]}",</apex:repeat>
<br/>
<apex:repeat value="{!AllRecords}" var="result" id="theDataLoop">
<apex:repeat value="{!FieldsList}" var="f" id="theFieldsLoop2">"{!result.mapDto[f]}",</apex:repeat>
<br/>
</apex:repeat>
</apex:page>

--------------------------

 

Thank you in advance for your reply :)

best, 

 

bob_buzzardbob_buzzard

And have you tried changing your merge fields to use outputtext with escape="false" ?  E.g.

 

<apex:repeat value="{!FieldsList}" var="f" id="theFieldsLoop2">"<apex:outputText value="{!result.mapDto[f]}" escape="false"/>",</apex:repeat>

 

SDFC_kittySDFC_kitty

Hello bob, 

 

I tried!!! but it dint work!! :(  i still cannot solve it now. 

 

Best, 

bob_buzzardbob_buzzard

So I guess you'll have to handle this server side, by replacing all instances of '<br/>' with '\n'.

SDFC_kittySDFC_kitty

You meant replace the html tag <br/> with "\n" in my code , right ??? 

 

Best, 

bob_buzzardbob_buzzard

I don't know.  Do you add the HTML line breaks in via code?  If so, then yes, replace it that way.  If a user just types it in to a rich text area or similar, then no, you'll have to parse the string and replace it.

SDFC_kittySDFC_kitty

yes, i add it all in my apex page code. 

I have no idear now. i did replace it to '\n' but it dint work. 

my code sample: 

--------------------------------

<apex:repeat value="{!FieldsList}" var="f" id="theFieldsLoop2">"{!result.mapDto[f]}",</apex:repeat>
\n
</apex:repeat>

---------------------------

Here is the sample of the result. 

 

Name       Age

\n"Marry"   20

\n"Jhon"    25 

---------------

Best, 

 

 

bob_buzzardbob_buzzard

I guess that means that '\n' doesn't mean anything in CSV files.  It sounds like you might have to actually move onto a new line.  Not sure how you'd do that TBH.

SDFC_kittySDFC_kitty

What is TBH? Would mind clarifying a bit more clear about what just said? 

 

Best, :) 

bob_buzzardbob_buzzard

TBH means to be honest.

 

Short answer, I don't know how you are going to be able to do it.

SDFC_kittySDFC_kitty

 

"move onto a new line" , it is what you think m not gonna be able to do it??? or the topic of my post??? 

However, i thank you very much for spend your time help me :) 

 

You will be blessed ;) 

 

 

bob_buzzardbob_buzzard

I don't know how you are going to get a CSV file to throw a line break.

SDFC_kittySDFC_kitty

There must be ways to solve, or we can change to other solution beside csv file if it really does not work :) 

Thank you again bob :)

 

Cheer :) 

SFDC_kittySFDC_kitty

.

SDFC_kittySDFC_kitty

hi