Convert a DOCX to PDF in Salesforce Apex

Cloudmersive
1 min readFeb 26, 2021

--

Let’s be real; Microsoft Word may be the go-to for creating and editing business documents, but it’s not exactly the prettiest or most presentable format. When it comes to presentability, PDF files win every time; they are tough to edit, so clients won’t modify the documents, and formatting from the original document can transfer seamlessly. The following API will make automatically converting DOCX files to PDFs a cinch.

To use the API in Apex, you will first need to download and copy the /client folder into your Apex project:

Download Apex Client

For the next step, you can call the function with the following code:

SwagConvertDocumentApi api = new SwagConvertDocumentApi();
SwagClient client = api.getClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');
Apikey.setApiKey('YOUR API KEY');
Map<String, Object> params = new Map<String, Object>{
'inputFile' => Blob.valueOf('Sample text file\nContents')
};
try {
// cross your fingers
Blob result = api.convertDocumentDocxToPdf(params);
System.debug(result);
} catch (Swagger.ApiException e) {
// ...handle your exceptions
}

That’s it! The returned response will provide you with your newly created PDF file. To retrieve your API key, simply head to the Cloudmersive website to register for a free account and gain access to 800 monthly calls.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet