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
ra1ra1 

CSS not working in PDF generation within Apex

Hi All,

 

I am trying to generate PDF Attachment within Apex class by using below mention code  :

 

public class CssNotWorkingWithPdfVFC {
    
    public static void generatePdf() {
        String pdfHtml = 
        ' <HTML> '+
            ' <HEAD> '+
                ' <STYLE type="text/css"> '+
                    ' body { font-family: \'Arial Unicode MS\'; } ' + 
                    ' .companyName { font: bold 30px; color: red; }  ' +
                    ' @page { size:landscape; } ' +
                ' </STYLE> ' +
            ' </HEAD> ' + 
            ' <BODY> ' +           
                ' <CENTER> ' + 
                    ' <TABLE> ' + 
                        ' <TR> ' + 
                            ' <TD>Company Name</TD> ' + 
                            ' <TD class="companyName">Test generating PDF in SALESFORCE</TD> ' + 
                        ' </TR> ' + 
                    ' </TABLE> ' + 
                ' </CENTER> ' + 
            ' </BODY> ' + 
        ' </HTML> ';
        
        System.debug('@@ ' + pdfHtml);
        
        Id recordId = 'a099000000JyF9i';
        String pdfName = 'TestCssInPdf.pdf';
        
        try {
            System.debug('@@ Generating PDF attachment ');
            Attachment attachmentPDF = new Attachment();
            attachmentPDF.parentId = recordId;
            attachmentPDF.Name = pdfName;
            attachmentPDF.body = Blob.toPDF(pdfHtml );
            insert attachmentPDF;

            System.debug('@@ Successfully generated PDF attachment ');                
        } catch(Exception e) {
            System.debug('@@## Error : ' + e.getMessage());
        }
    }
}

 

Here PDF attachment is creating successfully but without any CSS effects. Whereas If I tried to use same HTML in VF page (by using renderAs="pdf") CSS will work perfectly. below is sample code :

<apex:page showHeader="false" applyHtmlTag="false" applyBodyTag="false" standardStylesheets="false" renderAs="pdf" >
    <HTML>
        <HEAD>
            <STYLE> 
                body { font-family: 'Arial Unicode MS'; }
                .companyName { font: bold 30px; color: red; }  
                @page { size:landscape; }
            </STYLE>
        </HEAD>
        
        <BODY>
            <CENTER>
                <TABLE>
                    <TR>
                        <TD>Company Name</TD>
                        <TD class="companyName">Test generating PDF in SALESFORCE</TD>
                    </TR>
                </TABLE>
            </CENTER>
        </BODY>
    </HTML>
</apex:page>

 

I am unable to figureout the root cause and need your expert help on same.

Thanks in advance !!

 

 

 

 

ra1ra1

Hi Vinita,

 

Thanks for your quick response. I am able to add CSS within PDF if I go with Visualforce page but same generated HTML is not working if I use it in Apex class/trigger.  (using below code)

 

attachmentPDF.body = Blob.toPDF(pdfHtml );

 

In sort, CSS is not working in Apex code and I have to generate PDF in trigger only.

 

Thanks & Regards,

 

 

Vinita_SFDCVinita_SFDC

okay, well it is not recommended to include CSS in apex trigger. Can you please View source of the page and check even if CSS is working at all from tirgger.

ra1ra1

Hi Vinita,

 

In my code, I am generating HTML only and CSS is part of this html as String, so as per my understanding it should not create any issue. Correct me if I am wrong.

 

My doubt is, why same HTML is working fine with VF page where not working in Apex.. :(

 

Regards,

Vinita_SFDCVinita_SFDC

Hello,

I understood that the issue is occurring when you are including HTML in apex trigger. Following thing should be considered when parsing the blob into a string

    PDF rendering doesn’t support images encoded in the data&colon; URI scheme format
    Don't use components that rely on JavaScript
    Don't use components that depend on Salesforce stylesheets
    tags that rely on JS or an external URI are going to confuse the PDF generator
    a tag that refers to an external stylesheet that's not a static resource are also going to be problematic.

Refer: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_additional_render_pdf.htm#pages_compref_additional_render_pdf

But in your code i don't see if any of these is violated, are you getting any exception?

ra1ra1

Hi Vinita,

 

No, I am successfully able to generate PDF using above mention code, only facing formating issue within it.

I also tried to add inline style within each & every tag and it is working fine. for example : 

<TD style="font: bold 30px; color: red;">Test generating PDF in SALESFORCE</TD>

 

But it will will be very tuff to manage if later I want to change any style of particular time, also how can I set PDF layout / header / footer releated CSS like :

 

   @page { 
       size:landscape;  
       
       @bottom-right {
           content: "Page " counter(page) " of " counter(pages);
        } 
    }

 

Regards,

 

bob_buzzardbob_buzzard

That's been the case for a few years now - one or two of the CSS items make it through, but the majority don't.  HTML tables have always required inline styling I'm afraid.

 

You can add the footer css into the page - here's an example from one of my pages:

 

<style type="text/css" media="print">
            @page {                 
                @top-center {                   
                    content: element(header);               
                }
                @bottom-left {
                    content: element(footer);
                }           
                margin-top: 100px;
                margin-bottom:80px;
            }                       
 
            div.header {                
                padding: 10px;              
                position: running(header);           
            }           
            div.footer {                
                display: block;             
                padding: 5px;               
                position: running(footer);          
            }                       
 
            .pagenumber:before {                
               content: counter(page);             
            }                       
            .pagecount:before {             
                content: counter(pages);            
            }                   
</style>            

 

Vinita_SFDCVinita_SFDC

Hello,

 

Salesforce recommends CSS to be included in the page instead of apex, though it not documented anywhere that css in apex is not supported but no document says it is supported.

 

I would suggest you to have CSS implemented in your VF.

ra1ra1

Hi Vinita,

 

I can't move my code to VF Page, as per our business requirement I have to generate PDF on record creation, with each update of record if set of fields get modified or when record get approved or rejected.

 

Also we are using standard page to view/edit this record. So I believe I have only option to design the PDF in Apex itself. 

I also tried below code : 

 

PageReference pdfPage = Page.pdfRenderPage;
pdfPage.getParameters().put('id',recId);
Blob body = pdf.getContent();

 But "getContent()" method call cann't be called from trigger, apex or even from async calls (like future, batch or scheduler). There is a hack if I go with REST/SOAP call then it will work.. but I don't want to whitelist any url (Remote Site Settings for REST) or to generate WSDL & its stub for SOAP, just to support generating PDF.

 

Apex based PDF is workign as expected and the only issue is Formating.. which we already discussed here.

Looks like Salesforce is treating PageReference.getContent() & Blog.toPDF() separately.

 

@bob_buzzard : Na.. I am getting all style CSS as pdf content.. so it is not working if I m generating it in Apex itself.

 

Thanks & Regards,