ラベル OSS の投稿を表示しています。 すべての投稿を表示
ラベル OSS の投稿を表示しています。 すべての投稿を表示

2013年11月27日水曜日

CentOS 5.x wkhtmltoimage, wkhtmltopdf (ウェブサイトキャプチャー)

WebKitのレンダリングエンジンを使用してPDFや画像に変換するための簡単なシェル·ユーティリティー。

wkhtmltoimage, wkhtmltopdfは、指定されたWebサイトをキャプチャーし、画像またはPDFなどで保存することが可能です。

wkhtmltoimageのインストール

[root@centos5 ~]# /usr/bin/php -i | grep 'GD Support'
GD Support => enabled
[root@centos5 ~]# cd /usr/local/src/
[root@centos5 src]# wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2
--2013-12-31 23:23:59--  http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2
wkhtmltopdf.googlecode.com をDNSに問いあわせています... 74.125.128.82, 2404:6800:4005:c00::52
wkhtmltopdf.googlecode.com|74.125.128.82|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 11393207 (11M) [application/octet-stream]
`wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2' に保存中

100%[=====================================>] 11,393,207  3.10M/s 時間 3.5s

2013-12-31 23:23:59 (3.10 MB/s) - `wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2' へ保存完了 [11393207/11393207]

[root@centos5 src]# tar -jxvf ./wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2
wkhtmltoimage-i386
[root@centos5 src]# mv wkhtmltoimage-i386 /usr/local/bin/wkhtmltoimage
[root@centos5 src]# chown root:root /usr/local/bin/wkhtmltoimage
[root@centos5 src]# ls -l /usr/local/bin/wkhtmltoimage
-rwxr-xr-x 1 root root 11424736 10月  3  2011 /usr/local/bin/wkhtmltoimage
[root@centos5 src]# cd ~
[root@centos5 ~]# wkhtmltoimage -h
Name:
  wkhtmltoimage 0.11.0 rc1

Synopsis:
  wkhtmltoimage [OPTIONS]... <input file> <output filegt;

Description:
  Converts an HTML page into an image,

General Options:
      --crop-h <int>                  Set height for croping
      --crop-w <int>                  Set width for croping
      --crop-x <int>                  Set x coordinate for croping
      --crop-y <int>                  Set y coordinate for croping
  -H, --extended-help                 Display more extensive help, detailing
                                      less common command switches
  -f, --format <format>               Output file format
      --height <int>                  Set screen height (default is calculated
                                      from page content) (default 0)
  -h, --help                          Display help
      --quality <int>                 Output image quality (between 0 and 100)
                                      (default 94)
  -V, --version                       Output version information an exit
      --width <int>                   Set screen width, note that this is used
                                      only as a guide line. Use
                                      --disable-smart-width to make it strict.
                                      (default 1024)

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: <uuf6429@gmail.com>

[root@centos5 ~]# wkhtmltoimage --quality 100 http://google.co.jp/ /var/www/html/google.co.jpg
Loading page (1/2)
Rendering (2/2)
QPixmap: Cannot create a QPixmap when no GUI is being used   ] 25%
~中略~
QPixmap: Cannot create a QPixmap when no GUI is being used
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::translate: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setWorldTransform: Painter not active
QPainter::setOpacity: Painter not active
QPainter::setFont: Painter not active
セグメンテーション違反です

セグメンテーション違反です
セグメンテーション違反です
セグメンテーション違反です
セグメンテーション違反です
(。A。)

情報を収集してみたところ、最新版でこの問題が発生している様子でした。
既存ファイルのパーミッション状態が原因でも発生する様ですね。
 (解決が大変そう)なので、ここは旧バージョンをインストールすることにしました。

[root@centos5 ~]# cd /usr/local/src/
[root@centos5 src]# wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2
--2013-12-31 23:23:59--  http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2
wkhtmltopdf.googlecode.com をDNSに問いあわせています... 74.125.128.82, 2404:6800:4005:c00::52
wkhtmltopdf.googlecode.com|74.125.128.82|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 11217033 (11M) [application/x-bzip2]
`wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2' に保存中

100%[==========================================================================>] 11,217,033  2.71M/s 時間 4.6s

2013-12-31 23:23:59 (2.30 MB/s) - `wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2' へ保存完了 [11217033/11217033]

[root@centos5 src]# tar -jxvf ./wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2
wkhtmltoimage-i386
[root@centos5 src]# chown root:root ./wkhtmltoimage-i386
[root@centos5 src]# mv ./wkhtmltoimage-i386 /usr/local/bin/wkhtmltoimage
[root@centos5 src]# cd ~
[root@centos5 ~]# wkhtmltoimage http://yahoo.co.jp /var/www/html/yahoo.co.jpg
[root@centos5 ~]# wkhtmltoimage --quality 100 http://google.co.jp/ /var/www/html/google.co.jpg

wkhtmltoimageのインストール

前項のwkhtmltoimageでの反省を踏まえ、こちらも旧バージョンをインストールしています。

[root@centos5 ~]# cd /usr/local/src/
[root@centos5 src]# wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2
--2013-12-31 23:23:59--  http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2
wkhtmltopdf.googlecode.com をDNSに問いあわせています... 74.125.128.82, 2404:6800:4005:c00::52
wkhtmltopdf.googlecode.com|74.125.128.82|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 11240983 (11M) [application/x-bzip2]
`wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2' に保存中

100%[==========================================================================>] 11,240,983  4.55M/s 時間 2.4s

2013-12-31 23:23:59 (4.55 MB/s) - `wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2' へ保存完了 [11240983/11240983]

[root@centos5 src]# tar -jxvf ./wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2
wkhtmltopdf-i386
[root@centos5 src]# chown root:root ./wkhtmltopdf-i386
[root@centos5 src]# mv ./wkhtmltopdf-i386 /usr/local/bin/wkhtmltopdf
[root@centos5 src]# ls -l /usr/local/bin/wkhtmltopdf
-rwxr-xr-x 1 root root 11272224  1月 31  2011 /usr/local/bin/wkhtmltopdf
[root@centos5 src]# cd ~
[root@centos5 ~]# wkhtmltopdf -h
Name:
  wkhtmltopdf 0.10.0 rc2

Synopsis:
  wkhtmltopdf [GLOBAL OPTION]... [OBJECT]... <output file>

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

Global Options:
      --collate                       Collate when printing multiple copies
                                      (default)
      --no-collate                    Do not collate when printing multiple
                                      copies
      --copies <number>               Number of copies to print into the pdf
                                      file (default 1)
  -H, --extended-help                 Display more extensive help, detailing
                                      less common command switches
  -g, --grayscale                     PDF will be generated in grayscale
  -h, --help                          Display help
  -l, --lowquality                    Generates lower quality pdf/ps. Useful to
                                      shrink the result document space
  -O, --orientation <orientation>     Set orientation to Landscape or Portrait
                                      (default Portrait)
  -s, --page-size <Size>              Set paper size to: A4, Letter, etc.
                                      (default A4)
  -q, --quiet                         Be less verbose
      --read-args-from-stdin          Read command line arguments from stdin
      --title <text>                  The title of the generated pdf file (The
                                      title of the first document is used if not
                                      specified)
  -V, --version                       Output version information an exit

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>

[root@centos5 ~]# wkhtmltopdf http://google.co.jp/ /var/www/html/google.co.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done