1

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 ".

Tomas
  • 17,551
  • 43
  • 152
  • 257
RU_23
  • 89
  • 1
  • 10
  • Don't use JSON to post from Apex, use HTTP POST instead, you will save a lot of bandwidth, time and your nerves leaving JSON alone. The reference to HTTP POST https://developer.salesforce.com/forums/?id=906F0000000977iIAA The Curl example curl -F "Files[0]=@toMerge1.pdf" -F "Files[1]=@toMerge2.pdf" https://v2.convertapi.com/convert/pdf/to/merge?Secret=secret – Tomas Nov 08 '19 at 08:15
  • @Tomas, Thanks for the comment. I am using HTTP POST only in my code, but for HTTP body I am using JSON. – RU_23 Nov 08 '19 at 15:14
  • Please provide generated JSON. Thank you. – Jonas Nov 14 '19 at 06:50
  • @RU_23 What I meant that you use JSON as request body but I suggest to use multipart. Then you will save request body size and you don't need to build JSON. You can find example of multipart requests at https://www.google.com/search?q=Axep+HttpRequest+post+multipart&oq=Axep+HttpRequest+post+multipart&aqs=chrome..69i57.11023j0j7&sourceid=chrome&ie=UTF-8 – Tomas Nov 14 '19 at 08:28
  • @Tomas Thanks for your help. – RU_23 Nov 21 '19 at 18:58

0 Answers0