How to convert HTML to a Word DOCX Document in Salesforce Apex

I think we can all agree that nobody wants to deal with file format conversion. It’s finicky, inconvenient, and frustrating. So why not make it easy on yourself? We’ve got just the API for you.

First we need to download and unzip our Apex Client to your project’s folder.

Now that our client is accessible, we are able to invoke convertWebHtmlToDocx:

SwagConvertWebApi api = new SwagConvertWebApi();SwagClient client = api.getClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');Apikey.setApiKey('YOUR API KEY');Map<String, Object> params = new Map<String, Object>{'inputRequest' => SwagHtmlToOfficeRequest.getExample()};try {// cross your fingersBlob result = api.convertWebHtmlToDocx(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}

Easy. Now go enjoy all that time you just saved.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store