Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

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

Than whenever that page open it download as a pdf

share|improve this answer
1  
It only rendres a page as PDF. The user need to download it yourself. –  mast0r Dec 20 '12 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. –  Samuel De Rycke Dec 20 '12 at 13:20
    
Yes, it can be but without a normal filename. For my org it is very important to have custom filenames. –  mast0r Dec 20 '12 at 13:22
add comment

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. First page generates an PDF file and 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;
    }
}
</stype>
</head>

Here is your main content ...

</apex:page>
share|improve this answer
    
thanks mastOr this code is really helpfull to me –  rakesh Dec 20 '12 at 12:17
    
An alternative approach to setting the file name is to set the content-disposition header: salesforce.stackexchange.com/questions/4118/… –  Peter Knolle Jan 1 '13 at 13:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.