1

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 }" 
                  value="leads-List.pdf"/>
</apex:commandLink>

VF Page

<apex:page standardController="lead" 
           showHeader="false" 
           id="partnersPage" 
           renderAs="{!renderingService}" 
           recordSetVar="myrecords" 
           extensions="customCtrl,SaveAsPdfExtension"
           applyBodyTag="false">
  <!-- Begin Default Content REMOVE THIS -->
  <head>
      <style>
          @page {
                size: A4;
                margin: 25mm;
                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages);
                }
            }
      </style>
  </head>

  <body>

      <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" 
                                       action="/apex/dashboard"/>
                   <apex:commandLink target="_blank" 
                                     action="{! saveToPdf }" 
                                     value="Save as PDF">
                       <apex:param assignTo="{! renderedFileName }" 
                                   value="leads-List.pdf"/>
                   </apex:commandLink>
              </apex:pageBlockSection>

              <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:pageBlockTable>
              <apex:pagemessages id="msg" />     
          </apex:pageBlock>


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


               </apex:pageBlockTable>
               <apex:pagemessages id="msg2" />

          </apex:pageBlock>


      </apex:form>
    </apex:outputPanel>
  </body>
  <!-- End Default Content REMOVE THIS -->
</apex:page>

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 { 
        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);
    }
}
1
  • 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

1

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.

0

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 .