+ Start a Discussion

Saving PDF obtained via callout as an Attachment?

Having figured that I cannot save a PDF retrieved from an external webservice as an Attachment in SFDC (callouts do not support binary), was wondering what workarounds or other options are available?


All I was attempting was:



Http h = new Http();
HTTPRequest req = new HttpRequest();       
HTTPResponse resp = h.send(req);
String body = resp.getBody();
Attachment attach = new Attachment();
try {
    attach.Body = Blob.valueOf(body); 
    attach.Name = 'PDF Quote';
    attach.ParentId = this.contact.Id;
    attach.ContentType = resp.getHeader('Content-Type');
    attach.IsPrivate = false;			
    insert attach;
} catch (DMLException e) {

 All works fine. The attachment is saved. But, is never readable as a valid PDF. Any suggestions? Thanks.



I'd imagine that either the HTTP body contains an encoded form of the PDF rather than the PDF itself, or that when the page body is returned as a String, some of the binary type data is lost during conversion.  When dealing with PDFs created from Visualforce pages, the data is treated as a Blob all the way through the process so I'm not sure what using it as a String would mean.




Any workarounds for this?


Hasn't anybody had the same requirements? How did you work around this issue? The kludge I'll have to pursue is to return all related data to SFDC and then use that to basically "build from scratch" a clone of the required PDF doc using VF's renderAsPDF feature. :-(

Suman GiriSuman Giri

Any solution for the above problem, is ti resolved ?
We have a similar requirement and the Response Header what we get is:

Date: Wed, 16 May 2012 09:37:02 GMT
Content-Encoding: gzip
Transfer-Encoding: chunked
Content-Disposition: attachment; filename="XYZ.pdf"
Server: Apache-Coyote/1.1
Vary: Accept-Encoding


What I understand from the above headers is that I could see that the "Content-Encoding" is set as gzip, so instead of the PDF as an attachment, we would get the encoded stream of data, which needs to be decoded or unzip in my case and then read the attachment.
Let me know if there is a way to get this done in Apex HTTPResponse processing ?
Appreciate any pointers.