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
Sanjay Vinayak TSanjay Vinayak T 

How to generate Pdf on clicking a button in Visual force page.

Hi All,

I'm trying to create a visual force page to enter vendor details and perform actions based on the details.

But I'm unable to generate Pdf upon clicking on the Botton on the Visual force page.
and need to validate some conditions to perform the action, but unable to validate.
Validation conditions are:
1. If the amount is blank, cannot click on the "Send Email" button.
2. Generating a PDF is not possible when the city and Country are not mentioned. 

Below are my Visual force page code and Controller code:
<apex:page standardController = "Vendors_Detail__c" extensions="Vendorhandler">
        <!-- Javascript -->
    <script type="text/javascript">

    function validate()
    {
        if(document.getElementById('{!$Component.form.pb.pbs.cname}').value == '')
        {
            alert("Vendor name is mandatory");
        }
        if(document.getElementById('{!$Component.form.pb.pbs.amtevent}').value == '')
        {
            document.getElementById('{!$Component.form.pb.pbs.save}').disabled = true;
        }
        if(document.getElementById('{!$Component.form.pb.pbs.city}').value == '' && 
           document.getElementById('{!$Component.form.pb.pbs.country}').value == '')
        {
           document.getElementById('{!$Component.form.pb.pbs.pdf}').disabled = true;
        }
    }
   
    </script>
<!-- Javascript -->
    
    <apex:form id="form">
    <apex:pageBlock id="pb">
        
    <apex:pageBlockButtons > 
        <apex:commandButton id="save" action="{!save}" value="Send Email" onclick="validate();"/>
        <apex:commandButton id="reset" action="{!reset}" value="Reset" onclick="validate();" ></apex:commandButton>
        <apex:commandButton id="pdf" action="{!generatePDF}" value="Generate PDF" onclick="validate();" />

    </apex:pageBlockButtons>
        
      <apex:pageBlockSection id="pbs" title="Vendor Registration Form" columns="2">
          
          <apex:inputText id="cname" value="{!vendor.Vendor_Company_Name__c}" label="Vendor Company Name" required="true"/>
          <apex:inputText id="cperson" value="{!vendor.Vendor_Contact_Person__c}" label="Vendor Contact Person"/>
          <apex:inputText id="amtevent" value="{!vendor.Amount_Per_Event__c}" label="Amount Per Event"/>
          <apex:inputText id="city" value="{!vendor.City__c}" label="City"/>
          <apex:inputText id="country" value="{!vendor.Country__c}" label="Country"/>
   
     </apex:pageBlockSection>
     </apex:pageBlock>
    </apex:form>
</apex:page>
public class Vendorhandler {
    public Vendors_Detail__c vendor {get; set;}
    
    public Vendorhandler(ApexPages.StandardController std) {
        this.vendor = (Vendors_Detail__c) std.getRecord();
    }   

    public PageReference save(){
        
        insert vendor;
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'sasanjayt@gmail.com'};
        mail.setToAddresses(toAddresses);
        String emailSub = 'Vendor Registration Details';
                mail.setSubject(emailSub);
        String content = 'Vendor Company name = ' + vendor.Vendor_Company_Name__c + ',<br/><br/>'+ 
                              ' Vendor Contact person = ' + vendor.Vendor_Contact_Person__c + ',<br/><br/>' + 
                              'Amount per Event = ' + vendor.Amount_Per_Event__c + ',<br/><br/>' + 
                              'City = '+ vendor.City__c + ',<br/><br/>'+
                              'Country = '+ vendor.Country__c + '.<br/><br/>'+
                              '<br/><br/>Thank you <br/><br/>';
        mail.setHtmlBody(content);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
        PageReference pr = new PageReference(System.currentPageReference().getURL());
        return pr;
        
    }
    public PageReference reset() {
        PageReference newpage = new PageReference(System.currentPageReference().getURL());
        newpage.getParameters().clear();
        newpage.setRedirect(true);
        return newpage;
    }
    
     public PageReference generatePDF(){
         PageReference newpage = new PageReference(System.currentPageReference().getURL());
        Vendors_Detail__c v = new Vendors_Detail__c();
         v.Vendor_Company_Name__c = vendor.Vendor_Company_Name__c;
         v.Vendor_Contact_Person__c = vendor.Vendor_Contact_Person__c;
         v.Amount_Per_Event__c = vendor.Amount_Per_Event__c;
         v.City__c = vendor.City__c;
         v.Country__c = vendor.Country__c;
         return newpage;
         
    }
    
}

Tried adding renderAs= "pdf" but visual force will be rendered as pdf and unable to enter the values.
User-added image

Kindly help me in fixing the code to achieve the desired output.

Thanks in advance.

Regards,
Sanjay Vinayak T