はじめに

PDF分割をコマンドでできるというのを見たので、試してみようかなと思い実際にやってみた。

環境

1
2
3
Windows 10 Professional
WSL2 - (Ubuntu22.04 LTS)
pdfseparate version 22.02.0

準備

分割用のPDFは下記を使用する。 mysql-article

pdfseparateのインストール

1
$ sudo apt-get install poppler-utils

pdfseparateコマンドを使って分割をする

manコマンドでの確認

 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
pdfseparate(1)                                 General Commands Manual                                 pdfseparate(1)

NAME
       pdfseparate - Portable Document Format (PDF) page extractor

SYNOPSIS
       pdfseparate [options] PDF-file PDF-page-pattern

DESCRIPTION
       pdfseparate extract single pages from a Portable Document Format (PDF).

       pdfseparate  reads the PDF file PDF-file, extracts one or more pages, and writes one PDF file for each page to
       PDF-page-pattern.

       PDF-page-pattern should contain %d (or any variant respecting printf format), since %d is replaced by the page
       number.

       The PDF-file should not be encrypted.

OPTIONS
       -f number
              Specifies the first page to extract. If -f is omitted, extraction starts with page 1.

       -l number
              Specifies the last page to extract. If -l is omitted, extraction ends with the last page.

       -v     Print copyright and version information.

       -h     Print usage information.  (-help and --help are equivalent.)

EXAMPLE
       pdfseparate sample.pdf sample-%d.pdf

       extracts all pages from sample.pdf, if i.e. sample.pdf has 3 pages, it produces

       sample-1.pdf, sample-2.pdf, sample-3.pdf

AUTHOR
       The  pdfseparate  software  and documentation are copyright 1996-2004 Glyph & Cog, LLC and copyright 2005-2011
1
2
3
Usage: pdfseparate [options] <PDF-sourcefile> <PDF-pattern-destfile>

pdfseparate [options] PDF-file PDF-page-pattern

でいけるらしい。

すべてのページを1ページごとに分割したいので、 -f, -l オプションはなしで実行する。
下記で実行してみたところ…怒られた!

1
2
$ pdfseparate mysql_article.pdf outfile.pdf
Syntax Error: 'outfile.pdf' must contain '%d' (or any variant respecting printf format) if more than one page should be extracted, in order to print the page number

出力ファイルは複数になるので、%d を入れて連番を入れられるようにする必要があるってことかな。

改めて実行

1
pdfseparate mysql_article.pdf out_file_%d.pdf

おぉ~できてる。

1
2
3
$ ls
mysql_article.pdf  out_file_2.pdf  out_file_4.pdf  out_file_6.pdf
out_file_1.pdf     out_file_3.pdf  out_file_5.pdf  out_file_7.pdf

参考

おわりに

これでPDF分割もマスターです。
Twitterで「PDF分割をChatGPTに聞いたら、pythonコードでPDF分割する回答が返ってきて便利!」みたいなのを見たので、もっと簡単な解決方法がないかなと探してみたらあったのがきっかけ。
これで俺もChatGPT超えか…!