0

I have a VF page that I display to the user and also send it as a PDF email attachment. I wish to use normal font size while showing the VF page but smaller font size while sending the VF page as an email attachment. I tried to implement this requirement through merge field but it's not working.

Code

Flag reduceFont will control the font size on the VF page.

<apex:pageBlockTable style="{!IF( reduceFont = true, 'width:100%;font-size: 90%;', 'width:100%;')}" > 

Declared the flag in the controller.

public boolean reduceFont{get; set;}  

Initialized the flag to false in the constructor.

reduceFont = false;

Used the following code to set the flag reduceFont to true and send an email.

public PageReference EmailRateSheet() {        
        
        PageReference rateProfile =  ApexPages.currentPage(); //get the current page reference
        rateProfile.setRedirect(true); //set the redirect property to true to avoid the page resubmission warning
        rateProfile.getParameters().put('reduceFont','true'); //set the flag reduceFont to true
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); //create attachment
        attachment.setFileName('RateProfile for ' + CustomerRec.Name + '.pdf'); //set the file name of attachment
        if(!test.isRunningTest()){ //The method getContentAsPDF() cannot be used for test methods. More Info- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_getContentAsPDF.htm
            attachment.setBody(rateProfile.getContentAsPDF()); //set the pdf version of the page in body of the attachment.  
        }else{
            attachment.setBody(blob.toPDF('Test class Running'));
        }
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); //create SingleEmailMessage object
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } ); //set the attachment to the email
        message.setSubject('Rate sheet for ' + CustomerRec.Name); //set subject of the email
        message.setHTMLBody('Hi '+ userInfo.getFirstName() + ', <p>' +' The Rate sheet for <b>' + CustomerRec.Name + '</b> is attached. <p> Regards,<P> Salesforce Team'); //set body of the email
        message.setToAddresses( new String[] { UserInfo.getUserEmail() } ); //set the email address of the logged-in user as recipient address
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } ); //send the email
        return rateProfile; //return the page reference. If you return null, page resubmission warning will appear on refreshing the page.
}

The above method is not working. The font size does not get reduced in the PDF.

I tried setting reduceFont = true in the method EmailRateSheet() but that didn't work too. What am I doing wrong? Please guide. Thank You!

1 Answer 1

0

In your PDF page's constructor, read the reduceFont parameter and set it appropriately:

reduceFont = ApexPages.currentPage().getParameters().get('reduceFont') == 'true';

Alternatively, use the @media print CSS media query:

@media print {
  font-size: 90%;
}
8
  • Thank You for the answer! I tried out both methods. Method 1: I set the reduceFont parameter appropriately. It is not working for PDF. I am able to reduce the font size of VF page by returning page reference after setting rateProfile.getParameters().put('reduceFont','true'); but it's not working on the PDF email attachment. I am not sure why. It may be due to rateProfile.getContentAsPDF(). Method 2: I tried setting @media print { font-size: 90%; } in <style> tag as well as in CSS static resource. Both didn't work.
    – Koustubh
    Commented Jan 29, 2021 at 9:13
  • I also tried applying merge field on stylesheet as follows: <apex:stylesheet value="{!IF( reduceFont = true, '{!$Resource.ReduceFontPDF}', '')}"/>. In this case, the resource is not getting applied because of extra quotes. If I remove the quotes, I get a syntax error.
    – Koustubh
    Commented Jan 29, 2021 at 9:17
  • @SFdev You don't use merge field syntax inside a merge field, it would be value="{!IF(reduceFont, $Resource.ReduceFontPDF, '')}"
    – sfdcfox
    Commented Jan 29, 2021 at 9:28
  • @SFdev Also, if you've already set renderAs="pdf" on the page, you don't need getContentAsPDF(), just use the normal getContent() instead.
    – sfdcfox
    Commented Jan 29, 2021 at 9:29
  • Thanks for responding. I tried setting the <apex:stylesheet> as suggested by you. It did not reduce the font size of PDF file. Also, I have not used renderAs="pdf" on the VF page because I am displaying the VF page to users and sending the same page as email PDF attachment on click of a button.
    – Koustubh
    Commented Jan 29, 2021 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .