function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mikefmikef 

renderAs PDF wrapper

Hi:

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?
jwetzlerjwetzler
How about something like this:
Code:
<apex:page showHeader="false" renderAs="{!pdf}" controller="pdfCon">
  <apex:form>
    This page can be rendered as a pdf
    <br/>
    <apex:commandButton value="render as pdf" action="{!renderpdf}" rendered="{!ISNULL(pdf)}"/>
  </apex:form>
</apex:page>

 
Code:
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.

mikefmikef
Jill this is perfect, thank you.