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

Sending pdf in web service

We are rendering a page as the pdf in visualforce.


As a part of an interface, this pdf needs to be sent a response to a web service. 


Any pointers on how this can be done?


are there any specific restrictions or implementation considerations that need to be thought about before implementing the interface?




Thanks a lot,


David VPDavid VP

Experts on the subject please correct me if I'm wrong but if you're talking about a SOAP webservice then you'd need support for SOAP Attachments ( Salesforce does not have that at this point in time.


What you could do is expose your pdf via VF and Sites (make sure you put in a security check with a generated key or something) and send the link + key over with the webservice. The receiving end can then pull in the document as needed.





No need for SOAP attachments, your apex web service can return binary data in base64 format like the regular api does, see the Blob type in apex. 


Thanks a lot for the quick response.


Can serialization help in any way for sending the pdf in the web service?



How will the .NET client be able to access visualForce links? Can you refer me to links that explain how VF can be exposed outside We need to implement a batch printing functionality. The .NET webservice sends in a list of 50 objectIds and we need to send back 50 pdfs to them. .NET client will direct it to the printer.


Each pdf is about 60KB. So we should be within the limits of the response size. But do you see other problems with this approach? I am not sure how we can save the VF page rendered as the pdf and send it in any way to a web service.





You can save your VF page rendered as PDF in the attachements, which stores them as blob object. You may then send the attachements  based on its parent id.



Whenever i search for saving VF rendered as PDF In the attachments, I am getting examples of saving it as email attachments. Will the same logic work for storing it as attachments on the object.  


The following function will be called in a loop for different quoteIds.

        public blob getpdfContent(Id quoteId)

            PageReference pdf =  Page.attachmentPDF; 


            Blob b = pdf.getContent();
            return b;

AttachmentPdf is associated to the custom controller which has the logic for rendering it as a pdf.


Also, does the same rule apply for sending word documents as responses to web services?




Thanks a lot. Your responses have been tremendous help






public PageReference attach_Your_Document(Id RecordId) { // Get the page definition PageReference pdfPage = Page.PageNameOftheVisuaforcePageRenderedAsPDF; // set the Record id on the page definition (To execute the vf page with the recordId pdfPage.getParameters().put('id',RecordId); // generate the pdf blob Blob pdfBlob = pdfPage.getContent(); // create the attachment against the quote Attachment a = new Attachment(parentId = id, name=name + '.pdf', body = pdfBlob); // insert the attachment insert a; return null; }

This is a sample code to attach documents for a record Id


Thanks a lot for the response.


I was able to add an attachment using the code you gave.


However when I return a blob in a web service and test it using SOAP UI I get the following error


'The child of the Envelope element must be either a Header or Body element'


The web service is as follows:

global class pdfAttachmentservice
webservice string quoteId;

webservice static blob returnPDF(String quoteId)
// Get the page definition
PageReference pdfPage = Page.InsourceSoftwareAndServicesOrderForm;

// set the Record id on the page definition (To execute the vf page with the recordId

// generate the pdf blob
Blob pdfBlob = pdfPage.getContent();
return pdfBlob;


Please advise.





my bad! I was sending the XML request incorrectly.


I get a response back which appears gibberish.


But I guess thats how base 64 encoded result will look like.






Do you know if it's possible to read PDF returned from the external webService, within Salesforce ?
Naresh Krishna.ax1176Naresh Krishna.ax1176

Hi All,


I have to do same thing (read PDF returned from the external webService, within Salesforce). Is there any solution ?






Did you got any solution for the problem? Can you please share it.