Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a PDF file into another two PDF files using qpdf

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.

like image 278
Alen Paul Varghese Avatar asked Jan 25 '26 16:01

Alen Paul Varghese


2 Answers

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

like image 65
Hakan Usakli Avatar answered Jan 29 '26 11:01

Hakan Usakli


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")
like image 25
Rebecca Amodeo Avatar answered Jan 29 '26 11:01

Rebecca Amodeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!