0

I have a requirement to open a pdf in a different tab in same window on click of a button on a visualforce page.Currently in the system - Chrome HTML Document (.pdf) is getting downloaded.

Current implementation of vf page 1(where sumit button is placed) is:-

  <apex:pageBlockButtons >   
            <apex:actionFunction name="openNewPage" action="{!GeneratePDFpage}" status="openNewPage"  rerender="panel,userMessages" />
            <apex:commandButton id="sbtn" value="submit" onclick="openNewPage(); " rerender="none"/> 
            
            <apex:outputPanel id="panel">
                <script type="text/javascript">
                    function popupnewwindow()
                    {
                        var url  = '{!urlFromApex}';
                        var newWin1 = window.open(url, 'url','height=80,width=600,left=100,resizable=yes,top=100,scrollbars=yes');
                        window.history.back();
                    }
                </script>
                <apex:actionStatus onstop="(popupnewwindow())" id="openNewPage" >
                </apex:actionStatus>            
            </apex:outputPanel> 
        </apex:pageBlockButtons> 

Apex class:-

//URL Gnerator method

    public void GeneratePDFpage()
       {    
       
                PageReference  pageRef;
                pageRef = Page.pdfVFPageToDisplay;
                pageRef.getParameters().put('Id', g_Id);
                pageRef.getParameters().put('inBRS', string.valueof(IsBRS));//parameter 1
                pageRef.getParameters().put('inSummary', string.valueof(IsSummary));//parameter 2
                urlFromApex =  pageRef.getUrl();// tried pageref.setredirect=true;

        }

Visualforce page(vf page 2- which opens on click of submit button page 1) for pdf:-

Header:-

    <apex:page standardController="Account" extensions="ControllerExtension" 
      showHeader="false" sidebar="false"
       applyhtmltag="false" standardstylesheets="false" renderAs="PDF" 
     contentType="application/pdf#{!Account.id}.pdf">
     </apex:page>

What have I tried:-

  1. Moving away from commanddbutton to commandlink to utilize target=blank attribute

  2. Using simple window.open() to avoid any pop up and open in a new tab.

  3. File Upload and Download Security - changed pdf and html to execute in browser from download

         <apex:actionFunction name="openNewPage" action="{!GeneratePDFpage}" status="openNewPage"  rerender="panel,userMessages" />
         <!--<apex:commandButton id="sbtn" value="submit" onclick="openNewPage(); " rerender="none"/> -->
             <apex:commandLink id="sbtn" styleClass="btn" style="text-decoration:none;padding:4px;" value="submit" onclick="openNewPage();" target="_blank"/>
       <script>
       function popupnewwindow()
                     {
                         var url  = '{!urlFromApex}';
                         window.open(url);//https://bearOrg--dev--c.sandbox.vf.force.com/apex/pdfVFPageToDisplay?Id=0010SALESFORCE&inBRS=true&inSummary=false
                         window.history.back();
                     }
        <apex:actionStatus onstop="(popupnewwindow())" id="openNewPage" >
                   </apex:actionStatus> 
    

What have I achieved:-

  1. pdf getting downloaded in seperate tab but NOT OPENING by default.
  2. Note :- vf page 1 remains open but as a standalone page not within app.

Where I need help:-

  1. Open vf page 2 i.e. pdf instead of downloading it in a new tab of same window.
  2. PDF should open standalone(preferred)/ within the app.

I have a similar requirement implemented where a vf page with renderAsPDF is called using window.open() from lwc. Here it opens in a new tab with pdf already open by default:-

 navigateToVFPage() {
    this[NavigationMixin.GenerateUrl]({
        type: 'standard__webPage',
        attributes: {
            url: `/apex/vfPagePDF?id=${this.recId}`
        }
    }).then(vfURL => {
    window.open(vfURL, "_blank");
   //vfURL = https://bearOrg--dev.sandbox.lightning.force.com/lightning/webpage/%2Fapex%2FvfPagePDF%3Fid%3D001oSALESFORCE%26lang%3Den-CA
    this.closeQuickAction();
    });

Note- A noticable difference in url getting generated from vf /lwc.

Links I have referred to solve this issue:- Doc link - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_renderas_pdf.htm

  1. vf page Selected Accounts show in different window as PDF
  2. open pdf in new tab
  3. Salesforce open PDF in new tab
  4. Open PDF in browser instead of Downloading
  5. https://stackoverflow.com/questions/4853898/display-pdf-within-web-browser
  6. Creating a VF that redirects to another URL
1
  • 2
    Not all browsers can view PDFs inline. Also, why not just show the Visualforce page in normal HTML form, and provide a download button to get a PDF version?
    – Phil W
    Jul 30, 2023 at 19:30

0

You must log in to answer this question.

Browse other questions tagged .