0

Rendering VF page as PDF. Attachment pdf is working fine in classic. On opening pdf in classic view PDF is correctly opening but when i switch to lightning then on click of button pdf is saved in salesforce files but on opening the same pdf it is giving error "Failed to reload PDF". What could be the issue.

public PageReference saveDSRPdf() {
    filename = 'OCS_'+dsr.PO_Number__c+'_'+dsr.Account__r.Name+'.pdf';    
          system.debug('value of filename :::: '+filename);  
        PageReference pdf = Page.M2O_DSR_GeneratePDF;
        // add parent id to the parameters for standardcontroller
        parentId = dsr.Id;
        Blob body;
        pdf.getParameters().put('id',parentId);
        String themeType = UserInfo.getUITheme();
        // create the new attachment
              if(themeType!='Theme4d'){
        Attachment attach = new Attachment();

        // the contents of the attachment from the pdf


        try {

            // returns the output of the page as a PDF
            body = pdf.getContent();
            system.debug('value of body pdf ::::: '+body);
        // need to pass unit test -- current bug    
        } catch (VisualforceException e) {
            body = Blob.valueOf('Some Text');
        }

        attach.Body = body;
        // add the user entered name
        attach.Name = filename;
        attach.IsPrivate = false;
        // attach the pdf to the account
        attach.ParentId = parentId;
        insert attach;
              }else if(themeType=='Theme4d') {
              ContentVersion cont = new ContentVersion();
                //body = pdf.getContent();
            cont.Title = filename;
            cont.PathOnClient = fileName;
            cont.VersionData = pdf.getContentAsPdf();
            system.debug('value of versiond :::: '+cont.VersionData);

            insert cont;

            cont = [select id, ContentDocumentId from ContentVersion WHERE Id =: cont.Id];
        ContentDocumentLink cl = new ContentDocumentLink();
        cl.ContentDocumentId = cont.ContentDocumentId;
        cl.LinkedEntityId = parentId; 
        cl.ShareType = 'V';
        cl.Visibility = 'AllUsers';
        insert cl;
              }    
        }
        // send the user to the account to view results
        return new PageReference('/'+parentId);

      }
  • What do your debug logs tell you? If you open the PDF in a programmer's editor or hex viewer, what is its content? – David Reed Oct 19 '19 at 21:17
  • Content is code of VF page as well as controller code. Not sure why it is taking such detail instead of taking processed data which comes in pdf. There is no javascript involved on VF page as well. – Maik Floyd Oct 20 '19 at 16:37
  • I've previously found that directly calling getContentAsPdf() from a Lightning server controller method doesn't work. It's not clear whether or not that's exactly what's going on here. – David Reed Oct 21 '19 at 2:18
  • @DavidReed-It worked for me as well. i had to split transaction into two part.1st part is getting content from VF page rendered as PDF and another part calling a future method to insert salesforce files using content from 1st part. – Maik Floyd Oct 24 '19 at 7:39

Your Answer

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

Browse other questions tagged or ask your own question.