+ Start a Discussion
Arul Bernard I 14Arul Bernard I 14 

How to renderas pdf of attachment(Related List) in custom object and pass it dynamically in apex

I need to be done with the help of visualforce page?
Raj VakatiRaj Vakati
Yes .. You need to use VF Page render as .. after rendering the pdf you can use logic to store as attachments
 
public PageReference savePdf() {

// EXECUTE VF PDF PAGE (RENDERAS="PDF")
        PageReference pdf = Page.proof_pdf;

// CREATE PARENT OBJECT RECORD
        Proofs__c proofobj = new Proofs__c();
        proofobj.Status__c = 'Generated';
        proofobj.Category__c = categorySelected;
        insert proofobj;

// HOLD THE CATEGORY
        pdf.getParameters().put('cat',categorySelected);

// CREATE AN ATTACHMENT OBJECT RECORD
        Attachment attach = new Attachment();       

// GET THE VISUALFORCE PAGE IN A BLOB
Blob body;
        try {
            body = pdf.getContent();
        } catch (VisualforceException e) {
            body = Blob.valueOf('Error : ' + e);
        }

// ATTACH FILE TO ATTACHMENT OBJECT AND RENAME FILE
        attach.Body = body;
        attach.Name = Datetime.now().format('yyyy-MM-dd HH:mm') + ' ' + categorySelected + '.pdf';
        attach.IsPrivate = false;

// ADD TO PARENT OBJECT RECORD
        attach.ParentId = proofobj.Id;
        insert attach;
       
// TAKE USER TO PARENT RECORD
        return new PageReference('/'+proofobj.Id);
    }

 
Arul Bernard I 14Arul Bernard I 14

Hi Raj,

Thanks for your response, Can you please help with vf page coding too... As I'm beginner with it... Please help me out!!

Thanks in advance 

Raj VakatiRaj Vakati
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_renderas_pdf.htm


http://www.sfdcpoint.com/salesforce/visualforce-page-render-pdf/