+ Start a Discussion
The new LearnerThe new Learner 

How to download an attachment on click

Hi Experts,

I have requirment i need  download account related contact object records as pdf, i can able to make it as pdf, but , i want download once i click on that button,i am using this page as button source. can anyone help me.
 
<apex:page readOnly="true"     
           standardController="Account"    
           applyHtmlTag="false"     
           sidebar="false"     
           showHeader="false"     
           cache="true"     
           renderAs="advanced_pdf">    
        
    <head>    
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />    
        <style type="text/css">    
            
            @page {    
              
                size: A4 landscape;    
                border: 2px solid black;    
                padding-left: 8px;    
                padding-right: 8px;      
                  
            }    
                
            th {    
              
                text-align: center;    
                font-weight: bold;    
                  
            }    
                
                
            td {    
              
                text-align: center;    
                font-size: 14px;    
                  
            }    
                
        </style>    
          
    </head>    
      
    <center>    
      
        <h3>Testing Advanced PDF = {!Account.Name}</h3>    
          
    </center><br/><br/>    
      
    <table border="1" width="99%">    
      
        <tr>    
          
            <th>First Name</th>    
            <th>Last Name</th>    
              
        </tr>    
        <apex:repeat value="{!Account.Contacts}" var="con">    
          
            <tr>    
              
                <td>{!con.FirstName}</td>    
                <td>{!con.LastName}</td>    
                  
            </tr>    
              
        </apex:repeat>    
          
    </table>    
    
</apex:page>

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi there,

Have you tried checking the below documentation where there is an explanation to save as pdf functionality to vfpage:

> https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_output_pdf_print_as_pdf_button.htm

I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be used by others in the future.

Below is the code referd in above documentation
 
public class SaveAsPdfExtension {

    // Required extension constructor (empty, no-op)
    public SaveAsPDFExtension(ApexPages.StandardController controller) {}
    
    // Determines what kind of rendering to use for the page request
    public String renderingService { get; private set; }
    
    // Allow the page to set the PDF file name
    public String renderedFileName { 
        get; 
        set { renderedFileName = this.sanitizeFileName(value); }
    }

    // Rendered content MIME type, used to affect HTTP response
    public String renderedContentType {
        get {
            String renderedContentType = 'text/html'; // the default
            
            if( ! this.renderingAsHtml() ) {
                // Provides a MIME type for a PDF document 
                renderedContentType = 'application/pdf';
                
                // Add a file name for the PDF file
                if( this.renderedFileName != null) {
                    // This is supposed to set the file name, but it doesn't work
                    renderedContentType += '#' + this.renderedFileName;
                    
                    // This is a work-around to set the file name
                    ApexPages.currentPage().getHeaders().put(
                        'content-disposition', 'attachment; filename=' + 
                         this.renderedFileName);
                }
            }
            
            return renderedContentType;
        }
    }
    
    // Are we rendering to HTML or PDF?
    public Boolean renderingAsHtml() {
        return ( (renderingService == null) || 
                 ( ! renderingService.startsWith('PDF')) );
    }

    // Action method to save (or "print") to PDF
    public PageReference saveToPdf() {
        renderingService = 'PDF';
        return null;
    }
    
    // Private helper -- basic, conservative santization
    private String sanitizeFileName(String unsafeName) {
        String allowedCharacters = '0-9a-zA-Z-_.';
        String sanitizedName = 
            unsafeName.replaceAll('[^' + allowedCharacters + ']', '');
        // You might also want to check filename length, 
        // that the filename ends in '.pdf', etc.
        return(sanitizedName);
    }
}

 
The new LearnerThe new Learner
Hi Anutej, 

THanks for the reply,

is this not possible once , i click on that button, instead of link on the same record page
The new LearnerThe new Learner
HI AnuTej,

can you tell me how to design below table for all fields.

one complete row one field, after that another field.
 
<apex:page readOnly="true"     
           standardController="Account"    
           applyHtmlTag="false"     
           sidebar="false"     
           showHeader="false"     
           cache="true"     
           renderAs="advanced_pdf">    
        
    <head>    
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />    
        <style type="text/css">    
            
            @page {    
              
                size: A4 landscape;    
                border: 2px solid black;    
                padding-left: 8px;    
                padding-right: 8px;      
                  
            }    
                
            th {    
              
                text-align: center;    
                font-weight: bold;    
                  
            }    
                
                
            td {    
              
                text-align: center;    
                font-size: 14px;    
                  
            }    
                
        </style>    
          
    </head>    
      
    <center>    
      
        <h3>Testing Advanced PDF = {!Account.Name}</h3>    
          
    </center><br/><br/>    
      
    <table border="1" width="99%">    
      
        <tr>    
          
            <th>First Name</th>    
        
              
        </tr>    
        <apex:repeat value="{!Account.Contacts}" var="con">    
          
            <tr>    
              
                <td>{!con.FirstName}</td>    
             
                  
            </tr>    
              
        </apex:repeat>    
          
    </table>    
    
</apex:page>