VF Page - PDF Header & Footer
We usually get a requirement to create a PDF from a visualforce page and its not at all difficult to generate the PDF. But sometimes we waste our time in dealing with the repeating header and footer of the pdf. Good news for you, this post will save a lot of time.
Use these attributes in the <apex:page> tag:
1. standardStylesheets="false" - This attribute removes the standard styles of a visualforce page.
2. applyBodyTag="false" - This attribute stops visualforce to add a body tag itself.
3. contentType="application/pdf#MyDoc.pdf" - This attribute decides the content type of this page and provide the document name after #.
4. applyHTMLTag="false" - This attribute stops visualforce to add a html tag on its own.
5. sidebar="false" - This attribute hides the sidebar.
6. showHeader="false" - This attributes hides the standard header.
7. renderAs="pdf" - This is the name of any supported content converter. Currently PDF is the only supported content converter. Setting this attribute to "pdf" renders the page as a PDF.
8. controller="YourControllerName" - This attribute gives the apex controller name of your vf page.
Use below CSS for a working Header and Footer:
1. The @page CSS at-rule is used to modify some CSS properties when printing a document. Provide the position of header and footer at top and bottom respectively. Use the <div> IDs of header and footer.
@page {
margin-top: 150px;padding:0px;margin-bottom: 90px;
@top-left {
content: element(header);
}
@bottom-left {
content: element(footer);
}
}
2. Write the Position style for Header and Footer separately like below. It forces the header and footer to be repeated on every page of the document.
position: running(header);
position: running(footer);
3. Write the -fs-table-paginate style to paginate the content. Provide this style on that content which you want to paginate.
-fs-table-paginate: paginate;
Here is the full code of the visualforce page for reference:
<apex:page standardStylesheets="false" title="ABC" applyBodyTag="false" contentType="application/pdf#ABC.pdf" applyHtmlTag="false" controller="ABC" sidebar="false" showHeader="false" renderAs="pdf">
<html>
<head>
<title>ABC</title>
<style type="text/css">
.leadTable{
margin: 10px !important;
}
@page {
margin-top: 150px;
padding:0px;
margin-bottom: 90px;
@top-left {
content: element(header);
}
@bottom-left {
content: element(footer);
}
}
div.header {
margin-top:10px;
position: running(header);
width: 100%;
margin-right: 10px;
}
div.footer {
position: running(footer);
width: 100%;
}
.repeatTableHeader{-fs-table-paginate: paginate;}
</style>
</head>
<body style="font-family:SansSerif !important;margin-left:12px;margin-right:12px;">
<div class="header">
HEADER CONTENT
</div>
<div class="footer" >
FOOTER CONTENT
</div>
<div class="leadTable">
PDF CONTENT
<table class="repeatTableHeader">
<tr><td>CONTENT TO BE PAGINATED</td></tr>
</table>
</div>
</body>
</html>
</apex:page>
Use these attributes in the <apex:page> tag:
1. standardStylesheets="false" - This attribute removes the standard styles of a visualforce page.
2. applyBodyTag="false" - This attribute stops visualforce to add a body tag itself.
3. contentType="application/pdf#MyDoc.pdf" - This attribute decides the content type of this page and provide the document name after #.
4. applyHTMLTag="false" - This attribute stops visualforce to add a html tag on its own.
5. sidebar="false" - This attribute hides the sidebar.
6. showHeader="false" - This attributes hides the standard header.
7. renderAs="pdf" - This is the name of any supported content converter. Currently PDF is the only supported content converter. Setting this attribute to "pdf" renders the page as a PDF.
8. controller="YourControllerName" - This attribute gives the apex controller name of your vf page.
Use below CSS for a working Header and Footer:
1. The @page CSS at-rule is used to modify some CSS properties when printing a document. Provide the position of header and footer at top and bottom respectively. Use the <div> IDs of header and footer.
@page {
margin-top: 150px;padding:0px;margin-bottom: 90px;
@top-left {
content: element(header);
}
@bottom-left {
content: element(footer);
}
}
2. Write the Position style for Header and Footer separately like below. It forces the header and footer to be repeated on every page of the document.
position: running(header);
position: running(footer);
3. Write the -fs-table-paginate style to paginate the content. Provide this style on that content which you want to paginate.
-fs-table-paginate: paginate;
Here is the full code of the visualforce page for reference:
<apex:page standardStylesheets="false" title="ABC" applyBodyTag="false" contentType="application/pdf#ABC.pdf" applyHtmlTag="false" controller="ABC" sidebar="false" showHeader="false" renderAs="pdf">
<html>
<head>
<title>ABC</title>
<style type="text/css">
.leadTable{
margin: 10px !important;
}
@page {
margin-top: 150px;
padding:0px;
margin-bottom: 90px;
@top-left {
content: element(header);
}
@bottom-left {
content: element(footer);
}
}
div.header {
margin-top:10px;
position: running(header);
width: 100%;
margin-right: 10px;
}
div.footer {
position: running(footer);
width: 100%;
}
.repeatTableHeader{-fs-table-paginate: paginate;}
</style>
</head>
<body style="font-family:SansSerif !important;margin-left:12px;margin-right:12px;">
<div class="header">
HEADER CONTENT
</div>
<div class="footer" >
FOOTER CONTENT
</div>
<div class="leadTable">
PDF CONTENT
<table class="repeatTableHeader">
<tr><td>CONTENT TO BE PAGINATED</td></tr>
</table>
</div>
</body>
</html>
</apex:page>
First published on Blogger in 2018. Some screenshots and APIs have changed since; the code is kept as written.