[SalesForce] Merging pdf attachments

I am trying to merge pdf attachments and sending it as email. I am able to merge them but the last pdf always get overwrite all the others and send email as one. When I checked my email attachment size is total of all the attachments. Do anyone have any idea on how to achieve it.

My code

public class mergePdf{

public static void mergePrds(){
     List<Attachment> att = new List<Attachment>();
          String combinedDataAsHex = '';


     for(Attachment a : [Select Id, Body FROM Attachment WHERE ParentId= 'AccountId']){
         System.debug('Attachment ::: '+ a);
         combinedDataAsHex = combinedDataAsHex + EncodingUtil.convertToHex(a.body);

     }

     Blob combinedDataAsBlob = EncodingUtil.convertFromHex(combinedDataAsHex);

     System.debug('combinedDataAsBlob ::: ' + combinedDataAsBlob );
     //Blob combinedDataAsBlob1 = blob.toPDF(combinedDataAsHex);
     //Blob mergePdf = combinedDataAsBlob.toPDF(combinedDataAsBlob);


        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.setContentType('application/pdf');
        attach.setFileName('testPdf.pdf');
        attach.setInline(false);
        attach.Body = combinedDataAsBlob;

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setUseSignature(false);
        String ccEmail = 'email@gmail.com';
        mail.setToAddresses(new String[] { ccEmail  });
        mail.setSubject('PDF Email Demo');
        mail.setHtmlBody('Here is the email you requested! Check the attachment!');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 



        // Send the email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}
}

Best Answer

PDF is a binary format that, like most binary formats, simply can't be squashed together directly. It contains magic headers, object graphs, images, compression, and a bunch of other stuff that you really can't parse very well in Apex Code. You can try reading the documentation on parsing it, but you'd probably have better luck using an external library on Heroku, AWS EB, or some other cloud hosting service.

I'm not going to sit down and try to figure out how to make it work, but a feasible implementation in Apex Code might look something like this:

String[] pdfPages = new String[0];
for(Attachment record: [SELECT Body FROM Attachment WHERE ParentId = :accountId]) {
    String pdfFile = EncodingUtil.convertToHex(record.Body);
    // Figure out where the pages are, and extract one at a time
    while(hasMorePages(pdfFile)) {
        pdfPages.add(nextPage(pdfPage));
    }
}
// Now, generate the PDF headers and tables from the pdfPages...

Please note that this is only pseudo-code and not meant to be taken literally. While it's technically possible to do this, I can't imagine it'd be very efficient, and would probably run the risk of running into CPU or heap limits.