+ Start a Discussion

Tell button to open page in new window and set its attributes



I have a button called Display As PDF which basically displays the current page in PDF. I want this button, upon press, to open the PDF view in a new window while leaving the current window as is. ALSO, it should set some attributes on the new page before displaying it (FYI the attribute it sets is the renderAs attrib of the page).



Best Answer chosen by Admin (Salesforce Developers) 

I'm assuming your button is a commandButton and you have an action that returns a PageReference.  Displaying the current page as a pdf requires some javascript:


<apex:commandButton value="View as PDF" onclick="window.open('{!$CurrentPage.url}?renderas=pdf&id={!$CurrentPage.parameters.id}');" />

Here I have used the javascript function 'window.open' and passed in the current page's url and two parameters (one is the renderas attribute whilst the other is the id attribute that I take from the current page parameters).


Alternatively you could make your commandButton a commandLink and use the target attribute:



<apex:commandLink action="{!pdfPage}" value="View as PDF" target="_blank" />