1

I have a webservice class 'restExample' which first performs DML operations and then calls a future method 'createDocument()'. This method will create PDF document. One important point: Running user is a guest user with user license Guest License

restExample:

@RestResource(urlMapping='/restExample')
global class MyRestContextExample{

@HttpPost
global static void getPara () {

   //performs DML operations on multiple salesforce objects.
   simulate.createDocument(); 
   //'simulate' is the class name and 'createDocument' is the future method.
}

Reason behind using future method is : createDocument() will also perform a callout which will use records that gets updated in restExample webservice class.

Future Class: simulate

public class simulate{

@future(callout=true)
public static void createDocument(){
  // code that generates PDF document.
}
}

A PDF document is getting successfully generated but it is throwing error when opened.

Error in chrome: enter image description here

Error in Adobe Reader

enter image description here

When I execute future method createDocument() from anonymous window in Developer Console , PDF gets generated correctly and also able to open properly. Hence the code in createDocument() method works correctly.

NOTE: I am aware of the fact that I am not providing lot of code in the question. Three Reasons for the this: 1) All the DML operations are happening correctly and future method gets called in a seperate context. 2)Future method IS INFACT creating PDF. 3) PDF generation is happening with managed class,I just call that class in future method and code in managed class is hidden ..

  • @SantanuBoral Are you referring to callout to restExample webservice OR the callout performed in future method. – devforce May 15 '18 at 8:33
  • Are you performing this action from visualforce? anyone of above – Santanu Boral May 15 '18 at 8:35
  • Yes, PDF does get generated through visualforce. I will not be able to post that code since that particular VF page is a part of managed package. Also, Since I am able to generate PDF properly through anonymous window in Developer Console, I am assuming VF is working correctly. – devforce May 15 '18 at 8:38
  • Is that other callout is generated from visualforce? – Santanu Boral May 15 '18 at 8:41
  • Yes, that is correct – devforce May 15 '18 at 8:42
1

Managed package class is using getContentAsPDF to generate the PDF, am sorry to say it will always be blank/corrupted in guest user context.

The reason being, In the managed package they have used this below code to generate PDF

PageReference ref = Page.MyPage;
 Blob b = ref.getContentAsPDF();

 Insert new Attachment(ParentId='0069E00000790ok',body=b,Name='my.pdf');

For PDF to generate in guest user context following conditions should be met for Valid generation

  1. Pagereference should be made with full URL to VF page

    PageReference ref = new PageReference('https://mysite.cs88.force.com/MyPage');

  2. Your class should be without sharing.

  3. Guest User profile should have Access to VF Page and Apex Class used

  4. That VF Page should be Publicly accessible via guest site user(Public user)

That being said, if above conditions are met, you will be able to generate proper PDF, from Guest user profile, this would mean you have to ask you managed partner client to edit these changes for you.

Is there any other option other than asking manged package guys to edit code?

Yes there is, but it will get ugly (warning)

From your RestApi apex controller

  1. Do a Rest login call to get access token(session id) of Admin(Automated User). Using oauth2 Rest call to get access token of Admin User. You need to use Username and Password stored in custom setting/metadata. https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm
  2. Use Tooling API to call execute anonymous code, with sessionID received from step one. https://developer.salesforce.com/forums/?id=906F00000009FJ6IAM
  3. Once you do that tooling api rest call, it will be as good as running executeAnon in admin user context. You will get your PDF. Its sort of replicating session of ADMIN user in guest user context.

Source: getContentAsPDF menance for Guest user

| improve this answer | |
  • Thanks @Pranay. This does provide insights to the problem. I have queries on possible workarounds. As you have mentioned Pagereference should be made with full URL to VF page -- This should be done in VF page in managed package? – devforce May 15 '18 at 10:45
  • Also @Pranay, Can you provide some more details for any example for the workaround From your RestApi apex controller 1) Do a Rest login call to get access token(session id) of Admin(Automated User) 2) Use Tooling API to call execute anonymous code, with sessionID received from step one. 3) See pdf being generated – devforce May 15 '18 at 10:47
  • Hi I have updated the answer with Links , do let me know if you have any issues.. we can discuss more in chat. – Pranay Jaiswal May 15 '18 at 10:58
  • Hi @Pranay, Can we discuss more about the 2nd Solution . I am facing some issues in building that. Let me know if we can chat. – devforce May 16 '18 at 6:55
  • Lets chat here. chat.stackexchange.com/rooms/77561/… – Pranay Jaiswal May 16 '18 at 8: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.