3

The documentation suggests that a Visual Force page can easily be rendered with apex: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_output_pdf_render_in_apex.htm

        // Create PDF
        PageReference reportPage =
            (PageReference)this.reportPagesIndex.get(this.selectedReport);
        reportPage.getParameters().put('id', this.selectedAccount);
        Blob reportPdf;
        try {
            reportPdf = reportPage.getContentAsPDF();
        }
        catch (Exception e) {
            reportPdf = Blob.valueOf(e.getMessage());
        }

Is anyone aware of how something similar can be invoked with the Salesforce REST directly?

I can't seem to find anything when searching through the REST api explorer (/services/data/v45.0/sobjects)

1 Answer 1

5

There isn't one. This is a special feature built in to Apex specifically. That said, you could write your own custom REST endpoint to handle this (see @RestResource for details on writing a custom REST service).

You'd create a URL that would ultimately look like:

/services/apexrest/pdf/VisualforcePageName

In your Apex code, you'd parse out the Visualforce page name and any parameters, then call getContentAsPDF, and finally put the response Blob back to the client.


Sample service implementation:

@RestResource(urlMapping='/pdf/*')
global class q254085 {
    @HttpGet global static void process() {
        RestContext.response.responseBody = new PageReference('/apex/'+RestContext.request.requestUri.substringafterlast('/')).getContentAsPDF();
        RestContext.response.headers.put('Content-Type', 'application/pdf');
        RestContext.response.headers.put('Content-Length', RestContext.response.responseBody.size()+'');

    }
}
6
  • Thanks! Spent a good chunk of my day researching this. Going to give it a try now.
    – Jesse
    Mar 15, 2019 at 20:11
  • 1
    @sfdcfox I tried this in my org but got an error like 'Callout Loop exception'. I believe the getContentAsPdf is itself treated as a callout. So callout doing a callout again will give error. The issue I was trying to solve is to render vf page and get content as pdf on click of button of lightning component which is shown on community. With standalone lightning component, there is no need to use rest api to render but if we are using community then due to the diff base url it gives error.
    – Sukruti
    Apr 13, 2020 at 5:43
  • Nice... exactly what i needed. Thanks
    – dacology
    Jul 2, 2020 at 13:44
  • This hasn't worked for me. I get a html response with a javascript redirect to a login page. It appears the PageReference doesn't run as the same user session that's calling the @RestResource class. The Connected App scope settings say: "Allows access to customer-created Visualforce pages only. This scope doesn’t allow access to standard Salesforce UIs." Apr 22, 2022 at 20:42
  • @DaveEckblad This technique does indeed use the user session from the calling context. This situation usually occurs because the user doesn't have access to the requested page. For example, it might be because the Site doesn't have the page enabled, the user in context doesn't have access to that page on their profile/permission sets, etc. I'd suggest asking a new question and being as detailed as possible. I'd love to take a look at it with you if you need the help.
    – sfdcfox
    Apr 22, 2022 at 21:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .