大きなPDFを小さなファイルに分割

大きなPDFを小さなファイルに分割

使用pdftk次のコマンドを使用してPDFからページ範囲を抽出できます。

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

約500ページ、100MBを超える巨大なPDFが複数あります。自動的に最大5MBのチャンクに分割できますか?

ベストアンサー1

私はこのPythonスクリプトを見つけました。smpdfこの機能で。スクリプトは(一部)ドイツ語で書かれていますが、それが何をしているのか、そしてどのように使用するのかを簡単に理解できます。この必要pypdf

インストールと設定

まずスクリプトをダウンロードします。

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

次に、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

次に、以下からPDFサンプルファイルをダウンロードしました。example5.com。具体的には文書

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

ダウンロードしたサンプルファイルは次のとおりです。

$ 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

したがって、サンプルファイルは44ページで構成され、サイズは386KBです。次のコマンドを使用すると、PDFを〜0.1 MB(〜100 KB)のチャンクファイルに分割できます。

python pdfsm.py chunk 0.1 chickering04a.pdf

次の出力が生成されます。

    ======== 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

今、私たちのディレクトリには、次のファイルが含まれています。

$ 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

生成されたPDFファイルの統計を表示するには、この「ハッキング」コマンドを使用します。

$ 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

おすすめ記事