It's fair to say that Monday's are not usually the favourite day of the week for most people. However, on this particular Monday we were able to help a customer with an operational problem which was really causing headaches. High off the endorphins of our success, we thought we would share our work in this blog; and show you, how you can automate the merging of PDF files, using MOVEit Automation.
The brief was very simple, the customer receives a batch of around 100 single-page PDFs via an automated process. Those PDFs are then printed by a human user at the destination. Already, I am sure you can see the problem - not only is this unnecessarily labour intensive, but there were issues with users opening 100 PDFs at once, versus the maximum number of open files in Adobe Acrobat Reader.
As the files are being received by MOVEit Transfer, we thought we could write a custom script in MOVEit Automation to collect these files and merge them into one single file, with 100 pages.
So how did we do it?
Installing the MergePDF Module
Firstly, we elected to use PowerShell as our scripting language. VBscript is also supported by MOVEit Automation but we try to avoid using this where possible due to its age.
PDF interaction and merging is not available in the default PowerShell modules, so we will need to install a module to provide this functionality. We opted to use MergePDF written by Sergey Pavlyukovich, which is available on Microsoft's PowerShell Gallery.
To install, just run the command below:
PS C:\> Install-Module -Name MergePdf
Note. Make sure to open a PowerShell x86 PowerShell terminal as an administrator.
You may be prompted to install some dependencies, before you can complete the installation of the MergePDF module Once installed, we can use this module within the MOVEit Automation PowerShell scripting interface.
Creating the MOVEit Automation PowerShell Script
In the MOVEit Automation web administration console, we are going to create a new script under RESOURCES > SCRIPTS. Press the "Add Script" button in the top right corner of the interface.
We have chosen a name of "Merge-PDFs" and selected PowerShell as the script type. The full script in text format is below:
<# capture name of current file being processed and set a file name based on a customisable parameter #>
$sourceFile = $miaclient.MICacheFilename()
$outputName = $miaclient.MIGetTaskParam('OutputName')
<# test to see if an output file has already been created in the destination #>
If(Test-Path -path ("C:\Users\chrispayne804\Desktop\Merged PDFs\" + $outputName))
<# if output file already exists, append to it #>
$sourceFile | Merge-Pdf -OutputPath ("C:\Users\chrispayne804\Desktop\Merged PDFs\" + $outputName) -Append
<# if not, create the output file with the first PDF file #>
$sourceFile | Merge-Pdf -OutputPath ("C:\Users\chrispayne804\Desktop\Merged PDFs\" + $outputName)
<# return any errors to MOVEit Automation #>
$miaclient.MISetErrorDescription("Error: " + $Error)
In the above script, we have created a fixed path for a destination but this can easily be replaced with a dynamic variable.
Setting Up the Task
With our script now in place, we can go ahead a create an advanced task, which will pick up the PDF files from the source and pass them into the script.
If you remember back in the script, we referenced a dynamic parameter for the file name. I did this so that we could create a file name unique for each day. To complete this, we need to edit the script and insert something called a task parameter. This parameter name must match the name referenced in the PowerShell Script. In our case "OutputName".
I have entered a macro which will produce today's date.
Running the Merge Task
Now that we have everything in place, all that is left to do is place multiple PDF files into the source directory. In my case I am using the datasheets for MOVEit Transfer, Automation and Cloud.
I run the task manually as I have not set up a schedule yet...and what we end up with is one file with today's date as its name.
Which contains the content of all three of the individual PDFs.
It is as simple as that!
If you would like further information about MOVEit Automation, custom scripting or any other assistance with file transfer solutions. You can book a short call with one of our solution specialists. We have over twelve years of experience working with file transfer solutions and would be happy to speak with you.