I want to merge PDFs that I have in my Salesforce org using "convertapi".
I follow this link: https://www.convertapi.com/pdf-to-merge and apply using reference code using JSON and the apex. code is mentioned below.
List<Id> relatedFilesIdsList = new List<Id>();
for(ContentDocumentLink cdl : [SELECT ContentDocumentId
FROM ContentDocumentLink
WHERE LinkedEntityId = 'XXXXX']){
relatedFilesIdsList.add(cdl.ContentDocumentId);
}
//Select list of attachments that should be merged
List<ContentVersion> selectedAttachmentsList = [SELECT Title,VersionData
FROM ContentVersion
WHERE ContentDocumentId IN :relatedFilesIdsList];
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('Parameters');
gen.writeStartArray();
gen.writeStartObject();
gen.writeStringField('Name','Files');
gen.writeFieldName('FileValues');
gen.writeStartArray();
gen.writeStartObject();
gen.writeStringField('Name',selectedAttachmentsList[0].Title);
gen.writeBlobField('Data',selectedAttachmentsList[0].VersionData);
gen.writeEndObject();
gen.writeStartObject();
gen.writeStringField('Name',selectedAttachmentsList[1].Title);
gen.writeBlobField('Data',selectedAttachmentsList[1].VersionData);
gen.writeEndObject();
gen.writeEndArray();
gen.writeEndObject();
gen.writeEndArray();
gen.writeEndObject();
String jsonS = gen.getAsString();
//System.debug(jsonS);
HttpRequest req = new HttpRequest();
req.setEndpoint('https://v2.convertapi.com/convert/pdf/to/merge?Secret=xxxxx');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setbody(jsonS);
Http http = new Http();
HTTPResponse response = http.send(req);
System.debug(response);
But I don't know for some reason I am getting the error: " Status=Internal Server Error, StatusCode=500 ".