0

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

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()+'');

    }
}
  • Thanks! Spent a good chunk of my day researching this. Going to give it a try now. – Jesse Mar 15 at 20:11

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.