Search In This Blog

2016-08-17

How to display Visualforce page as PDF

Add renderAs="pdf" in <apex:page> tag as below.
<apex:page renderAs="pdf">
Add CSS style to set format of page.
@page {
size: letter;
@top-center {content: "PDF Sample";}
@bottom-center {content: "Page " counter(page) " of " counter(pages);}
}
*Most used page size: landscape/8.27in 11.69in (A4 width and height)

Attention:
@page must be inside of <html><head><style>, or include as a static resource.
<apex:page renderAs="pdf">
    <html>
        <head>
            <style>
                @page {...}
            </style>
        </head>
    </html>
</apex:page>

Use page-break to control when is next page. If you didn't set page break, it will only display in next page when out of bound.
<style>
.page-break {display:block; page-break-after:always;}
</style>
<body>
        <div class="page-break">
                <apex:outputText >Page 1</apex:outputText>
        </div>
        <div>Page 2</div>
</body>

No comments:

Post a Comment