I have the following CommandButton:

<apex:commandButton value="View Statement"
                  action="{!openInvestmentStatement}" title="View Statement"
                  oncomplete="window.open('{!redirectURL}','_blank','height=800,width=600,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1)"/>

This opens a Visualforce PDF page and works fine. Problem is if there is no data (ie the PDF will be blank), how do I cancel the window.open?


Set a boolean value from your controller from which you can identify if the PDF page will remain blank or it will contain values.

And using that boolean value, call a javascript function from oncomplete event of commandButton, if the boolean is true, do not redirect to PDF page.

Changes in Apex Controller

public boolean isPDFBlank {get;set;}

public PageReference openInvestmentStatement(){
    //all other existing logic
    if(PDFPage is blank) //depending on your logic
        isPDFBlank = true;
        isPDFBlank = false;

Changes in visualforce page

function openPDFPage(){
    if({!isPDFBlank} == true){
        alert('PDF page is blank')
    } else {
        window.open('{!redirectURL}','_blank','height=800,width=600,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1);

<apex:commandButton value="View Statement"
                  action="{!openInvestmentStatement}" title="View Statement"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.