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
asadimasadim 

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

Hi,

 

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).

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

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" />