function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
arti rathodarti rathod 

vf attchmenet via apex not working for community guest user salesforce

This is my below code  on insertion of lead record community guest user unable to open pdf attchment although from backend when i am passing lead id its working fine --
public static void sendEmail(Id leadId,String leadStatus){
        List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
        EmailTemplate emailTemp = new EmailTemplate();
        String emailTempDevName;
        if(leadStatus == System.Label.PartExp_LeadStatusOpen){
            emailTempDevName= System.Label.PartExp_EmailTempName;
        }else if(leadStatus == System.Label.PartExp_LeadStatusRejected){
            emailTempDevName= System.Label.PartExp_RejectedEmailTempName;
        }
        emailTemp =[Select id, Subject, Body From EmailTemplate Where DeveloperName =:emailTempDevName];
       OrgWideEmailAddress orgWideEmailAddress = [SELECT Id,DisplayName FROM OrgWideEmailAddress WHERE DisplayName =: System.Label.PartExp_OWDForPartnerEmail Limit 1];
       
    Lead leadRecord = [SELECT Id, FirstName, LastName, Email FROM Lead WHERE Id = :leadId  Limit 1];
       // PageReference customPage = Page.PartExp_PrintViewRegistrationForm;
        PageReference customPage = new pagereference('/apex/PartExp_PrintViewRegistrationForm?id='+leadId);
        
        customPage.getParameters().put('id', leadRecord.Id);
        System.debug('leadId:'+leadId);
        
        Blob pdfBlob;
       
        try {
            pdfBlob = customPage.getContentAsPDF();
             System.debug('pdfBlob'+pdfBlob);
        } catch (VisualforceException e) {
             pdfBlob = Blob.valueOf('data');
             System.debug('error dedatil' + e.getStackTraceString());
           System.debug('Error generating PDF: ' + e.getMessage());
          // return;
           }
        
      
         Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
         attach.setFileName('LeadDetails.pdf');
         attach.setBody(pdfBlob);
         attach.Body = pdfBlob;
         System.debug('attach#'+attach);
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         mail.setTemplateId(emailTemp.Id);
         mail.setTargetObjectId(leadId);
       mail.setOrgWideEmailAddressId(orgWideEmailAddress.Id);
         mail.setFileAttachments(new List<Messaging.EmailFileAttachment>{ attach });
         emailList.add(mail);
        
        if(!emailList.isEmpty()){
            Messaging.sendEmail(emailList);
        }
AshwiniAshwini (Salesforce Developers) 
Hi Arti,
Make sure that the guest user has all the necessary permissions to access and create attachments.

You can refer to below stackexchange link which can help you:
https://salesforce.stackexchange.com/questions/210009/create-a-pdf-attachment-when-you-are-a-guest-user

If this information helps, please mark the answer as best. Thank you