Print this page

Visualforce: Text truncated when PDF is generated

Knowledge Article Number 000206650
Description If text value in a field is longer then it might be truncated when visualforce is rendered as PDF (renderAs="PDF"). 

There are 2 solutions for this:
  1. Wrapping text in <pre> HTML tag
    1. This will preserve the new lines, however, it will still truncate continuous text for a long line
  2. Using <apex:outputText> will solve the truncate issue how ever it doesn't preserve new lines
Resolution Solution is to use <apex:outputText> and preserve new lines by replacing "\n" with "<br/>" tag
<apex:page standardController="Case" renderAs="PDF" applyBodyTag="false" extensions="TestPDFExtension" action="{!process}"> 
	<head> 
		<style> 
		/*If needed*/ 
		</style> 
	</head> 
	<body> 
		<h1>case number: {!Case.CaseNumber}</h1> 
		<table> 
			<tr> 
				<td> 
					<apex:outputLabel > Detail of &nbsp;test:&nbsp;&nbsp; </apex:outputLabel> 
				</td> 
				<td> 
					<apex:outputText value="{!str}" escape="false" /> 
				</td> 
		</tr> 
		</table> 
	</body> 
</apex:page>
 
public class TestPDFExtension { 
	public Case c; 
	public String str {get;set;} 
	public TestPDFExtension (ApexPages.StandardController stdController) { 
		stdController.addFields(new list<string>{'test__c'}); 
		this.c= (Case)stdController.getRecord(); 
	} 
	public PageReference process(){ 
		str = this.c.test__c; 
		str = str.replaceAll('\n','<br />'); 
		return null; 
	} 
}

 




promote demote