+ Start a Discussion

renderAs PDF wrapper


I have an existing page that works great that shows the user a consoled view of a few objects.
I want to render this as a pdf but the users want to do this on their own.
So I will have a button on the page that creates the pdf.

My question is can I have a new page just be the pdf wrapper to my existing page and not duplicate the page code for the pdf page?
How about something like this:
<apex:page showHeader="false" renderAs="{!pdf}" controller="pdfCon">
    This page can be rendered as a pdf
    <apex:commandButton value="render as pdf" action="{!renderpdf}" rendered="{!ISNULL(pdf)}"/>

public class pdfCon {

    public String getPdf() {
        return ('1'.equals(ApexPages.currentPage().getParameters().get('pdf')) ? 'pdf' : null);
    public PageReference renderPdf() {
        PageReference p = ApexPages.currentPage();
        p.getParameters().put('pdf', '1');
        return p;


 So your page only gets rendered if you visit it with a query parameter of pdf=1.

When you click on the button it forwards you to your page with pdf=1.

And also I conditionally rendered the button based on whether you were creating a pdf or not.

Jill this is perfect, thank you.