Generating PDFs in Salesforce is pretty straight forward using the standard Visualforce page functionality of renderAs=pdf. Attaching those PDFs to a chatter post and having them render correctly as PDFs can be a little tricky. Below I have created a very simple Visualforce page and controller to demonstrate this functionality. (Also this solution has been tested and works in the three major browsers: Chrome, Firefox and IE)
pdfGenerator Visualforce Page
pdfGenerator Visualforce Page
<apex:pagecontroller="pdfGenerator">
<apex:form>
<apex:outputPanel id="theOP">
<apex:pageBlock title="Chatter Account PDFs">
<apex:pageBlockSectioncolumns="1">
<apex:pageBlockSectionItem>
<apex:outputLabelvalue="PDF Name"/>
<apex:inputTextvalue="{!chatterFileName}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem>
<apex:outputLabelvalue="Chatter Message"/>
<apex:inputTextAreavalue="{!chatterMsg}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!showNewLink}">
<apex:outputLabel value="Chatter Link" style="color:green;" />
<apex:outputLink value="/{!newLink}" target="_blank">Chatter Post</apex:outputLink>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButtonvalue="Create PDF and Share to Chatter" action="{!share}" reRender="theOP"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>
pdfGenerator Controller
public with sharing class pdfGenerator {
public string chatterMsg {get;set;}
public string chatterFileName {get;set;}
public string newLink {get;set;}
public boolean showNewLink {get;set;}
public pdfGenerator() {
showNewLink = false;
}
public string queryAccount() {
string a = [SELECT Id, Name
FROM Account
WHERE Name = 'Test Account'].Id;
return a;
}
/* -------------------------
PDF Generate / and post to chatter
------------------------- */
public PageReference share() {
// create pdf
PageReference pdf = Page.accountPDF;
pdf.getParameters().put('accountId', queryAccount());
Blob body;
body = pdf.getContent();
// post to chatter
FeedItem post = newFeedItem();
post.ParentId = '0F9i0000000Cjqw'; // nickforce chatter group id
post.Body = chatterMsg;
post.Type = 'ContentPost';
post.ContentData = body;
//**you must include '.pdf' in the file name or it will not be recognized as a pdf file
post.ContentFileName = chatterFileName + '.pdf';
insert post;
newLink = post.Id;
showNewLink = true;
return null;
}
}
And here is the very simple Visualforce page rendered as a PDF.
PDF Visualforce Page
<apex:page standardController="Account" renderAs="pdf">
<h1>Account: {!Account.Name}</h1>
</apex:page>
Thanks for the feedback! I'm glad it was helpful for you
ReplyDelete