I have a single PDF file 6 pages long that I want to split into six seperate pdfs (1.pdf, 2.pdf, 3.pdf) such that each file produced represents one page from the input. I would love to be able to do this simple task from the command line.
7 Answers
Open up the pdf in preview and then on the view menu select thumbnails. Ctrl select the pages that you want now drag and drop them to the desktop.
-
1This worked well. Took me about 30 seconds to do this after flailing about for around 30 minutes. Some people are using this technique in conjunction w/ Automator but I haven't tried it yet. Oct 17, 2014 at 0:38
This can be achieved by using pdfseparate
. You can install poppler with homebrew, by brew install poppler
. This will also install pdfseparate
. To split the PDF document.pdf
into into single pages 1.pdf
, 2.pdf
, etc. use:
pdfseparate document.pdf %d.pdf
-
3Just installed
poppler
a day ago for being able to convert PDF documents to SVG withpdf2svg
. Didn't notice thatpoppler
comes withpdfseparate
command. Since the accepted answer above (dragging and dropping all PDF pages with preview to desktop) requires me to "click around" and since I like solutions on terminal that work automagically by just a single command line,pdfseparate
is exactly what I need. Thanks a lot for that hint!– ArvidDec 18, 2015 at 8:47 -
1Interestingly, pdfseparate produces pdfs whose total size is much much larger than the size of the original pdf. I had a 400 pages document with 1.9 MB. After splitting, I got something around 60 MB. Jul 19, 2017 at 6:59
-
Just tested Konstantin's comment and he's right. Sizes from pdfseparate were ~9x bigger than those produced from just dragging pages out using preview.app Apr 23, 2020 at 6:55
-
1
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=document-compressed.pdf document.pdf
both before and after splitting (i.e., then replacedocument.pdf
by1.pdf
, etc) helps to decrease the size (see also Rastapopoulos's comment). Also it is expected to be bigger if some resources are shared among the pages, such as font inclusions.– ttqApr 23, 2020 at 8:25 -
3This worked perfectly, but mind that
brew install poppler
downloads the Internet.– pcambraAug 16, 2021 at 9:23
If you're interested in doing this from the command line, you can look at Benjamin Han's splitPDF python script to do the job. For instance:
splitPDF.py in.pdf 3 5
would split the file in.pdf
into 3 files, splitting at pages 3 and 5.
-
This is good, and a bit more flexible in what you can output than pdfseparate above. Though it is mainly for splitting a pdf into chucks of pages, if you did want to split each page, you could easily use
seq
to produce a range of numbers in your command. Thanks!– dmgigApr 22, 2016 at 18:20 -
1something like
python splitPDF.py MyPDF.pdf $(seq -s ' ' 1 10 411)
worked for me– dmgigApr 22, 2016 at 18:29 -
1
In ubuntu you'll need to install pdftk:
sudo apt update; sudo apt install pdftk
Then to have your problem solved, just:
pdftk input.pdf burst
-
As of 8/16/2023, this should be the accepted answer. With Nix, it even work without installing pdftk:
nix-shell -p pdftk --run 'pdftk multi_page.pdf burst'
(on Mac too). Aug 16 at 15:53
If you want to extract a range of pages, you can use the following script which you call like this (assumed that you save it to file pdfextract.py somewhere on your system's PATH, e.g. /usr/local/bin, and assign it execution permission with chmod 744 pdfextract.py):
pdfextract.py --file-in /path/to/large/pdf --file-out /path/to/new/pdf --start --stop
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
import subprocess as sp
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--file-in', required=True, type=str, dest='file_in')
parser.add_argument('--file-out', required=True, type=str, dest='file_out')
parser.add_argument('--start', required=True, type=int, dest='start', default=-1)
parser.add_argument('--stop', required=True, type=int, dest='stop', default=-1)
args = parser.parse_args()
assert os.path.isfile(args.file_in)
assert not os.path.isfile(args.file_out)
# remove temporary files
for el in os.listdir('/tmp'):
if os.path.isfile(os.path.join('/tmp', el)) and el[:12] == 'pdfseparate-':
os.remove(os.path.join('/tmp', el))
sp.check_call('pdfseparate -f {:d} -l {:d} {:s} /tmp/pdfseparate-%d.pdf'.format(args.start, args.stop, args.file_in), shell=True)
cmd_unite = 'pdfunite '
for i in range(args.start, args.stop + 1):
cmd_unite += '/tmp/pdfseparate-{:d}.pdf '.format(i)
cmd_unite += args.file_out
sp.check_call(cmd_unite, shell=True)
# remove temporary files
for el in os.listdir('/tmp'):
if os.path.isfile(os.path.join('/tmp', el)) and el[:12] == 'pdfseparate-':
os.remove(os.path.join('/tmp', el))
if __name__ == "__main__":
main()
For another alternative, see this answer. This uses the ImageMagick command line tools.
convert x.pdf -quality 100 -density 300x300 x-%04d.pdf
However, you have to be careful with the quality.
-
Because there are quality and density options, it sounds like this renders each page to an image. I think people here would prefer the option which preserves the original PDF data which might include text/fonts/vector graphics.– binkiAug 30 at 15:48
I have started to put together a tool to provide a simplified interface to common actions.
You can split PDFs into individual pages like this:
$ npm install @lancejpollard/act -g
$ act split my.pdf -o outputDirectory
If nothing else check out the source and see how to write your own script to do this in JavaScript.