0

I have a requirement where I have to send a VF page as a pdf email attachment to user. The problem is that a table present on the VF page has many columns and they don't fit in the pdf attachment.

All the columns will fit if I am able to send the pdf attachment in landscape format. Is there a way to do it?

I tried adding style = "size: landscape;" to apex:pageBlockTable.

I also tried adding CSS but it didn't work.

<style type="text/css">
            @page {
            size: landscape;  
            }
</style>

I found this Reference. It used renderAs="advanced_pdf". But this didn't work too.

Seems like it has not been implemented yet. Link. Release notes

Are there any other options available? Please guide. Thank You!

Code:

VF Page

<apex:page controller="profile_Controller" tabStyle="Account">
    <apex:form id="copsform">
        <apex:pageBlock >
            <apex:pageBlockButtons >                
                <apex:commandbutton id="button2" value="Email RateSheet" action="{!EmailRateSheet}" reRender="" rendered="{!RateListTable.size!=0}"/>                              
            </apex:pageBlockButtons>
            <apex:pageBlockSection Title="Rate"  columns="1" collapsible="false" rendered="{!RateListTable.size!=0}" >
                <apex:pageBlockTable value="{!ListTable}"  var="Rate" id="RateTable" style="width:100%;" cellPadding="4" border="1">   
                    <apex:column headerValue="ID" value="{!Rate.Id}" /> 
                    <apex:column headerValue="Product Name" value="{!Rate.PrName}" />
                    <!-- More columns here -->
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock> 
    </apex:form>
</apex:page>

EmailRateSheet() - Controller

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
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); //create attachment
        attachment.setFileName('RateProfile for ' + CustomerRec.Name + '.pdf'); //set the file name of attachment
        attachment.setBody(rateProfile.getContentAsPDF()); //set the pdf version of the page in body of the attachment
        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.
    }

1 Answer 1

1

You can apply landscape to the entire page using the @page CSS selector. Here is a fully functional version you can use as a starting point:

<apex:page renderAs="advanced_pdf" showHeader="false" applyHtmlTag="false" applyBodyTag="false">
    <html>
        <head>
            <style>
                @page { size: A4 landscape; }
            </style>
        </head>
        <body>
            Content
        </body>
    </html>
</apex:page>

If you still want to use the standard headers and such, you can use apex:stylesheet to import the style sheet properly for the rendering engine to recognize the page layout.

Note that you should be able to use either pdf or advanced_pdf for the rendering mode. While it is true that this mode was suspended, as far as I can tell, it is now GA for everyone.


Edit: CSS as static resource version.

myPDFCSS Static Resource

@page { size: A4 landscape; }

Example Visualforce

<apex:page renderAs="pdf">
    <apex:stylesheet value="{!$Resource.myPDFCSS}" />
    Whatever Page Content Here
</apex:page>
4
  • Thank You! It's working. It didn't work the first time because I was not setting showHeader="false" and applyHtmlTag="false" for the <apex:page>. Is there a way to retain the header or does it have to go? I didn't get the point about how I can retain the header using apex:stylesheet. Thanks again.
    – Koustubh
    Jan 11, 2021 at 12:41
  • 1
    @Koustubh See edit.
    – sfdcfox
    Jan 11, 2021 at 12:46
  • Okay. Got it. Thanks a lot! 😃
    – Koustubh
    Jan 11, 2021 at 13:22
  • I have a question that's related to this. Please guide. salesforce.stackexchange.com/questions/332490/…
    – Koustubh
    Jan 27, 2021 at 7:18

You must log in to answer this question.

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