TransWikia.com

Is there a command-line method to split a multi-page PDF file into single pages?

Super User Asked by user391339 on December 5, 2021

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.

6 Answers

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.

Answered by Lance Pollard on December 5, 2021

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

Answered by Konstantin on December 5, 2021

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.

Answered by pheon on December 5, 2021

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

Answered by ttq on December 5, 2021

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.

Answered by Jean-Philippe Pellet on December 5, 2021

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.

Answered by eleethesontai on December 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP