Search code examples
apex-codevisualforce

Link on current Visualforce page to render as PDF


I have a custom built list report page created using Visual force. A link on each row opens up the object details in a new visual force page. I need to have two buttons. One to save/open the current page with all of its information as a PDF and the other button to open the print dialog.

For the button to render current page as PDF, I am assuming its something simple. Remember when the page was first created, it was being passed the record ID as a parameter.

Thanks, Calvin


Solution

  • To render a page as a PDF just requires adding a renderAs="PDF" attribute to the apex:page tag. This value can be dynamic, as in: <apex:page renderAs="{!ISNULL($CurrentPage.parameters.p),'','PDF')}">

    This would render the page as HTML if there was no parameter named "p" included in the URL (e.g. /apex/myPage?id=someId), and as a PDF if this param was included (e.g. /apex/myPage?id=someId&p=1).

    To make it render the current page as a PDF I'd just have the button refresh the page and add the parameter.