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
Gautam_KasukhelaGautam_Kasukhela 

Generating a PDF Document from an Apex Web service

Hello All,
Is it possible to generate a PDF document from an apex web service?
My requirement is as follows:
The user wants to download a PDF document in the front end portal (front end being built by a third party). On click of the button, a Web service will be invoked to fetch the data from Salesforce. The response of the web service is a JSON response. The front end does not use a VF page to use renderAs() function.

Is there any way in which I can send this response as a PDF for the portal user to view it as a PDF when he clicks on the buton. The PDF needs to be generated dynamically every time this button is clicked and I do not want to store the PDF data in the attachement object as this may lead to creation of 'n' number of files for a single functionality.
 
Darshan FarswanDarshan Farswan
Hi Gautam,

I guess this is totally possible to send a pdf as response to the web-service call. You can do so by adding 
 
response.addHeader('Content-Type','application/pdf')

But make sure that, you can only send one type of data as response. You wont be able to send the JSON response once you do this.

For dynamic generation of the PDF, you can use methods like getContentAsPdf() and send the resultant pdf to the end user.
 
Thanks
Darshan