0%

使用qpdf对pdf进行分割合并

最近购买了一台家用打印机MG3080
打印机只能打单页,不能打双页
需要手动换纸,比较麻烦
找了个pdf分割合并的工具

安装

1
yum install qpdf

windows版本

简单使用

查询页数: qpdf src.pdf --show-npages
分割: qpdf src.pdf --pages src.pdf 1,3,5,7 -- 1357.pdf
合并: qpdf --empty --pages 1.pdf 3.pdf -- 13.pdf

脚本

split_pdf.shview raw
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
#!/bin/bash - 
#===============================================================================
#
# FILE: split_pdf.sh
#
# USAGE: ./split_pdf.sh xxx.pdf
#
#===============================================================================

#######color code########
RED="31m"
GREEN="32m"
YELLOW="33m"
BLUE="36m"

###############################
colorEcho(){
COLOR=$1
echo -e "\033[${COLOR}${@:2}\033[0m"
}

colorEchoN(){
COLOR=$1
echo -n -e "\033[${COLOR}${@:2}\033[0m"
}

CUR_DIR=$(cd $(dirname $0); pwd)

QPDF=${CUR_DIR}/qpdf-8.4.2/bin/qpdf.exe

splitPdf(){
PDF_FILE=$1

if [[ "${PDF_FILE}" == "exit" ]]; then
colorEcho $RED "exit"
exit
fi

if [[ ! -e ${PDF_FILE} ]]; then
colorEcho $RED "没有找到该文件"
continue
fi

EXT=${PDF_FILE##*.}
if [[ "${EXT}" != "pdf" && "${EXT}" != "PDF" ]]; then
colorEcho $RED "不是pdf文件"
continue
fi

DEST_DIR=${PDF_FILE}.d
mkdir -p ${DEST_DIR}

TotalPage=$(${QPDF} ${PDF_FILE} --show-npages)

## 逆序
Page1=$(seq 1 2 $TotalPage | tac | paste -sd,)
Page2=$(seq 2 2 $TotalPage | tac | paste -sd,)

${QPDF} ${PDF_FILE} --pages ${PDF_FILE} $Page1 -- ${DEST_DIR}/1.pdf
${QPDF} ${PDF_FILE} --pages ${PDF_FILE} $Page2 -- ${DEST_DIR}/2.pdf

colorEcho $GREEN "分割完成,请在同名目录下查看。"
}

colorEcho $GREEN "-------------------------------------------------------------"
colorEcho $GREEN "欢迎使用pdf分割工具"
colorEcho $GREEN ""
colorEcho $GREEN "该工具基于qpdf,更高级的应用请查看qpdf --help"
colorEchoN $GREEN "该工具会将pdf文件分割成"
colorEchoN $BLUE "纯单页pdf(1.pdf)"
colorEchoN $GREEN "和"
colorEchoN $BLUE "纯双页pdf(2.pdf)"
colorEcho $GREEN ""
colorEchoN $GREEN "分割后的文建保存在"
colorEchoN $BLUE "pdf文件.d"
colorEcho $GREEN "目录下"
colorEcho $YELLOW "运行中的warning可以忽略,不影响结果"
colorEcho $GREEN "-------------------------------------------------------------"
colorEcho $GREEN ""
while true; do
colorEchoN $GREEN "请输入pdf文件(Ctrl-c/exit-退出,直接拉文件到窗口即可):"
read PDF_FILE
splitPdf "$PDF_FILE"
done

先打1.pdf
将打印完成的反过来(不需要调整顺序),再打2.pdf

问题

  1. 脚本即使省去了每次都要换纸的麻烦,但是还是需要在打印完成后手动调整顺序