Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Using pdftk it is possible to extract page ranges from a pdf using

pdftk a.pdf cat 124-end output b.pdf dont_ask

I have a bunch of huge PDFs with about 500 pages and over 100 MB, is it possible to automatically split those in pieces of maximal 5 MB?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

I found this python script called smpdf that has this feature. This script is written in German (some of it) but it's easy enough to figure out what it's doing and how to use it. It requires PyPdf.

Installation & Setup

First download the script:

svn checkout http://smpdf.googlecode.com/svn/trunk/ smpdf

Then download & install PyPdf:

wget http://pybrary.net/pyPdf/pyPdf-1.13.tar.gz
tar zxvf pyPdf-1.13.tar.gz
cd pyPdf-1.13
sudo python setup.py install
cd ../smpdf

Next I downloaded a sample PDF file from example5.com. Specifically this file.

Usage of smpdf:

[ERROR] Ung�ltiger Aufruf
===========================================================================
                         PDF Manipulator
               (c) 2007 by Franz Buchinger
---------------------------------------------------------------------------

   Verwendung:

   pdfm split 5 file.pdf       Datei file.pdf in PDFs mit jeweils 5 Seiten splitten
   pdfm chunk 3 file.pdf       Datei file.pdf in max. 3 MB grosse PDFs splitten
   pdfm burst file.pdf         Jede Einzelseite in file.pdf in ein PDF schreiben
   pdfm merge f1.pdf f2.pdf    f1.pdf und f2.pdf in ein PDF mergen
   pdfm merge output.pdf dir   mergt alle PDFs im Verzeichnis dir in die Datei output.pdf
   pdfm info f1.pdf            zeigt Dokumentinformationen (Groesse, Seitenzahl, Titel,..) zu f1.pdf an

The sample file we downloaded is as follows:

$ pdfinfo chickering04a.pdf 
Title:          chickering04a.dvi
Creator:        dvips(k) 5.94a Copyright 2003 Radical Eye Software
Producer:       AFPL Ghostscript 8.0
CreationDate:   Fri Oct  8 17:53:18 2004
ModDate:        Fri Oct  8 17:53:18 2004
Tagged:         no
Pages:          44
Encrypted:      no
Page size:      612 x 792 pts (letter)
File size:      386372 bytes
Optimized:      no
PDF version:    1.3

So this sample file has 44 pages and is 386KB in size. Using the following command we can split the PDF up into chunk files that are ~0.1MB (~100KB).

python pdfsm.py chunk 0.1 chickering04a.pdf

Which produces the following output:

    ======== NEUES PDF ========
    Seite:0, Groesse: 12696
    Seite:1, Groesse: 11515
    Seite:2, Groesse: 17209
    Seite:3, Groesse: 17411
    Seite:4, Groesse: 17060
    Seite:5, Groesse: 26303
======== NEUES PDF ========
    Seite:9, Groesse: 31014
    Seite:10, Groesse: 27666
    Seite:11, Groesse: 18548
...
...
======== NEUES PDF ========
    Seite:40, Groesse: 19059
    Seite:41, Groesse: 20912
    Seite:42, Groesse: 17685
    Seite:43, Groesse: 5362

Our directory now contains the following files:

$ ls -l
total 1220
-rw-rw-r-- 1 saml saml  74471 May 12 09:23 chickering04a-chunk001.pdf
-rw-rw-r-- 1 saml saml  78673 May 12 09:23 chickering04a-chunk002.pdf
-rw-rw-r-- 1 saml saml  89259 May 12 09:23 chickering04a-chunk003.pdf
-rw-rw-r-- 1 saml saml  92569 May 12 09:23 chickering04a-chunk004.pdf
-rw-rw-r-- 1 saml saml  96953 May 12 09:23 chickering04a-chunk005.pdf
-rw-rw-r-- 1 saml saml  86390 May 12 09:23 chickering04a-chunk006.pdf
-rw-rw-r-- 1 saml saml  90815 May 12 09:23 chickering04a-chunk007.pdf
-rw-rw-r-- 1 saml saml  92094 May 12 09:23 chickering04a-chunk008.pdf
-rw-rw-r-- 1 saml saml  78909 May 12 09:23 chickering04a-chunk009.pdf
-rw-rw-r-- 1 saml saml 386372 May 12 08:30 chickering04a.pdf
-rwxrwxr-x 1 saml saml   9324 May 12 07:41 pdfsm.py
drwxr-xr-x 4 saml saml   4096 May 12 08:25 pyPdf-1.13
-rw-rw-r-- 1 saml saml  35699 May 12 08:24 pyPdf-1.13.tar.gz

I used this "hacked" command to show the stats of the generated PDF files:

$ printf "%7s%6s\n" "# pages" "size"; for i in chickering04a-chunk00*; do pdfinfo $i | egrep "File size|Pages"|cut -d":" -f2;done|sed 's/[\t ]\+/ /'|paste - -
# pages  size
 5       74471 bytes
 3       78673 bytes
 3       89259 bytes
 5       92569 bytes
 4       96953 bytes
 3       86390 bytes
 5       90815 bytes
 6       92094 bytes
 5       78909 bytes
share|improve this answer

pdftk does not have such an option. But you can burst the complete file in single pages and combine sets of these pages until you would go over 5Mb.

There would be a little error margin as a single page PDF has some overhead compared to the combined ones (resulting in chunks that could have had one more page before going over the 5Mb). But with an average of 0.2M per file that is probably acceptable.

I have a program (in Python) that does that while combining scanned pages in a TIFF file, as my tiff→djvu conversion has a size limit.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.