Skip to content

Instantly share code, notes, and snippets.

@k-barton
Last active March 16, 2023 14:57
Show Gist options
  • Save k-barton/f8314031054e868b92feb6dca7317a0d to your computer and use it in GitHub Desktop.
Save k-barton/f8314031054e868b92feb6dca7317a0d to your computer and use it in GitHub Desktop.
Windows batch script to combine mutiple pdfs into one using Ghostscript
@echo off
setlocal EnableDelayedExpansion
set result=
set outfile=
set nextisoutfile=
set "self=%~n0"
:startHelp
if "%1" EQU "/?" goto :showHelp
if "%%j" EQU "--help" goto :showHelp
if "%1" EQU "" goto :endHelp
shift
goto :startHelp
:endHelp
for %%j in (%*) do (
set "x=%%j"
if "!nextisoutfile!" NEQ "" (
set "outfile=!x!"
set nextisoutfile=
) else if "!x:~0,2!"=="-o" (
set nextisoutfile=true
) else if "!x:~0,9!"=="--outfile" (
set nextisoutfile=true
) else (
set "infile=%%~dpnxj"
if not exist "!infile!" (
echo Warning: file "!infile!" does not exist.
) else (
echo ^+ %%~nxj
set "result=!result! ^"!infile!^""
)
set nextisoutfile=
)
)
if "!outfile!" EQU "" (
echo Error: no output file given.
) else if exist "!outfile!" (
echo Error: file "!outfile!" already exists.
) else (
gswin64c -dNOPAUSE -sDEVICE=pdfwrite -dOptimize=true -dAutoRotatePages=/None -sOUTPUTFILE=!outfile! -dBATCH !result!
)
goto :end
:showHelp
echo.
echo. Combines two or more pdf files into one.
echo. Usage:
echo. %self% input_file [^+ ...] [--output= ^| -o] output_file
echo.
:end
@k-barton
Copy link
Author

command line:

combine file1.pdf file2.pdf [...] -o output.pdf
combine file1.pdf file2.pdf [...] --output=output.pdf
combine /?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment