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 protected]';
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 });
}
}