PDF の画像を圧縮(続き) このエントリーを含むはてなブックマーク このエントリーをはてなブックマークに追加

Ghostscript (Windows 版)を使って、重たい PDF ファイルの画像を圧縮して、いくらかスリムにする方法。以下のコマンドを打つべし。


gswin32c.exe -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -sOutputFile=output.pdf -c .setpdfwrite -f input.pdf

各パラメータの意味は、だいたいこんな感じ。

  • -q : メッセージがうるさくなくなる
  • -dNOPAUSE : ページごとに処理を中断しない
  • -dBATCH : 最後のページまで処理が済んだらプログラムを終了する
  • -sDEVICE=pdfwrite : 出力形式は PDF
  • -dAutoFilterColorImages=false :
    カラー画像に関して、勝手にフィルタ(圧縮)形式を選ばないでくれ
  • -dColorImageFilter=/FlateEncode :
    カラー画像に関して、フィルタを FlateEncode にしてくれ(ZIPでくれ!)
  • -sOutputFile=output.pdf : 出力ファイル名
  • -c .setpdfwrite -f : でっかい文書を扱えるよう、仮想メモリを大きめに取るらしい
  • input.pdf : 入力ファイル名

調べているうちに、作成者やタイトルなどの情報も埋め込めることがわかった。方法は、まず

% Document information
[/CreationDate (D:20080331)
/Creator (Handmade)
/Title (Example)
/Subject (Example of how images in a PDF file are compressed with Ghostscript)
/Keywords (PDF Ghostscript Compression)
/Author (PDF Hanako)
/DOCINFO pdfmark

こんなものをテキストエディタで作り、info.ps のような名前を付けて保存する。そんで、gswin32c のオプションに加えてやれば良い。

gswin32c.exe -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -sOutputFile=output.pdf -c .setpdfwrite -f info.ps input.pdf

試しに Inkscape で 300×300 ピクセルの PNG 画像(71 KB) 1 枚を貼り付けただけの文書を作り、PDF で保存したところ、なんと 710 KB に。それを上記のコマンドで処理し直したら 66 KB になった。こうかはばつぐんだ!

Print This Post Print This Post

Leave a Reply