I'm using code to save page As PDF, with custom save button. but what If I need more than one save button in the same page and each button will save part of the page?? How can I handle that issue ?

Save as PDF link

<apex:commandLink target="_blank" 
                  action="{! saveToPdf }" 
                  value="Save as PDF">
      <apex:param assignTo="{! renderedFileName }" 

VF Page

<apex:page standardController="lead" 
  <!-- Begin Default Content REMOVE THIS -->
          @page {
                size: A4;
                margin: 25mm;
                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages);


      <apex:outputPanel id="mainpanel"> 
      <apex:form id="mainform" >
          <apex:pageBlock title="Partners List">

              <apex:pageBlockSection columns="1" >
                   <apex:commandButton immediate="true" 
                                       value="Back to Dashboard" 
                   <apex:commandLink target="_blank" 
                                     action="{! saveToPdf }" 
                                     value="Save as PDF">
                       <apex:param assignTo="{! renderedFileName }" 

              <apex:pageBlockTable value="{!myrecords}" var="pt" id="mid">   

                  <apex:column value="{!pt.Name}"/>
                  <apex:column value="{!pt.Phone__c}"/>
                  <apex:column value="{!pt.Company__c}"/>

              <apex:pagemessages id="msg" />     

             <apex:pageBlock id="another_list" 
                             title="another list">
                   <apex:commandLink target="_blank" 
                                     action="{! saveToPdf }" 
                                     value="Save as PDF">
                       <apex:param assignTo="{! renderedFileName }" 
                  <apex:pageBlockTable value="{!anotherList}" var="agr">
                  <apex:column headerValue="Name" value="{!agr.name}"/>
                  <apex:column headerValue="Phone" value="{!agr.Phone__c}"/>

               <apex:pagemessages id="msg2" />


  <!-- End Default Content REMOVE THIS -->

SaveAsPdfExtension Apex Class

public class SaveAsPdfExtension {

    public SaveAsPdfExtension(ApexPages.StandardSetController 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 { 
        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
                        'content-disposition', 'attachment; filename=' + 

            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.
  • 1
    Creating different VF's pages and then combining all of them on one page. Commented Feb 24, 2016 at 10:47

2 Answers 2


I think use of render attribute & set it to false on save button click on blocks that you don't want in pdf & viceversa.

I hope this idea will resolve your issue.


Use visualforce components- write you individual blocks you want to save as pdf in a separate components and use all those components in single VF page. here you can print whole page or particular block you need.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .