-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfcat.cmd
87 lines (71 loc) · 2.56 KB
/
pdfcat.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@setlocal enabledelayedexpansion && echo off
@REM // This tool uses gs (GhostScript) to concatenate an arbitrary number of
@REM // PDF files into a single output. Run `pdfcat --help` for usage.
@REM // Ensure that the `gs` tool is somewhere on the path.
where gs 1>nul 2>nul
if %errorlevel% equ 0 (
set gsTool=gs
) else (
@REM // If no `gs` tool is found, search in standard locations for GhostScript.
if not exist "%ProgramFiles%\gs" (
echo.ERROR: Could not find an installation of GhostScript. To run this tool, 1>&2
echo. download GhostScript from https://www.ghostscript.com. 1>&2
echo. 1>&2
)
pushd "%ProgramFiles%\gs"
set gsTool=echo No ghostscript found. Arguments:
for /f "delims=" %%f in ('dir/b gs*') do (
if exist "%ProgramFiles%\gs\%%f\bin\gswin64.exe" (
set gsTool="%ProgramFiles%\gs\%%f\bin\gswin64.exe"
) else if exist "%ProgramFiles%\gs\%%f\bin\gswin32.exe" (
set gsTool="%ProgramFiles%\gs\%%f\bin\gswin32.exe"
) else (
REM -- No-Op
)
)
popd
)
set output=
set sources=
set batchOption=-dBATCH
@REM // Scan through all command-line arguments.
:scan_args
set arg="%~1"
if %arg% equ "" goto :args_end
if /i %arg% equ "/h" goto :help
if /i %arg% equ "/help" goto :help
if /i %arg% equ "-h" goto :help
if /i %arg% equ "-help" goto :help
if /i %arg% equ "--help" goto :help
if /i %arg% equ "--remain" (
set "batchOption= "
) else if not defined output (
set output=%~1
set output="!output:.pdf=!.pdf"
) else (
set glob=
for /f "delims=" %%g in ('dir /b /on %arg%') do (
set glob=!glob! "%%g"
)
set sources=!sources!!glob!
)
shift
goto :scan_args
:args_end
if not defined output goto :help
if not defined sources (
echo 1>&2ERROR: No source files specified.
echo. 1>&2
goto :help
)
call %gsTool% -dNOPAUSE %batchOption% -sDEVICE=pdfwrite -sOUTPUTFILE=%output% %sources%
exit /b 0
:help
echo.pdfcat: Concatenate multiple PDF files into one
echo.Usage: pdfcat [--remain] ^<outputName^> ^<input1^> [input2] [input3] ...
echo.
echo.This tool uses GhostScript to do the work, so you need to have it installed.
echo.You can download GhostScript from https://www.ghostscript.com.
echo.
echo.--remain
echo. Leave GhostScript window up to review all job output. Type 'quit' to close window.