Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arjun00231/9421c8a9b477cd6525dea774701ab9af to your computer and use it in GitHub Desktop.
Save arjun00231/9421c8a9b477cd6525dea774701ab9af to your computer and use it in GitHub Desktop.
Create a pdf of vf page and attach to the record.
public PageReference generatePdf() {
//PageReference pdf = new PageReference('/apex/yourPageName');
//Page.yourPageName;
PageReference targetPage = Page.yourPageName;
targetPage.getParameters().put('id',objOpp.Id);
//pdf.setRedirect(true);
system.debug('<<<<<<<pdf : '+targetPage);
Attachment attach = new Attachment();
Blob targetBody;
// try {
targetBody = targetPage.getContentAsPDF();
// } catch ( VisualforceException exp ) {
// targetBody = Blob.valueof('There was an error.');
// }
attach.Body = targetBody;
attach.ContentType = 'application/pdf';
attach.Name = 'Final quote- '+objOpp.Name +' '+String.valueof(Datetime.now())+'.pdf';
attach.IsPrivate = false;
attach.ParentId = objOpp.Id;
insert attach;
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment