Created
September 9, 2012 19:58
-
-
Save pfeilbr/3686866 to your computer and use it in GitHub Desktop.
Download PDF from Trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global class PDFDownloader { | |
// called from trigger | |
@future (callout=true) | |
public static void download(String sessionID, String attId) { | |
String vfEndpoint = 'https://c.cs10.visual.force.com/apex/PDFTest_mrk?id=' + EncodingUtil.urlEncode(attId, 'UTF-8'); | |
String vfSessionURL = 'https://cs10.salesforce.com/visualforce/session?url=' + EncodingUtil.urlEncode(vfEndpoint, 'UTF-8'); | |
Http http = new Http(); | |
HttpRequest req = new HttpRequest(); | |
HttpResponse res = null; | |
// request vf pdf page | |
req.setMethod('GET'); | |
req.setEndpoint(vfSessionURL); | |
req.setHeader('Cookie','sid=' + sessionID); | |
res = http.send(req); | |
// follow 302 redirect | |
String vfGetSIDURL = res.getHeader('Location'); | |
System.debug('vfGetSIDURL=' + vfGetSIDURL); | |
req.setEndpoint(vfGetSIDURL); | |
res = http.send(req); | |
// grab vf session id out of cookie | |
String cookieString = res.getHeader('Set-Cookie'); | |
String vfSID = cookieString.split('sid=')[1].split(';')[0]; | |
req.setEndpoint(vfEndpoint); | |
req.setHeader('Cookie','sid=' + vfSID); | |
res = http.send(req); | |
// extract pdf from body | |
Blob binaryPDF = res.getBodyAsBlob(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment