You can add PDF files stored in files within Salesforce to a generated document. This can be done via APEX, and an example is provided below.
However, Managing the entire PDF in memory in Salesforce can lead to heap size (memory) errors.
To avoid this, Set the “Handle Files Via API” field to true on your DonConfig record.
Example:
You might have already created the DocConfigs, let’s use one of them to merge pdf file that is in your salesforce org to the generated document from that DocConfig. Please note that I choose random DocConfig record to explain this.
- Set “Handle Files Via API field” to true on DocConfig record.
Note: If you do not find that field on layout, add it to the layout or update it in the backend using developer console. - Create below class to add salesforce files to the generated document.
global with sharing class AddFileViaAPI implements cadmus_core.AbstractBeforeActionable { global void execute(cadmus_core__Actionable__c actionable, Id docConfig, Id objectId, Map<String, Object> inputMap, cadmus_core.ConvertController.ConvertDataModel cdm) { cadmus_core.DocumentDataHandler.addDynamicFilewithoutcontent('069d1000001d4L3AAI');// Id of the ContentDocument or salesforce file to merge to the generated document from DocConfig } }
- Create “Run Class” Actionable record to the DocConfig as shown below and tag the above created class to it.
- Below is the salesforce file which I want to merge to the DocConfig document. The ContentDocument Id highlighted in the below screenshot should be added in the apex class created in previous step.
- Open any opportunity record and edit the layout. Select the PDF Butler Document Selection component and update below highlighted field to ‘MERGE’ and save it.
- In the opportunity record, generate the DocConfig Document. PDF Butler will generate the document by merging the Salesforce pdf files added in apex class created in previous step.