Skip to content

Instantly share code, notes, and snippets.

@SeemaKurawale111
Last active December 2, 2016 09:00
Show Gist options
  • Save SeemaKurawale111/b3d14b1bb6872cdf539b20e25ef7c019 to your computer and use it in GitHub Desktop.
Save SeemaKurawale111/b3d14b1bb6872cdf539b20e25ef7c019 to your computer and use it in GitHub Desktop.
Generate PDF attachment of opprtunity details
<apex:page standardController="Opportunity" extensions="Pdf_of_Attachment_Extension" renderAs="pdf">
<apex:pageBlock >
<apex:pageBlockSection columns="1" >
<apex:pageBlockSectionItem >Opportunity Name: {!Opportunity.Name} </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >CloseDate : {!Opportunity.CloseDate} </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >StageName : {!Opportunity.StageName} </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >Probability : {!Opportunity.Probability} </apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:repeat var="attachment" value="{!attachments}">
<apex:image url="/servlet/servlet.FileDownload?file={!attachment.Id}"/><br></br>
</apex:repeat>
</apex:pageBlock>
</apex:page>
public with sharing class Pdf_of_Attachment_Extension
{
public String currentId {get;set;}
public Pdf_of_Attachment_Extension(ApexPages.StandardController controller)
{
currentId = ApexPages.currentPage().getParameters().get('id');
}
public List<Attachment> getattachments()
{
List<Attachment> attachmentList = [SELECT Id, Name, Body, ContentType
FROM Attachment
WHERE Parentid =: currentId];
system.debug(attachmentList);
return attachmentList;
}
public PageReference attachpdf(){
PageReference pdf = Page.Pdf_of_Attachment;
pdf.getParameters().put('id',currentId);
Attachment attach = new Attachment();
Blob body;
try {
body = pdf.getContent();
} catch (VisualforceException e) {
system.debug('Exception :'+e);
}
attach.Body = body;
attach.ContentType = 'application/pdf';
attach.Name = currentId +' Attachment';
attach.IsPrivate = false;
attach.ParentId = currentId ;
try{
insert attach;
} catch (DMLException e) {
system.debug(e);
}
PageReference pr = new pagereference('/'+currentId);
return pr;
}
}
<apex:page standardController="Opportunity" extensions="Pdf_of_Attachment_Extension" action="{!attachpdf}">
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment