1

I have to download the file to system when an API is occur i am using below code may i know how to change my code to for download functionality.

@RestResource(urlMapping='/getPDF/*')
 Global class GETPDF{
 @HttpGet
global static void sendEmail() {
  GeneratePDFController  c = new GeneratePDFController();
  PageReference ref = Page.AccountDetails;
    Blob b = ref.getContentAsPDF();
4
  • One simple solution is you can send this pdf to email. SF natively don't provide support for download file. And this is your Web service so i don't think you can use browser for this. Nov 22, 2015 at 10:37
  • 1
    You can try return pdf as Blob or convert it to Base64 string.
    – Gres
    Nov 22, 2015 at 18:20
  • Gres can you please provide me the sample code how to edit my controller
    – Sathya
    Nov 22, 2015 at 18:24
  • @Gres Imporatant factor is content type here. setting content type to application/octet-stream makes that file downloadable and setting content-disposition with filename gives a name to the file. Nov 22, 2015 at 18:42

1 Answer 1

2

@Sathya you can do by below way:

@RestResource(urlMapping='/FileDownloadService/*')
global with sharing class FileDownloadService {
      @HttpGet
      global static void getResponse(){
         RestContext.response.addHeader('Content-Type', 'application/octet-stream');
         RestContext.response.addHeader('Content-Disposition', 'attachment; filename="kiran_machhewarpdf.pdf"');
         PageReference ref = Page.PdfPage;
         Blob pdfBlob = ref.getContentAsPDF();
         RestContext.response.responseBody = pdfBlob;
      }

}

Page Code: enter image description here I have tested this by exposing this on site. https://kmforce-developer-edition.ap1.force.com/services/apexrest/FileDownloadService You can check by using above endpoint. As a common practice mark this as solution if this helps you.

9
  • can you plz post your PDF page code
    – Sathya
    Nov 22, 2015 at 18:51
  • @Sathya. There is nothing much in page. In my solution I used renderAs pdf but it will be fine to use normal page getContentAsPDF to get blob. I have updated answer with page code as you asked. I am having very hard time in putting code in Answers. For proper formatting I need to recode here. Thats why I just provided image of page code. Hope this helps you. Nov 22, 2015 at 18:58
  • i tested this service from workbench in that case pdf is not downloading ..
    – Sathya
    Nov 22, 2015 at 19:17
  • Can you provide any sanpshot about the way you are doing its testing. Also post sample code of your case page and rest service. This helps us to help you. Nov 22, 2015 at 19:20
  • 1
    @Sathya, As discussed in chat, this will work for downloading files. In order to test the same from other end. I have written a java code which is able to download such file. Here is the code--> gist.github.com/kiran-machhewar/… Nov 23, 2015 at 6:09

You must log in to answer this question.

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