wkhtmltopdfの高速化

1,078 views
Skip to first unread message

Takashi Okumura

unread,
Jul 4, 2012, 6:03:47 PM7/4/12
to fax...@googlegroups.com
おはようございます。

faxocrシステム中でhtmlからpdfを作るのにwkhtmltopdf
というコマンドを利用しているのですけれども、これがとても
遅くて難儀しています。

それで、ググってみると、随所で遅いという話があるのです
けれども、下記によると、193秒くらい掛かっていたのが、何
かをすると0.8秒くらいになるとのタレコミもあります。たしか
に、そこまで時間が掛かる処理ではないはずなので、どな
たか、この謎を解いて頂けるかたはおられませんでしょうか?

http://stackoverflow.com/questions/6290958/wkhtmltopdf-extremely-slow-on-debian-without-x-server

ベンチマークとしては、下記コマンドが早くなればとても
助かります。現状、11秒くらい掛かります。

faxocr@ubuntu:~/rails/files$ time xvfb-run -a wkhtmltopdf --page-size
A4 --orientation Landscape 2-201207041347.html 2-201207041347.pdf
Loading page (1/2)
Printing pages (2/2)
Done

real 0m15.526s
user 0m7.288s
sys 0m1.444s
faxocr@ubuntu:~/rails/files$ time wkhtmltopdf --page-size A4
--orientation Landscape 2-201207041347.html 2-201207041347.pdf
Loading page (1/2)
Printing pages (2/2)
Done

real 0m11.176s
user 0m7.284s
sys 0m1.252s

どうかよろしくお願いします!


奥村 貴史
2-201207041531.html

Tsutomu Miyazaki

unread,
Aug 4, 2012, 3:48:36 AM8/4/12
to fax...@googlegroups.com
HTMLの「opacity:0.8; 」を削除することで高速化されました。

修正対象のソースは以下のものになります。

:~/rails/app/external/sht_config.php

<div id="ex3" class="jqDnR" style="opacity:0.8; top:{$offsety}px; left:{$offsetx}px; z-index: 3; position: absolute; width: {$width}px; height:{$height}px; font-size: 12px; ">

<div id="ex3" class="jqDnR" style="top:{$offsety}px; left:{$offsetx}px; z-index: 3; position: absolute; width: {$width}px; height:{$height}px; font-size: 12px; ">

以下はベンチマークです。

修正前:
faxocr@ubuntu:~/test$ time wkhtmltopdf --page-size A4 -O Landscape --zoom 2.0 target.html test1.pdf
Loading page (1/2)
Printing pages (2/2)
Done

real 0m37.993s
user 0m17.501s
sys 0m7.740s

修正後:
faxocr@ubuntu:~/test$ time wkhtmltopdf --page-size A4 -O Landscape --zoom 2.0 target.html test1.pdf
Loading page (1/2)
Printing pages (2/2)
Done

real 0m1.290s
user 0m0.648s
sys 0m0.264s

上のコマンドでは使用していないのですが、xvfb-runをかますと少し遅くなりま
す(4秒くらいかかる)。

この修正で、生成されるPDFのサイズも小さくなります。。

修正前:
faxocr@ubuntu:~/test$ ls -al test1.pdf
-rw-r--r-- 1 faxocr faxocr 1884985 2012-08-04 15:13 test1.pdf

修正後:
faxocr@ubuntu:~/test$ ls -al test1.pdf
-rw-r--r-- 1 faxocr faxocr 20433 2012-08-04 15:11 test1.pdf

--
Tsutomu Miyazaki <tsum...@gmail.com>

Takashi Okumura

unread,
Aug 4, 2012, 6:48:09 AM8/4/12
to fax...@googlegroups.com
30倍の高速化、というのは、凄い発見ですね。確かに、
wkhtmltopdfとopacityの組み合わせは、問題が生じる
気があるようです。お使いのwkhtmltopdfのバージョンにも
依存するかとは思いますが、良いご指摘を、ありがとう
ございました。

http://code.google.com/p/wkhtmltopdf/issues/detail?id=821
http://code.google.com/p/wkhtmltopdf/issues/detail?id=730

xvfbで遅くなるのは、まあ、フレームバッファをエミュレート
しているので仕方が無いと思いますが、こちらもちょっと調査を
進めたいですね(とは言うものの、人手が足らずに進んでおらず、
申し訳ありません...)

ありがとうございました。


奥村 貴史

2012/8/4 Tsutomu Miyazaki <tsum...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages