Auto download a pdf rendered in Visualforce

37 Views Asked by At

In my org I have a VF page which when renders produce a PDF page. The VF is executed on clicking a custom button. The PDF is opening in a separte window. The code is as follows:

<apex:page standardController="Tour_Plan__c" extensions="PrintTourPlantController" renderAs="pdf" docType="html-5.0"  sidebar="false" showHeader="false" language="en-IN" standardStylesheets="false" >
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">__sfdcSessionId = '{!$Api.Session_Id}';</script>
    <link href="https://fonts.googleapis.com/css?family=Lato:400,700&subset=latin,latin-ext" rel="stylesheet" type="text/css" />
    <script src="/soap/ajax/9.0/connection.js"></script> 
    <style>
           @page {
                size: A4;
                margin:-1mm;

                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages);
                }
            }
            body{
                margin:0;
                padding:0;
            }
            
            .page-break {
                display:block;
                page-break-after:always;
            }
            #MainTable
            {
                margin-top:25px;
                font-family: Calibri, Candara, Segoe, 'Segoe UI', Optima, Arial, sans-serif;
                border:1px solid black;
            }
            tr,td
            {
                font-family: Calibri, Candara, Segoe, 'Segoe UI', Optima, Arial, sans-serif;
                border:1px solid black;
                
            }
            body 
            {
                font-family: Calibri, Candara, Segoe, 'Segoe UI', Optima, Arial, sans-serif;
                font-weight:normal; 
                font-size:13px; 
                mso-line-height-rule: exactly;
            }
            .infobox {
                  font-size: 80%;
            }
            .noBorder{
                 border:0px solid White;
            
            }
            p {
              padding: 30px;30px;30px;30px;
            }
    </style>
    <script>
            window.onload = function() {
                window.location.href = '/apex/printTourPlanPDF?Id={!Tour_Plan__c.Id}&isdtp=p1&action=print';
            }
    </script>
</head>
         <body width="100%">   
    
           <apex:form > 
                <table >
                <tr>
                <td>
              <p>
                <apex:image url="{!$Resource.agor_logo}" width="100px;" height="60px;"/>
              </p>
               
                </td>
                </tr> 
                </table>
                 <!-- <table id="MainTable"  cellspacing="0"  > -->
                 
                 <table id="MainTable" align="Center" cellspacing="0" style="width:80%">
                <tr>
                 <td  align="center" colspan="2" style="background:#9CA1A1;font-size:12px"><h2> Tour Plan </h2>
                 </td>
                 </tr>
                
                     </table>
                     <p></p>
                   <table style="width:100%">
                   <tr>
                       <td style="border-bottom: 1pt solid black; width:50%">Name:   <apex:outputText value="{!tourplan.Name__c}"/> </td>
                   <td style="border-bottom: 1pt solid black; width:50%"> Employee Id: {!tourplan.Employee_ID__c} </td>
                   </tr>
                   
                    <tr>
                   <td style="border-bottom: 1pt solid black; width:50%">Designation:   {!tourplan.Designation__c} </td>
                   <td style="border-bottom: 1pt solid black; width:50%"> H.Q: {!tourplan.H_Q__c}</td>
                   </tr>
                   
                   
                    <tr>
                   <td style="border-bottom: 1pt solid black; width:50%">Start Date:   {!Start_Date} </td>
                   <td style="border-bottom: 1pt solid black; width:50%"> End Date: {!End_Date} </td>
                   </tr>
                   
                    <tr>
                   <td style="border-bottom: 1pt solid black; width:50%">Submission Date:{!Submission_Date} </td>
                   <td style="border-bottom: 1pt solid black; width:50%"> Approval Date: {!Approval_Date} </td>
                   </tr>
                   
                    <tr>
                   <td style="border-bottom: 1pt solid black; width:50%">Tour No.:   {!tourplan.Name}</td>
                   <td style="border-bottom: 1pt solid black; width:50%"> Approval Status: {!tourplan.Approval_Status__c} </td>
                   </tr>
                   
                   
                   </table>
                    <table >
                   <p><b> Visit Details </b> </p>
                    <thead>
                <tr>
                       <th style="border-bottom: 1pt solid black;width:10%">Dealer/Lead</th>
                    <th style="border-bottom: 1pt solid black;width:10%">Planned Date</th>
                    <th style="border-bottom: 1pt solid black; width:10%">Visit Type</th>
                    <th style="border-bottom: 1pt solid black; width:10%">Check In Date</th>
                    <th style="border-bottom: 1pt solid black;width:10%">Purpose of Visit</th>
                    <th style="border-bottom: 1pt solid black;width:10%">Outcome</th>                    
               </tr>
            </thead>
             <apex:repeat value="{!visitlist}" var="a">
                      <tr>
                        <td style="border-bottom: 1pt solid black;font-size: 70%;">{!a.Name_of_Visit__c}</td>
                        <td style="border-bottom: 1pt solid black; font-size: 70%;"> 
                            <apex:outputText value="{0, date, MMMM d','  yyyy}">
                                <apex:param value="{!a.Planned_Date_2__c}" />
                            </apex:outputText>
                        </td>
                         <td style="border-bottom: 1pt solid black; font-size: 70%;">{!a.Tour_Stage__c}</td>
                         <td style="border-bottom: 1pt solid black; font-size: 70%;">{!a.CheckIn_CMV__Check_in_Date_Time__c}</td>
                         <td style="border-bottom: 1pt solid black; font-size: 70%;">{!a.Purpose_of_Visit_Fill_before_Tour__c}</td>
                         <td style="border-bottom: 1pt solid black; font-size: 70%;">{!a.Check_In_Comment__c}</td>
                    </tr>
                </apex:repeat>
                   </table>
                      </apex:form>
                     </body>
                     
</apex:page>

The Apex controller is as follows:

public class PrintTourPlantController {
    public string Start_Date {get;set;}
    public string End_Date {get;set;}
    public string Submission_Date {get;set;}
    public string Approval_Date {get;set;}

    public Tour_Plan__c tourplan{get;set;}
    
    public List<CheckIn_CMV__Visit__c> visitlist{get;set;}

    public String tourName;
    public String recordId{  get;  set; }

    public String expenseStatus{get; set;}
    public List <Expenses__c> allExpenseList{set;get;}
    public List <Expenses__c> conveyanceList{set;get;}
    public List <Expenses__c> nonconveyancelist{set;get;}

    public PrintTourPlantController(ApexPages.StandardController controller) {
        
        recordId = ApexPages.currentPage().getParameters().get('id');
        System.debug('Id - '+recordId);
        tourplan = new Tour_Plan__c();
        visitlist=new List<CheckIn_CMV__Visit__c>();
        conveyanceList=new List<Expenses__c>();
        nonconveyanceList=new List<Expenses__c>();
        // Initialize the StandardController to work with the Tour_Plan__c object
        ApexPages.StandardController stdController = controller;
        
        tourplan = [select Id,Name,OwnerId,Name__c,Start_Date__c,End_Date__c,Expenes_Status__c,Employee_ID__c,Approval_Date__c,Submission_Date__c,Designation__c,H_Q__c,Location__c,Approval_Status__c,Expenses_Comment__c,Sales_Manager_Expenses_Approved_Date__c,Finance_Expenses_Approved_Date__c,Expenses_submission_date__c,All_Total_Expenses_F__c,Expense_Total_Conveyance__c,Sum_Of_Hotel__c,Sum_Of_Non_Conveyance__c,Sum_of_DA_Amount__c,Sum_Of_Other__c,Reason_for_Cancellation__c,Reason_for_Changes__c  from Tour_Plan__c where Id=: recordId limit 1];
        System.debug('tour plan - '+tourplan);
        Datetime d = tourplan.Start_Date__c;
        if (d<> null)
        {Start_Date = DateTime.newInstance(d.year(),d.month(),d.day()).format('dd-MM-yyyy');}
        else if (d == null)
        {Start_Date = null;}
        d = tourplan.End_Date__c;
        if (d<> null)
        {End_Date = DateTime.newInstance(d.year(),d.month(),d.day()).format('dd-MM-yyyy');}
        else if (d == null)
        {End_Date = null;}
        d = tourplan.Submission_Date__c;
        if (d<> null)
        {Submission_Date = DateTime.newInstance(d.year(),d.month(),d.day()).format('dd-MM-yyyy');}
        else if (d == null)
        {Submission_Date =  null;}
        d = tourplan.Approval_Date__c;
        if (d<> null)
        {Approval_Date = DateTime.newInstance(d.year(),d.month(),d.day()).format('dd-MM-yyyy');}
        else if (d == null)
        {Approval_Date =  null;}
        
        visitlist= [select Id,Visit_Name__c,Name_of_Visit__c,Planned_Date_2__c,Planned_Tour_Day__c,Manager_Remarks__c,Purpose_of_Visit_Fill_before_Tour__c,Tour_Stage__c,Check_In_Comment__c,CheckIn_CMV__Check_in_Date_Time__c from CheckIn_CMV__Visit__c where Tour_Plans__c =: tourplan.Id AND OwnerId =: tourplan.OwnerId ORDER BY Planned_Date_2__c ASC];  
        
        allExpenseList = [SELECT Id,Name,Tour_Plans__c,Date_expenses__c,Tour_Location__c,Hotel__c,DA_Type__c,DA_Amount__c,Others__c,Total__c,Remarks__c,RecordTypeId,Total_Conveyance__c,Date__c,Town__c,Time__c,Arrival_Town__c,Arrival_Time__c,Fare_Rs__c,Class_and_Mode_of_Travel__c,Comments_2__c,Comments__c FROM Expenses__c WHERE Tour_Plans__c =: recordId  ORDER BY  Date_expenses__c ASC,Date__c ASC ];
        system.debug('checking visitlist=='+visitlist+'allExpenseList=='+allExpenseList);
        for(Expenses__c exp:allExpenseList){
            String recordtypename = Schema.SObjectType.Expenses__c.getRecordTypeInfosById().get(exp.RecordTypeId).getname();
            system.debug('Record type - '+recordtypename);
            system.debug('Expense - '+exp);
            if(recordtypename=='Non Conveyance'){
                nonconveyancelist.add(exp);
            }
            if(recordtypename=='Conveyance'){
                conveyancelist.add(exp);
            }
        }
        expenseStatus=tourplan.Expenes_Status__c;
    }
}

I want this PDF to get downloaded automatically on teh click of the custom button. How should I modify my code.

TIA.

0

There are 0 best solutions below