0

I have a visualforce page that I can open from a lightning web component with using the following:

openPdf1() {
  this[NavigationMixin.GenerateUrl]({
      type: 'standard__webPage',
      attributes: {
          url: '/apex/GroupStructurePDF?aid=' + this.recordId + '&gn=' + this.selectedgroupnumber +  '&st=' + this.selectedstatus
      }
  }).then(generatedUrl => {
      window.open(generatedUrl);
  });
}

This works fine. I am trying to save this pdf to the record and email it. From the button on the LWC I call an apex method. Here is the code that tries to create the PDF:

   // Create Email Attachment
                        // Use web service to retrieve pdf as a blob
                    Blob pdf = GroupStructureCreateAttachment.CreateAttachment(
                       gsn.Account__c, gsn.Group_Number__c, gsn.Active2__c, 
                            gsn.Section_Code__c, gsn.Package_Code__c, gsn.Group_Structure_File_Sent__c);              
                // system.debug(LoggingLevel.INFO, 'Here is teh blob' + pdf);    
                /////3/4/21 = testing to attach the file

                    Messaging.EmailFileAttachment mailAttachment = new Messaging.EmailFileAttachment();
                    mailAttachment.setFileName(gsn.Group_Structure_File_Sent__c + ' Group Structures for ' + aName + '.pdf');
                    mailAttachment.setBody(pdf);

Here is the apex class, GroupStructureCreateAttachment.CreateAttachment:

global class GroupStructureCreateAttachment {
 @AuraEnabled webservice static Blob CreateAttachment(
    
    String aid, 
    String gn,
    String af,
    String sc,
    String pc,
    String st) {
        system.debug('!1 aid' + aid);
        system.debug('!2 gn' + gn);
        system.debug('!3 af' + af);
        system.debug('!4 sc' + sc);
        system.debug('!4 pc' + pc);
        system.debug('!5 st' + st);
        // Generate the Invoice as a pdf file by retrieving 
        // the PDF as a Blob from the Visualforce page 
    PageReference gsPDF = Page.GroupStructurePDF;
    gsPDF.getParameters().put('aid', aid); 
    gsPDF.getParameters().put('gn', gn); 
    gsPDF.getParameters().put('af', af); 
    gsPDF.getParameters().put('sc', sc); 
    gsPDF.getParameters().put('pc', pc); 
    gsPDF.getParameters().put('st', st); 

    gsPDF.setRedirect(true);
    system.debug(LoggingLevel.INFO, '2 gsPDF is here......' + gspdf.getContent()); 
        // This is required because the test code cannot
        // use the getContent function.
     Blob pdf = Blob.valueOf('Big Blob');
    if (!test.isRunningTest()) {
        pdf = gsPDF.getContent();
    }
    return pdf;
}

}

All of the system debug statements are getting populated as expected. I added a debug to the GroupStructurePDF class and I'm finding that the debug never runs. So it seems that I'm never really getting to the GroupStructurePDF page/class that is being rendered as a PDF. Is there something I need to set on the GroupStrucutrePDF page/class so that it can be reached from the LWC? Like I said above, this works when I just navigate directly from LWC to the VF page as a PDF. Just can't save it.

Thank you!!!

0

But Page.GroupStructurePDF returns a PageReference / Link to the VF page, not the rendered page, so the logic inside it is not executed.

You should call getContentAsPDF() on your PageReference or just generate the pdf in another way (HTML saved in a PDF type file) that does not depends on a VisualForce Page.

1
  • Thanks Desproposito! I will move to plan B. – Frank Apr 19 at 13:06

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.