Salesforce Visualforce allows developers to render pages as PDF documents by setting the renderAs="pdf" attribute on the apex:page component. These PDF documents can be formatted using CSS to control layout properties such as paper size, margins, page headers, page footers, and page numbering.
This is useful when you need to generate professional, multi-page PDF documents from Salesforce data — for example, a branded quote, invoice, or report that requires consistent headers and footers across all pages and controlled page breaks between sections.
Visualforce PDF rendering uses the Flying Saucer library, which supports a subset of CSS 2.1 and some CSS 3 features. The key CSS construct for controlling PDF layout is the @page rule, which lets you define page size, margins, and content in the header and footer of every page.
The following Visualforce page demonstrates how to create a PDF document with these properties:
<div> element with the class page-break forces the content onto a new pageKey CSS properties explained:
@page { size: letter; } — sets the paper size to US Letter.@page { margin: 25mm; } — sets 25mm margins on all sides.@top-center { content: "Sample"; } — places the static text "Sample" in the center header of every page.@bottom-center { content: "Page " counter(page) " of " counter(pages); } — uses CSS counters to display the current page number and total page count..page-break { display: block; page-break-after: always; } — forces a page break after each element with this class.body { font-family: Arial Unicode MS; } — sets a Unicode-compatible font to support special characters.Visualforce page attributes used:
renderAs="pdf" — triggers PDF rendering.showHeader="false" and sidebar="false" — removes the Salesforce header and sidebar from the PDF.standardStylesheets="false" — prevents Salesforce default stylesheets from interfering with your custom CSS.applyBodyTag="false" and applyHtmlTag="false" — allows you to define your own <html> and <body> tags for full layout control.Note: The provided solution may not work for all layout scenarios. You may need to enable or disable apex:page options (such asapplyBodyTagorapplyHtmlTag) to achieve the desired PDF output. Test with your specific content and browser environment.
<apex:page 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">Page A</div>
<div class="page-break">Page B</div>
<div>Page C</div>
</body>
</html>
</apex:page>000385121

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.