You need to sign in to do that
Don't have an account?
Vivek Deshmane
Issue on Apex callout with .NET web service that returns PDF file byte stream
Through apex call out i am getting following response from .Net Webservice . How I will render/create as attachment in salesforce as well formed PDF file based on below response.
JVBERi0xLjQNCiVET0MxIFJlc291cmNlR1VJRD01ODU4REFCNUQzQUY0RjY2QjU2Mzc5Mzg5NjI0NDYxMg0KJeLj5A0KMSAwIG9iag0KPDwgL1R5cGUgL1BhZ2UNCi9QYXJlbnQgMyAwIFINCi9SZXNvdXJjZXMgNCAw........
Please help .
JVBERi0xLjQNCiVET0MxIFJlc291cmNlR1VJRD01ODU4REFCNUQzQUY0RjY2QjU2Mzc5Mzg5NjI0NDYxMg0KJeLj5A0KMSAwIG9iag0KPDwgL1R5cGUgL1BhZ2UNCi9QYXJlbnQgMyAwIFINCi9SZXNvdXJjZXMgNCAw........
Please help .
After doing lots of brainstroming, Finally, I have got sucess to render pdf byte stream reponse in salesforce using apex callout.
Step 1:- Apex callout
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(endPoint);
req.setbody(body);
req.setMethod('POST');
HttpResponse resp = http.send(req);
//get Response body and parse it.
Dom.Document doc = resp.getBodyDocument();
//Retrieve the root element for this document.
Dom.XMLNode transactionStatusDetails = doc.getRootElement();
// Loop through the child elements.
for(Dom.XMLNode child : transactionStatusDetails.getChildElements()) {
System.debug('@@@@@'+child.getName());
if(child.getName() =='PDFSummaryGetResponse'){
Attachment attach = new Attachment();
attach.Body = EncodingUtil.Base64Decode(child.getText());
attach.Name = 'Invoice.pdf';
attach.ParentId = 'a3if0000000Csrd';
attach.ContentType = 'application/pdf';
insert attach;
}
}
2.Conclusion
To render PDF byte stream response from Dont net as PDF in Salesforce first we have to insert it as attachment with help of EncodingUtil.Base64Decode() as Body content and display it on Page like .
pageReference pr= new pageReference('/servlet/servlet.FileDownload?file='+attach.id);
All Answers
After doing lots of brainstroming, Finally, I have got sucess to render pdf byte stream reponse in salesforce using apex callout.
Step 1:- Apex callout
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(endPoint);
req.setbody(body);
req.setMethod('POST');
HttpResponse resp = http.send(req);
//get Response body and parse it.
Dom.Document doc = resp.getBodyDocument();
//Retrieve the root element for this document.
Dom.XMLNode transactionStatusDetails = doc.getRootElement();
// Loop through the child elements.
for(Dom.XMLNode child : transactionStatusDetails.getChildElements()) {
System.debug('@@@@@'+child.getName());
if(child.getName() =='PDFSummaryGetResponse'){
Attachment attach = new Attachment();
attach.Body = EncodingUtil.Base64Decode(child.getText());
attach.Name = 'Invoice.pdf';
attach.ParentId = 'a3if0000000Csrd';
attach.ContentType = 'application/pdf';
insert attach;
}
}
2.Conclusion
To render PDF byte stream response from Dont net as PDF in Salesforce first we have to insert it as attachment with help of EncodingUtil.Base64Decode() as Body content and display it on Page like .
pageReference pr= new pageReference('/servlet/servlet.FileDownload?file='+attach.id);
regards,
Amitabh