How to Merge Multiple PDF Files in Batch Script
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
Ghostscript is not included with Windows. You must download and install it from the official Ghostscript website. After installation, locate the command-line executable:
- 64-bit:
C:\Program Files\gs\gsX.XX.X\bin\gswin64c.exe - 32-bit:
C:\Program Files (x86)\gs\gsX.XX.X\bin\gswin32c.exe
The c suffix (e.g., gswin64c.exe) denotes the console version, which is required for Batch automation. The non-c version (gswin64.exe) opens a GUI window.
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.
)
pause
| Flag | Description |
|---|---|
-q | Quiet mode, suppresses startup messages |
-dNOPAUSE | Disables the "press return to continue" prompt between pages |
-dBATCH | Exits Ghostscript after processing, essential for automation |
-sDEVICE=pdfwrite | Sets the output format to PDF |
-sOutputFile= | Specifies the output file path |
Merging 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.
)
pause
Why 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
The FOR loop reads files in the order the file system returns them, which is typically alphabetical. If merge order matters (e.g., Page1.pdf before Page2.pdf), use zero-padded filenames (Page01.pdf, Page02.pdf, ... Page10.pdf) so they sort correctly. Alternatively, use the explicit file list approach shown above.
If you have hundreds of PDFs, the fileList string may exceed the Batch variable length limit (~8191 characters) or the command line length limit. For large merges, write the filenames to a response file instead:
:: Write file list to a response file
(for %%F in ("!SOURCE_DIR!\*.pdf") do echo "%%F") > "%TEMP%\pdf_list.txt"
:: Use Ghostscript's @ parameter to read from the file
"!GS_EXE!" -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="!OUTPUT_FILE!" @"%TEMP%\pdf_list.txt"
del "%TEMP%\pdf_list.txt" 2>nul
Another excellent alternative to Ghostscript is PDFtk (PDF Toolkit). The syntax for merging is simpler:
pdftk "C:\Folder\*.pdf" cat output "Merged.pdf"
PDFtk is lighter weight than Ghostscript and purpose-built for PDF manipulation, but it is a separate download from pdftk.com.
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.