@tilal.ahmad
Thanks for all your effort . I feel i am very close. I have two questions here.
- What is file name parameter.
- What is HtmlData Parameter here.
Now My Input is
String jsonData = ‘
秘密保持契約書 および売主またはそのいずれかは、本契約の日付の前後いずれに提供されたかを問わないすべての情報(以下
’ ;
Output - Should be blob version or base64 encoded or UTF-8 encoded or at least an pdf url which i will convert to base64.
Request - System.HttpRequest[Endpoint=https://api.aspose.cloud/v4.0/words//MailMerge?destFileName=test.pdf, Method=PUT]
response: System.HttpResponse[Status=Not Found, StatusCode=404]
What am i missing here?
Below is my code.
public class asposeMailMerge {
public static string GetJWT(){
String grant_type = 'client_credentials';
String client_id= '1ddabfd7-82a1-421f-a051-1d39d487f6c0';
String client_secret= '7c0e602e6a5c346829e6ceae20070dc4';
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.aspose.cloud/connect/token');
request.setMethod('GET');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setHeader('Accept', 'application/json');
String body = 'grant_type=' + grant_type + '&client_id=' + client_id + '&client_secret='+ client_secret ;
request.setBody(body);
Http http = new Http();
HttpResponse res = http.send(request);
String jsonInput = res.getBody();
Map<String, Object> a =(Map<String, Object>)JSON.deserializeUntyped(jsonInput);
system.debug('JWT Token is: '+a.get('access_token'));
return a.get('access_token').ToString();
}
public static String ProcessCommand(String strURI, String strHttpCommand, String strContent, String ContentType, String accessToken) {
try {
HttpRequest request = new HttpRequest();
Integer len = 0;
if (strContent != null) {
request.setBody(strContent);
len = strContent.length();
}
system.debug('httpcommand: ' + strHttpCommand);
request.setEndpoint(strURI);
request.setMethod(strHttpCommand);
if (ContentType.toLowerCase() == 'xml')
request.setHeader('Content-Type', 'application/xml');
else
request.setHeader('Content-Type', 'application/json');
request.setHeader('Content-Length', String.valueOf(len));
request.setHeader('Accept', 'application/json');
request.setHeader('Authorization', accessToken);
request.setTimeout(120000);
Http http = new Http();
system.debug('request ' + request);
HttpResponse res = http.send(request);
system.debug('util response:' + res);
return res.getBody();
} catch (Exception ex) {
system.debug('HTTP ERROR' + ex.getMessage());
system.debug(ex.getStackTraceString());
return null;
}
}
public static String ExecuteMailMerege(String FileName, String htmlData, String outFormat) {
try {
String strURl = 'https://api.aspose.cloud/v4.0' + '/words/' + FileName + '/MailMerge?destFileName=test.pdf';
//Json
//String htmlData='{root':{'data':{'format':'html','htmlText':'<html><head><style type=“text/css”>h2{color:green} .color_red{color:red} .color_green{color:green} .color_magenta{color:magenta} .color_olive{color:olive} .color_teal{color:teal}</style></head><body><h2>Table</h2><table border=“2” cellspacing=“0” cellpadding=“4”><tr><th class=“color_red”>Column 1</th><th class=“color_green”>Column 2</th></tr><tr><td class=“color_magenta”>Value 1.1</td><td class=“color_olive”>Value 1.2</td></tr><tr><td class=“color_teal”>Value 2.1</td></tr></table><h2>Div</h2><div class=“color_olive”>Outer text<div class=“color_magenta”>Inner text</div>Outer text</div><h2>Image</h2><br/><img src=“data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAADUUlEQVR42lWTWUwTURSGaUtIKY5QweISTUzceDGu0Rj1xURjTNQEE+ODGn0hPhBEQdqZztahBbrAtFih1Fprh9IBiVLLErZSU7BUoS7VqNH44ovx0S0ovdfO4Ez04cw9Ofec7/5zzr15NEnkUxSVFx8ObZ1x7nzHN6z9nOhnzOFwWCnECcKoEFbBRrrp6iF/XRvD0BoplkdTuAiIhtt3P3Fu+jJlKlkM1WoXLFjVeSnpXrC94gV3ciJp1X0L1K7+NtDfvXdpj1TmmUwm8QTfLa867T+ceWTSwO5r5b87XNaDEoBrv3p2Ai+AU1Qh5OiDGS4UXi4roAisQHBiYfJS2obAVPMyELYcGxViOIaKcLaNLXzadej5nBUBPXW6hdYW0z4RQOYUXK2tUfr8QeXL0Kn4JKEBk7QWdtrrLggJJEmoCBxVCf50qIaaa0HABFkEPZaLNnGfwPNFGV0u05Y554bvMQoBPLbxs9t9QydJpHP/KayRbsf+pG1NdgTTgDuGXa8dbS41ZtArxaQYT1WlHVqYaloGOcvJIan4X2MdViTl3vExbS+GPfXlvzzutj1LPch9HrYea59t0YIRtAAO+PQuIWYwNKikYty41Iuk/3TvMzsCB9HcQR7msgzgjZs7Z5rLQPR6PojeJW2d3oDCoG/IlwAYqlexbp/i9eB19ziuBoMoAlrR03YZ0G8+0JFhS8EjuhD6zWfuCTEjppcBOKYX1bwaqA5MM2oYZ4pBy7XjrAyY7sPqU01FcNZW/jtwZeXXVpt5u1RM4JhYzHFBddqz5/28fUU2NwngtVUbZUCE79o6x67/MYiXLiSbS8D9xp1vI7znkLvDK07A5/MiT7mzdxMMAsdMqxaiWDHkgzePigBUXy9KTfdWeVKNSjjPrvv5uKkYxszlIHX7eDLeVfkgbt30bt5eAlOOtT9nLRrI03szrOumOtcb4SrTSj2KKfr4nuUZ7kR8HFPAYXLlYqxRl43TGjjTWAhHCC1MNK/+lWA0cJLRgfsh95G/N1H136z7+BCSuHMuMEGVLo4a1XCKKcuO0WXZBw1FcNighmPWbe8jIeeJpdFiCrkHguWUyM92NBKseBut8T92bfsw66z49IyrHH2T5CvlqeCYUvL/ANIZ7+ohXSdWAAAAAElFTkSuQmCC”></body></html>"}}}’;
//String jsonData = JSON.serialize('<p style="text-align: center;"><b style="color: rgb(102, 102, 102); font-size: 9pt;">秘密保持契約書 および売主またはそのいずれかは、本契約の日付の前後いずれに提供されたかを問わないすべての情報(以下</b></p>');
String jsonData='{"root":{"data":{"format":"html","htmlText":"<html><head><style type="text/css">h2{color:green} .color_red{color:red} .color_green{color:green} .color_magenta{color:magenta} .color_olive{color:olive} .color_teal{color:teal}</style></head><body><h2>Table</h2><table border="2" cellspacing="0" cellpadding="4"><tr><th class="color_red">Column 1</th><th class="color_green">Column 2</th></tr><tr><td class="color_magenta">Value 1.1</td><td class="color_olive">Value 1.2</td></tr><tr><td class="color_teal">Value 2.1</td></tr></table><h2>Div</h2><div class="color_olive">Outer text<div class="color_magenta">Inner text</div>Outer text</div><h2>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GjjTWAhHCC1MNK/+lWA0cJLRgfsh95G/N1H136z7+BCSuHMuMEGVLo4a1XCKKcuO0WXZBw1FcNighmPWbe8jIeeJpdFiCrkHguWUyM92NBKseBut8T92bfsw66z49IyrHH2T5CvlqeCYUvL/ANIZ7+ohXSdWAAAAAElFTkSuQmCC"></body></html>"}}}';
system.debug('jsonData ' + jsonData );
String strJSON = ProcessCommand(strURl, 'PUT', jsonData, 'json', GetJWT());
system.debug('strJSON @@ ' + strJSON);
String downloadURL = null;
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(strJSON);
if(params.containsKey('Document')){
Map<String, Object> doc = (Map<String, Object>)params.get('Document');
String DocName = (String)doc.get('FileName');
strURl = 'https://api.aspose.cloud/v4.0' + '/words/' + DocName + '?format=' + outFormat;
downloadURL = strURl;
}
return downloadURL;
}
catch (Exception ex) {
system.debug(ex.getLineNumber() + '- ' + ex.getCause() + '- ' + ex.getMessage() + '- '+ ex.getStackTraceString());
return null;
}
}
}