I blogged the other day about how to dynamically choose whether a Visualforce page is rendered as a PDF or not-useful when creating invoices and so on. Now I’m going to email that page using a little Apex code.
First, let’s define the page:
<apex:page renderAs="{!if($CurrentPage.parameters.p == null, null, 'pdf')}"
controller="MyController">
<apex:pageBlock title="My Dual-Rendering Invoice">
<apex:pageBlockSection title="Section 1"> Text </apex:pageBlockSection>
<apex:pageBlockSection title="Section 2"> Text </apex:pageBlockSection>
</apex:pageBlock>
<apex:form >
<apex:commandLink rendered="{!$CurrentPage.parameters.p == null}" value="PDF"
action="{!deliverAsPDF}" ></apex:commandLink>
</apex:form>
</apex:page>
This is a refinement of the one I blogged last time, using some tips from Doug Chasman. Note that the outputlink won’t render in the final PDF – cool! The only key thing here is that the page will render as a PDF if a parameter “p” is passed in. Otherwise it renders as HTML.
The question is how to write our getDeliverAsPDF()
method to create the PDF and mail it off in an email. Here’s how I coded the controller to do this:
public class MyController { public PageReference getDeliverAsPDF() {
// Reference the page, pass in a parameter to force PDF
PageReference pdf = Page.foo;
pdf.getParameters().put('p','p');
pdf.setRedirect(true);
// Grab it!
Blob b = pdf.getContent();
// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('From getDeliverAsPDF!');
String [] toAddresses = new String[] {'foobar@youremailaddressz.com'};
email.setToAddresses(toAddresses);
email.setPlainTextBody('Here is the body of the email');
// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
efa.setBody(b); //attach the PDF
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// send it, ignoring any errors (bad!)
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}}
The only tricky bit here is the pdf.GetContent()
method, which grabs the rendered PDF content. I then stuff it into an email attachment and send it off!