Hi, @sebgmc, here’s a function you can put in a CFC. The function takes the following params:
key: CloudConvert Key
input_format: html (you can find all the formats here - Convert Files API | CloudConvert)
output_format: docx
file_input: upload or url
file_path: path to the file locally (for upload) or url path
It returns a URL to the converted file. You can do something like:
<cfhttp method="get" url="#RETURN_FROM_FUNCTION#" getasbinary="YES" path="#SOMEDIRECTORY#" file="#SOMEFILENAME#"/>
<cffunction name="ConvertFile" returntype="string" access="public">
<cfargument name="key" type="string"/>
<cfargument name="input_format" type="string"/>
<cfargument name="output_format" type="string"/>
<cfargument name="file_input" type="string"/>
<cfargument name="file_path" type="string"/>
<cfscript>
//Cloudconvert API - https://cloudconvert.com/api/v2/convert#convert-tasks
//Default options
LOCAL.converteroptions = {
"page_orientation": "portrait",
"no_images": "null",
"no_background": "null",
"disable_javascript": "null",
"javascript_delay": "200",
"image_dpi": "600",
"image_quality": "94",
"zoom": "1.0",
"grayscale": "null",
"print_media_type": "null",
"lowquality": "null",
"page_size": "letter",
"page_width": "null",
"page_height": "null",
"enable_forms": "null",
"margin_top": "10",
"margin_bottom": "10",
"margin_left": 0,
"margin_right": 0,
"header_left": "null",
"header_center": "null",
"header_right": "null",
"header_line": "null",
"header_html": "null",
"footer_left": "null",
"footer_center": "null",
"footer_right": "null",
"footer_line": "null",
"footer_html": "null",
"disable_smart_shrinking": "null",
"run_script": "null",
"command": "null"
};
//If passing margin or footer, override default options
if (StructKeyExists(arguments,"margin_top")) LOCAL.converteroptions.margin_top = arguments.margin_top;
if (StructKeyExists(arguments,"footer_html")) LOCAL.converteroptions.footer_html = arguments.footer_html;
if (StructKeyExists(arguments,"footer_center")) LOCAL.converteroptions.footer_center = arguments.footer_center;
//Make first call to get a proces obj (task)
cfhttp(method="POST", charset="utf-8", url="https://api.cloudconvert.com/process", result="LOCAL.process") {
cfhttpparam(name="apikey", type="formfield", value=arguments.key);
cfhttpparam(name="inputformat", type="formfield", value=arguments.input_format);
cfhttpparam(name="outputformat", type="formfield", value=arguments.output_format);
}
LOCAL.processObj = DeSerializeJSON(LOCAL.process.FileContent);
//Override all of default objects with process object
LOCAL.conversionObj = LOCAL.processObj;
//Convert file
cfhttp(method="POST", charset="utf-8", url="https:#LOCAL.processObj.url#", result="LOCAL.conversion", multipart="yes") {
cfhttpparam(name="input", type="formfield", value=arguments.file_input);
if (arguments.file_input EQ "upload") {
cfhttpparam(type="file",name="file",file=arguments.file_path);
} else {
cfhttpparam(type="formfield",name="file",value=arguments.file_path);
}
cfhttpparam(type="formfield",name="outputformat",value=arguments.output_format);
if (StructKeyExists(arguments,"margin_top")) {
cfhttpparam(type="formfield",name="converteroptions[margin_top]",value=arguments.margin_top);
}
if (StructKeyExists(arguments,"footer_html")) {
cfhttpparam(type="formfield",name="converteroptions[footer_html]",value=arguments.footer_html);
}
if (StructKeyExists(arguments,"footer_center")) {
cfhttpparam(type="formfield",name="converteroptions[footer_center]",value=arguments.footer_center);
}
}
LOCAL.conversionObj = DeSerializeJSON(LOCAL.conversion.FileContent);
return "https:#LOCAL.conversionObj.output.url#";
</cfscript>
</cffunction>
Let me know if you run into any issues.