56

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.

2

8 Answers 8

38

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.

1
  • 1
    This 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.
    – user391339
    Oct 17, 2014 at 0:38
85

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
7
  • 3
    Just installed poppler a day ago for being able to convert PDF documents to SVG with pdf2svg. Didn't notice that poppler comes with pdfseparate 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!
    – Arvid
    Dec 18, 2015 at 8:47
  • 2
    Interestingly, 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.
    – Konstantin
    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
    – Vivek Gani
    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 replace document.pdf by 1.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.
    – ttq
    Apr 23, 2020 at 8:25
  • 4
    This worked perfectly, but mind that brew install poppler downloads the Internet.
    – pcambra
    Aug 16, 2021 at 9:23
7

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.

3
  • 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!
    – dmgig
    Apr 22, 2016 at 18:20
  • 1
    something like python splitPDF.py MyPDF.pdf $(seq -s ' ' 1 10 411) worked for me
    – dmgig
    Apr 22, 2016 at 18:29
  • 1
    Words great. I confirm this works directly on MacOS 10.13.3 Mar 28, 2018 at 21:52
4

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
1
  • 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).
    – toraritte
    Aug 16, 2023 at 15:53
1

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()
0

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.

0

A simple Node.js script to achieve this (requires Node.js to be installed):

# Customise this:
export PDFPATH=/path/to/input.pdf

npm install pdf-lib &&
cat << 'EOF' | node --input-type=module - "$PDFPATH"
import { readFile, writeFile } from 'fs/promises';
import { PDFDocument } from 'pdf-lib';

const pdfPath = process.argv[2];
const sourceDoc = await PDFDocument.load(await readFile(pdfPath))

for (let i = 0; i < sourceDoc.getPages().length; i++) {
  const outDoc = await PDFDocument.create()
  outDoc.addPage((await outDoc.copyPages(sourceDoc, [i]))[0])
  await writeFile(`${pdfPath}-${i}.pdf`, await outDoc.save());
}
EOF

NOTE: the script will create a node_modules directory in your CWD and install the pdf-lib dependency there.

-1

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.

1
  • 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.
    – binki
    Aug 30, 2023 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .