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
Lukas R.Lukas R. 

Render as pdf with header/footer

Hi,
Im trying to generate letter (pdf) using visualforce for campaign members who are contacts and have status 'Sent' with specific header and footer (see code below). This works fine however the result generated always includes one additional blank page with header/footer at the end. Im not really good at coding, can somebody help me out? Thanks a lot
<apex:page standardController="Campaign" renderAs="pdf" showHeader="false" sidebar="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">

    <html>
    <head>
        <style>
            @page {
                size: letter;
                margin: 25mm;
                @top-center {
                    content: "Sample";
                }
                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages);
                }
            }
            .page-break {
                display:block;
                page-break-after:always;
            }
            body {
                font-family: Arial Unicode MS;
            }
        </style>
    </head>
    
    
    <body>
        <div class="page-break">
    
          <apex:repeat value="{!Campaign.CampaignMembers}" var="line">
  
            <apex:outputPanel layout="block" style="page-break-after:always;" rendered="{!line.Status='Sent'}"> 
              
                  <apex:outputText value="{!line.contact.Name}," escape="false"/><br/>
                  <apex:outputText value="{!Campaign.Test_Letter__c}"  escape="false"/><br/>
                  <apex:outputText value="S pozdravem,"  escape="false"/><br/>
                  <apex:outputText value="Lukas" escape="false"/><br/>
            
            </apex:outputPanel>

          </apex:repeat>
    
        </div>
    </body>
    
   </html>
   
</apex:page>

 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Lukas,
Since, "page-break-after:always;" will insert the blank page always.  So, we need to render it based on some condition in order to get rid from the one extra blank page in last.
Since, You are using, StandardController , you can use extension to get the 'result size' of the standeard controller and you can render the ""page-break-after:always;" based on this condition.

I have created a small demo for you. Hope it will help you. 

VF page -
<apex:page Controller="testpdf" renderAs="pdf" showHeader="false" sidebar="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">
    <head>
        <style>
            @page {
                size: letter;
                margin: 25mm;
                @top-center {
                    content: "Sample";
                }
                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages);
                }
            }
            .page-break {
                display:block;
                page-break-before: always;
            }
            body {
                font-family: Arial Unicode MS;
            }
        </style>
    </head>
            <apex:variable value="{!1}" var="rowNum"/>
          <apex:repeat value="{!conList}" var="line"> 
                  
                  <apex:outputText value="{!line.Name}"  />

                  <apex:outputPanel rendered="{!IF(rowNum <= (listSize-1) ,true,false)}">
                      <div class="page-break" display="">  
                      </div>
                  </apex:outputPanel>
                  
              <apex:variable var="rowNum" value="{!rowNum + 1}"/>
          </apex:repeat>
</apex:page>

Apex Controller -
public class testpdf {
    public List<Contact> conList {get;set;}
    public Integer listSize{get;set;}

    public testpdf() {
        conList  = [SELECT id ,name from CONTACT LIMIT 5];
        listSize = conlist.size();
    }
}
Please, let me know if it helps you.

Thanks,
Sumit Kumar Singh
Sumit Kumar Singh 9Sumit Kumar Singh 9
Use this VF instead -
<apex:page Controller="testpdf" renderAs="pdf" showHeader="false" sidebar="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">
    <head>
        <style>
            @page {
                size: letter;
                margin: 25mm;
                @top-center {
                    content: "Sample";
                }
                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages);
                }
            }
            .page-break {
                display:block;
                page-break-after: always;
            }
            body {
                font-family: Arial Unicode MS;
            }
        </style>
    </head>
            <apex:variable value="{!1}" var="rowNum"/>
          <apex:repeat value="{!conList}" var="line"> 
                  
                  <apex:outputText value="{!line.Name}"  />

                  <apex:outputPanel rendered="{!IF(rowNum <= (listSize-1) ,true,false)}">
                      <div class="page-break">  
                      </div>
                  </apex:outputPanel>
                  
              <apex:variable var="rowNum" value="{!rowNum + 1}"/>
          </apex:repeat>
</apex:page>