Run Cloudmersive DOCX to PDF Convert API in Salesforce Apex Developer Console |
9/17/2025 - Cloudmersive Support |
In this sample we will call the Cloudmersive Document Conversion API from the Salesforce Apex Developer Console.
First, we will do some basic configuration:
Allow the Callout
- In Salesforce, navigate to
Setup . Then click on Security and Remote Site Settings and then click on New .
- Name the
Remote Site Name as CloudmersiveAPI , and set the Remote Site URL to either https://api.cloudmersive.com or your Managed Instance endpoint. Click on Save.
Upload a test DOCX as a static resource
- In Salesforce, navigate to
Setup . Then click on Static Resources and then New .
- Under
Name enter DocxSample . Under File select a valid DOCX (not DOC) file.
Execute the code
- Navigate to the
Settings Gear and then Developer Console and then Debug and then Open Execute Anonymous . Check Open Log . Paste the below source code. Replace YOUR-API-KEY-HERE with the appropriate API key. If needed, update the API base path URL to match your Managed Instance base path.
Execute your code
- In the logs that appear click on
Debug Logs .
- Look for
Status: OK (200) - this indicates the API call was successful
- You can view the output PDF and verify it is correct by looking at the log after this
Open the file - copy the displayed URL and paste it into a new tab in your browser. After loading you will be able to view the PDF version of the DOCX.
// Quick DOCX -> PDF callout using Cloudmersive Convert API
// Prereqs: Remote Site Setting for https://api.cloudmersive.com
// Static Resource: DocxSample (a small .docx)
String boundary = '----CMFormBoundary' + String.valueOf(Math.abs(Crypto.getRandomInteger()));
String CRLF = '\r\n';
// Load the DOCX bytes from a Static Resource
Blob templateBlob = [SELECT Body FROM StaticResource WHERE Name = 'DocxSample'].Body;
// Build the multipart/form-data body with a single "inputFile" part
Blob pre = Blob.valueOf('--' + boundary + CRLF
+ 'Content-Disposition: form-data; name="inputFile"; filename="template.docx"' + CRLF
+ 'Content-Type: application/octet-stream' + CRLF + CRLF);
Blob endPart = Blob.valueOf(CRLF + '--' + boundary + '--' + CRLF);
Blob payload = EncodingUtil.convertFromHex(
EncodingUtil.convertToHex(pre) +
EncodingUtil.convertToHex(templateBlob) +
EncodingUtil.convertToHex(endPart)
);
// Send the request
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.cloudmersive.com/convert/docx/to/pdf');
req.setMethod('POST');
req.setHeader('Apikey', 'YOUR-API-KEY-HERE'); // <-- paste your key here
req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
req.setTimeout(120000); // up to 120s
req.setBodyAsBlob(payload);
Http http = new Http();
HttpResponse res = http.send(req);
System.debug('Status: ' + res.getStatus() + ' (' + res.getStatusCode() + ')');
// If successful, save the PDF in Files so you can open it
if (res.getStatusCode() == 200) {
Blob pdf = res.getBodyAsBlob();
ContentVersion cv = new ContentVersion(
Title = 'Converted-from-docx',
PathOnClient = 'Converted-from-docx.pdf',
VersionData = pdf,
IsMajorVersion = true
);
insert cv;
// Get the ContentDocumentId to form a clickable URL
cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id];
String host = System.Url.getOrgDomainUrl().getHost();
System.debug('Open the file: https://' + host + '/lightning/r/ContentDocument/' + cv.ContentDocumentId + '/view');
} else {
System.debug('Response body: ' + res.getBody());
}
|