9

Is it possible to split a PDF file into two parts or n parts using qpdf tool?

The docs say so but I couldn't find the exact command to do it.

I'm using qpdf version 10.0.1.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Alen Paul Varghese
  • 1,278
  • 14
  • 27
  • 3
    I think you need to invoke qpdf for each output file separately. For each output file, you can provide the page range you'd like to extract to the output file, such as `qpdf infile.pdf --pages infile.pdf 2-3 -- outfile.pdf` to extract pages 2 and 3 from infile.pdf to outfile.pdf. So using some shell loop you can create a command line that will extract multiple files. – Palo Jun 20 '20 at 20:31

2 Answers2

13

Yes, it is very easy:

Assume infile.pdf has 12 pages (pagecount):

qpdf.exe --split-pages=n infile.pdf output-%d.pdf

Pseudo code:

n = Integer(pagecount / number_of_parts)

More details from the original documentation:

--split-pages=[n]

Write each group of n pages to a separate output file. If n is not specified, create single pages. Output file names are generated as follows:

If the string %d appears in the output file name, it is replaced with a range of zero-padded page numbers starting from 1.

Otherwise, if the output file name ends in .pdf (case insensitive), a zero-padded page range, preceded by a dash, is inserted before the file extension.

Otherwise, the file name is appended with a zero-padded page range preceded by a dash.

Page ranges are a single number in the case of single-page groups or two numbers separated by a dash otherwise. For example, if infile.pdf has 12 pages

qpdf --split-pages infile.pdf %d-out would generate files 01-out through 12-out

qpdf --split-pages=2 infile.pdf outfile.pdf would generate files outfile-01-02.pdf through outfile-11-12.pdf

qpdf --split-pages infile.pdf something.else would generate files something.else-01 through something.else-12

Reference: https://qpdf.readthedocs.io/en/stable/cli.html#option-split-pages

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Hakan Usakli
  • 492
  • 5
  • 11
1

A somewhat brutish way of handling this that can be updated to subset multiple files by only modifying the bigfile and output = then re-running it all. Will update once I make a proper function for it.

pacman::p_load(pdftools, qpdf)
#some prep
bigfile <- "Some/File/Path.pdf"
biginfo <- pdf_length(bigfile)

# now we subset x2 being sure to define unique names for output
# otherwise the second file will overwrite the first one we create here.
# for part 1
pdf_subset(bigfile,
           pages = 1:(biginfo/2),
           output = "Some/File/Path_part_1.pdf")
# for part 2
pdf_subset(bigfile,
           pages = ((biginfo+1)/2):biginfo,
           output = "Some/File/Path_part_2.pdf")