Wojtek – IT guy

IT, Security, IAM, PKI, Software, Tools, Games, Scripts etc…

Merge PDFs and send to remote computer

I had a need to automate the process of merging PDF files and sending them over to remote computer. It started with just few lines, but it grew in time to cover different scenarios. I also added comments and colors. I thought I will share it. I am using Ghostscript to merge PDFs. Access to remote computer granted through windows share to local account ‘wojtek’. Script goes like this:

Write-Host "SCRIPT STARTED" -ForegroundColor Green
# Define paths
Write-Host "Configuring variables"
$localpath = "C:\Temp\SCAN"
$remotepath = "\\192.168.0.248\share"
$outputFile = "$localpath\scan.pdf"
$gs = 'C:\Program Files\gs\gs10.04.0\bin\gswin64c.exe'

# Check for file existence
$agrEXIST = Test-Path "$localpath\agreements.pdf"
$part1EXIST = Test-Path "$localpath\part1.pdf"
$anyPDFExist = Test-Path "$localpath\*.pdf"

Write-Host "Configuring variables DONE" -ForegroundColor DarkGreen

# Exit if there is nothing to process
If (-not ($agrEXIST -or $part1EXIST -or $anyPDFExist)) {
    Write-Host "Nothing to process... Exiting..."
    exit
}

# Warn if only non-standard PDFs exist
If (-not ($agrEXIST -or $part1EXIST) -and $anyPDFExist) {
    Write-Host "Nothing to process... But I found PDF files in the folder. Check if their names are correct - agreements.pdf and part1.pdf, part2.pdf etc."
    exit
}

# Map network drive
Write-Host "Mapping kitchen computer as drive Z"
net use z: $remotepath /user:wojtek PASSWORD | Out-Null
Write-Host "Mapping kitchen computer as drive Z DONE" -ForegroundColor DarkGreen

# Process PDF files if part1.pdf exists
If ($part1EXIST) {
Write-Host "Defining settings for PDF merge"
    $pdfFiles = Get-ChildItem -Path $localpath -Filter "part*.pdf" | ForEach-Object { "`"$($_.FullName)`"" }
    $args = @("-dBATCH", "-dNOPAUSE", "-q", "-sDEVICE=pdfwrite", "-sOutputFile=$outputFile") + $pdfFiles
    Write-Host "Defining settings for PDF merge DONE" -ForegroundColor DarkGreen
    # Run Ghostscript to merge PDFs
    Write-Host "Merging PDFs"
    & $gs @args
    Write-Host "Merging PDFs DONE" -ForegroundColor DarkGreen

    # Copy files based on existence
    Write-Host "Copying Scan"
    Copy-Item $outputFile -Destination z:\
    Write-Host "Copying Scan DONE" -ForegroundColor DarkGreen

    If ($agrEXIST) {
        Write-Host "Copying Agreements"
        Copy-Item "$localpath\agreements.pdf" -Destination z:\
        Write-Host "Copying Agreements DONE" -ForegroundColor DarkGreen
    }
} Else {
    # Copy only agreements.pdf if part1.pdf does not exist
    Write-Host "Copying Agreements"
    Copy-Item "$localpath\agreements.pdf" -Destination z:\
    Write-Host "Copying Agreements DONE" -ForegroundColor DarkGreen
}

# Unmap network drive
Write-Host "Removing kitchen computer as drive Z"
net use z: /DELETE | Out-Null
Write-Host "Removing kitchen computer as drive Z DONE" -ForegroundColor DarkGreen
Write-Host "SCRIPT COMPLETED" -ForegroundColor Green

Simple, but pretty unusual. Perhaps you’ll find it useful.

Leave a Reply

Your email address will not be published. Required fields are marked *