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
kfennkfenn 

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"/>
</apex:page>

 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"/>
</apex:page>

 

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

Thanks!

asawantasawant

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.

 

Thanks!

Neeraj_ChauriyaNeeraj_Chauriya

Hi,

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 {
        get{
            return this;            
        }set;
    }
    public TestPDFController(){
        htmlText = '<p style="color:red;">Sample HTML Input</p>';    
    }
}

 
Page:

 

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

 
Component:

 

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

 


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,
Neeraj

kfennkfenn

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;
}

}

 

Neeraj_ChauriyaNeeraj_Chauriya

Hi,

 

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.