Sometimes we want to Combine Multiple PDF Files into a single one. Unforfutley, there is no inbuilt method or way in apex to combine these pdf, however, there are multiple 3rd party applications that can be utilized to combine pdfs into a single. I am going to explain two or more PDF merges using Conga Composer’s API.
Before you go with this approach, wanted to highlight that Conga composer is a paid tool and you may use it to pay for their API.
Once you have Conga Composer Install, you can use the Conga API using the below link:
Step1: Create the Conga query, which will bring the PDF from Salesforce, you can use below query, let's say you have an account object under which multiple pdfs are there and you wanted to combine them into one, write Conga query record with the below SOQL:
SELECT ContentDocument.id,ContentDocument.Title FROM ContentDocumentLink
WHERE LinkedEntityId = '{pv0}'
Step2: Make a REST API Using the below code:
public static String doCongaMergeAPICall (Id masterRecordId,Id queryId) {
String ssId=userinfo.getSessionId();
String servUrl = Url.getSalesforceBaseUrl().toExternalForm() + '
/services/Soap/u/37.0/' + UserInfo.getOrganizationId();
String url = 'https://composer.congamerge.com/composer8/index.html' +
'?sessionId=' + ssId +
'&serverUrl=' + EncodingUtil.urlEncode(servUrl, 'UTF-8');
String defaultParameters = '&SC0=1'
+ '&SC1=SalesforceFile'
+ '&defaultPDF=1'
+ '&APIMODE=1'
+ '&QVar0Format=10010'
+ '&APDF=1';
url = url +
+ '&Id='+masterRecordId
+ '&QVar0ID='+queryId+'%3Fpv0%3D'+masterRecordId
+ '&TemplateId={QVar0}'
+ '&OFN='+outputFileName
+ defaultParameters;
Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
try {
req.setEndpoint(url);
req.setMethod(GET);
res = http.send(req);
} catch(Exception ex) {
}
return res.getBody();
}
}