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}"> 
		/*If needed*/ 
		<h1>case number: {!Case.CaseNumber}</h1> 
					<apex:outputLabel > Detail of &nbsp;test:&nbsp;&nbsp; </apex:outputLabel> 
					<apex:outputText value="{!str}" escape="false" /> 
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