I am working on this requirement. I have a SaveAction() pagereference method in my controller on the Button click from the Input VF Page, where I am saving the VF page's input field values to an opportunity.
After the opportunity insert
, I am calling another pagereference method within this SaveAction() to generate a PDF (from renderAs = "pdf"
) from a second VF page, i.e. PDFViewer. Here I am just referencing the fields values that are set in the actual VF page. But, every time, I get an empty valued initial VF page, but not the rendered as PDF page? Am I am missing something?
public PageReference SaveAction() {
//insert Opp based on the values from the Input VF page.
generatePDF();
return null;
}
public PageReference generatePDF(){
List<Attachment> attList = new List<Attachment>();
PageReference pr = Page.PDFViewer;
Blob pdfAsBody;
if (!Test.isRunningTest()) {
pdfAsBody = pr.getContentAsPDF();
} else {
pdfAsBody = Blob.valueOf('Dummy data');
}
Attachment att = new Attachment();
att.Body = pdfAsBody;
att.parentId = opp.Id;
att.Name = 'attachment.pdf';
attList.add(att);
//Inserting the attachment.
if(!attList.IsEmpty()){
insert attList;
}
return pr;
}
pr.getParametersMap.put('Id', someRecord.Id);