それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

WEBページ(html)をPDFにして保存するコマンド

WEBページをJPEG/PNGなど画像にして保存するにはwebkit2png を使うことが出来た

htmlをPDFに変換するには

webkittopdf というツールが有ります

Google Project Hosting

Debian/GNU Linuxからはらwebkittopdf

Raspbian で試しました。

aptitude install webkittopdf

webkit を x frame buffer で仮想バッファにXのデータを書き出してそれをPDFにします。

OSX の homebrew では

Mac では brew でインストールできます。

brew install wkhtmltopdf 

使い方

xvfb-run wkhtmltopdf  src.html  out.pdf

html*1をPDFに変換することが可能です。

Raspberry Pi でもPDFに直ぐに変換できたのでxfbはいまでは軽量に動くようです。

使ってみた結果

元のHTMLはこんな感じ

(web-html)

Webkit 経由でPDF

PDF出力した結果。フォントはXでデフォルトのフォントになる。

(web-pdf)

詳しい使い方など

takuya@raspberrypi:~$ xvfb-run wkhtmltopdf
You need to specify atleast one input file, and exactly one output file
Use - for stdin or stdout

Name:
  wkhtmltopdf 0.9.9

Synopsis:
  wkhtmltopdf [OPTIONS]... <input file> [More input files] <output file>

Description:
  Converts one or more HTML pages into a PDF document, *not* using wkhtmltopdf
  patched qt.

General Options:
      --collate                       Collate when printing multiple copies
      --copies <number>               Number of copies to print into the pdf
                                      file (default 1)
      --extended-help                 Display more extensive help, detailing
                                      less common command switches
  -h, --help                          Display help
  -O, --orientation <orientation>     Set orientation to Landscape or Portrait
  -s, --page-size <size>              Set paper size to: A4, Letter, etc.
      --password <password>           HTTP Authentication password
  -p, --proxy <proxy>                 Use a proxy
  -q, --quiet                         Be less verbose
      --username <username>           HTTP Authentication username
  -V, --version                       Output version information an exit

Reduced Functionality:
  This version of wkhtmltopdf has been compiled against a version of QT without
  the wkhtmltopdf patches. Therefore some features are missing, if you need
  these features please use the static version.

  Currently the list of features only supported with patch QT includes:

 * Printing more then one HTML document into a PDF file.
 * Running without an X11 server.
 * Adding a document outline to the PDF file.
 * Adding headers and footers to the PDF file.
 * Generating a table of contents.
 * Adding links in the generated PDF file.
 * Printing using the screen media-type.
 * Disabling the smart shrink feature of webkit.

Contact:
  If you experience bugs or want to request new features please visit
  <http://code.google.com/p/wkhtmltopdf/issues/list>, if you have any problems
  or comments please feel free to contact me: see
  <http://www.madalgo.au.dk/~jakobt/#about>

*1:わたしは、htmlに画像やスタイルを事前にインライン化しました。