3

How do I convert a visualforce page to a PDF? Please provide a code sample.

2 Answers 2

9

You can use : apex:page renderAs="pdf" showHeader="false"

Than whenever that page open it download as a pdf

3
  • 2
    It only rendres a page as PDF. The user need to download it yourself. Dec 20, 2012 at 12:58
  • 1
    @mast0r I think that depends on the browser, older browsers probably make you download it as a file when they are not capable to display it. Dec 20, 2012 at 13:20
  • Yes, it can be but without a normal filename. For my org it is very important to have custom filenames. Dec 20, 2012 at 13:22
7

If you need not only convert your page to PDF but save this file with a specific name here is a workaround. You will need two pages – the first page generates an PDF file and the second saves it with specific filename:

The main "wrapper" page saves PDF and gives a filename:

<apex:page id="MainPage"
           showHeader="false" 
           cache="true"
           contentType="application/x-pdf#Here-is-your-filename.pdf">

    <!-- Here comes another page with a content to be converted to PDF -->
    <apex:include pageName="PageThatGeneratesPdf"/>
</apex:page>


PageThatGeneratesPdf – only generates PDF content:

<apex:page showHeader="false"
           renderAs="PDF"
           cache="true">

<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

<style type="text/css">
@page{
    size:A4 portrait;

    @bottom-right {
        content: "Page " counter(page) " - " counter(pages);
        font-family: 'Arial', 'Helvetica', sans-serif;
        font-size:10px;
    }
}
</style>
</head>

Here is your main content ...

</apex:page>
1

You must log in to answer this question.

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