0

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;
}
  • you might need to specify a parameters for PDFViewer page, i.e. pr.getParametersMap.put('Id', someRecord.Id); – kurunve Sep 9 at 20:37
  • I am not passing any parameters to the PDFViewer page (URL/any that sort). Still, I tried your suggestion. I am getting the same issue. – Austin Evans Sep 9 at 20:45
0

If the desired action when clicking a button/link associated with action method SaveAction is to return to the user a PDF page:

public PageReference SaveAction() {
   //insert Opp based on the values from the Input VF page.
    PageReference pdfPage = generatePDF();
    pdfPage.setRedirect(true);
    pdfPage.getParameters().put('Id',someId);  // to tell controller the object
    return pdfPage;
}
  • Hi Cropredy, looks like I still have the same issue, i.e., Attachment is still the empty Input VF page. But, as we have setRedirect = true, I am getting the PDFviewer pdf page, but with empty fields. – Austin Evans Sep 11 at 17:56
  • @AustinEvans - well, when the PDF page is rendered with setRedirect(true), it reinstantiates the controller and gets the data it needs from the controller properties of the new controller object. So, you need to make sure that the data is refetched. This probably means you have to pass a URL parameter to tell the PDF page which object to fetch. – cropredy Sep 11 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.