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);
}
}