Combining several PDFs into a single document is a frequent requirement in office automation, whether merging daily reports into a monthly archive or combining scanned pages into a single dossier. Because Batch cannot natively manipulate PDF binary structures, we must rely on an external command-line utility. One of the best tools for this task is Ghostscript, a powerful interpreter for PostScript and PDF files.
In this guide, we will demonstrate how to merge PDF files using Ghostscript from a Batch script.
Prerequisites
Implementation Script
@echo off
setlocal enabledelayedexpansion
:: 1. Define Paths
set "GS_EXE=C:\Program Files\gs\gs10.07.0\bin\gswin64c.exe"
set "SOURCE_DIR=C:\PDF_Reports\Daily"
set "OUTPUT_FILE=C:\PDF_Reports\Merged_Monthly_Report.pdf"
:: Verify Ghostscript exists
if not exist "!GS_EXE!" (
echo [ERROR] Ghostscript executable not found at: !GS_EXE!
echo Download from: https://www.ghostscript.com/releases/gsdnld.html
pause
exit /b 1
)
:: Verify source directory exists
if not exist "!SOURCE_DIR!\" (
echo [ERROR] Source directory not found: !SOURCE_DIR!
pause
exit /b 1
)
echo Building list of PDFs in !SOURCE_DIR!...
:: 2. Accumulate all PDF filenames into a single string
set "fileList="
set "fileCount=0"
for %%F in ("!SOURCE_DIR!\*.pdf") do (
set "fileList=!fileList! "%%F""
set /a "fileCount+=1"
)
if !fileCount! EQU 0 (
echo [WARNING] No PDF files found in !SOURCE_DIR!.
pause
exit /b 1
)
echo Found !fileCount! PDF files. Merging...
:: 3. Execute Ghostscript
:: -dBATCH exits after processing. -dNOPAUSE disables prompts.
:: -q runs quietly. -sDEVICE=pdfwrite sets PDF output.
"!GS_EXE!" -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="!OUTPUT_FILE!" !fileList!
if !errorlevel! EQU 0 (
echo.
echo ==========================================
echo MERGE SUCCESSFUL!
echo Output: !OUTPUT_FILE!
echo Files merged: !fileCount!
echo ==========================================
) else (
echo.
echo [ERROR] Merge failed with exit code: !errorlevel!
echo Check that all input PDFs are valid and not encrypted.
)
pauseMerging Specific Files in a Defined Order
If you need to merge specific files in a particular order rather than all PDFs alphabetically, list them explicitly:
@echo off
setlocal enabledelayedexpansion
set "GS_EXE=C:\Program Files\gs\gs10.02.1\bin\gswin64c.exe"
set "OUTPUT_FILE=C:\Reports\Complete_Report.pdf"
:: Define files in the exact merge order
set "fileList="
set "fileList=!fileList! "C:\Reports\cover_page.pdf""
set "fileList=!fileList! "C:\Reports\executive_summary.pdf""
set "fileList=!fileList! "C:\Reports\detailed_analysis.pdf""
set "fileList=!fileList! "C:\Reports\appendix.pdf""
:: Verify all files exist
for %%F in (!fileList!) do (
if not exist %%F (
echo [ERROR] File not found: %%F
pause
exit /b 1
)
)
"!GS_EXE!" -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="!OUTPUT_FILE!" !fileList!
if !errorlevel! EQU 0 (
echo [SUCCESS] Merged to: !OUTPUT_FILE!
) else (
echo [ERROR] Merge failed.
)
pauseWhy Merge PDFs via Command Line?
- Archiving Automation: At the end of every month, a scheduled task can gather all daily billing PDFs and merge them into a single archive file, reducing clutter.
- Report Generation: After various systems generate individual PDF charts, a master script can merge them into a unified stakeholder package.
- No Manual Software Needed: Users don't need to open Adobe Acrobat or third-party GUI tools; the script handles it instantly in the background.
Important Considerations
Conclusion
Merging PDFs is a high-value automation task that saves massive amounts of manual effort. By pairing Batch looping logic with a robust tool like Ghostscript or PDFtk, you can instantly assemble fragmented documents into professional, unified reports. This integration expands your script's capability from mere file management into actual document composition.