+ Start a Discussion

Render PDF in Visualforce Using Dynamic HTML

I am trying to create a simple VF page that renders a PDF from HTML that is passed in by my controller. Using the VF code below, the dynamic HTML renders properly as a regular VF page, but when I add the "renderAs="PDF" to my page tag, only the raw HTML appears in the PDF and not the properly rendered HTML.


The following correctly renders the dynamic HTML:

<apex:page controller="trainingLetterPDFController" >
    <apex:outputText value="{!LetterHTMLBody}" escape="false"/>

 The following renders the page as a PDF showing the raw HTML text:

<apex:page controller="trainingLetterPDFController" renderAs="PDF">
    <apex:outputText value="{!LetterHTMLBody}" escape="false"/>


Any suggestions on how to get this dynamic HTML to render in the PDF?



You probably need to use proper controller side encoding to the LetterHTMLBody variable. It appears that the variable value is being treated as text when it's rendered as PDF.





It seems like, while rendering PDF it escapes the HTML tags when used dynamically.
If it is possible in your scenario, create a visualforce component containing the dynamic HTML part of the your page and then include that component wherever you are using the field "LetterHTMLBody".

You can refer the following sample code:

Controller class:

public class TestPDFController{

    public String htmlText {get;set;}
    public TestPDFController conInstance {
            return this;            
    public TestPDFController(){
        htmlText = '<p style="color:red;">Sample HTML Input</p>';    



<apex:page controller="TestPDFController" renderAs="PDF">
  <apex:outputText >Sample PDF..Testing HTML fields</apex:outputText><br/>
  <c:PDFHTML conObj="{!conInstance}"/>



    <apex:attribute type="TestPDFController" name="conObj" description="con object instance"/>
  <apex:outputText value="{!conObj.htmlText}" escape="false"></apex:outputText>


Hope this helps you!

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank you,


Thanks, Neeraj and asawant. I will give this a try and will give kudos. In the meantime, I am posting my original controller code for some context.


The controller is accessing the HtmlValue of an email template, and this is ultimately what I am passing to my VF page to produce the PDF.


public class trainingLetterPDFController {

EmailTemplate e = [SELECT id,HtmlValue FROM EmailTemplate WHERE id = '00XK0000000QVjj'];

public String getLetterHTMLBody() {
    return e.HtmlValue;






You can go with the approach I have mentioned above, by using the htmlvalue in the component and adding component in the visualforce page.

Hara SahooHara Sahoo
Thanks Neeraj, the solution was helpful and solved the purpose.