log

showoff で PDF を作成する

markdown 形式のテキストからスライドを自動生成してくれる showoff で、PDF 生成するのにアレコレ修正する必要があったのでメモ。
環境は以下。


  • Mac OSX 10.7.2

  • Ruby 1.9.2-p180

  • showoff 0.7.0

  • pdfkit 0.5.2

  • wkhtmltopdf 0.8.3


ちなみに Ruby 周りは rvm で、wkhtmltopdf は brew で入れた。

showoff のドキュメントによると http://localhost:9090/pdf にアクセスすると PDF が出力されるとのことだけど、実際アクセスされるとエラーが出た。
showoff のログを見てみると以下を発見。

 
Unknown long argument --quiet

Usage: wkhtmltopdf [OPTION]... <input file> [more input files] <output file>
converts htmlpages into a pdf


おそらく showoff が wkhtmltopdf に渡すオプションを間違ってるんだろなってことで、showoff のソースを追ってみる。

そうすると、どうやら PDF 生成は pdfkit に任せてるってことがわかった(390行目辺り)。



382 def pdf(static=true)
383 @slides = get_slides_html(static, true)
384 @no_js = false
385 html = erb :onepage
386 # TODO make a random filename
387
388 # PDFKit.new takes the HTML and any options for wkhtmltopdf
389 # run `wkhtmltopdf --extended-help` for a full list of options
390 kit = PDFKit.new(html, :page_size => 'Letter', :orientation => 'Landscape')
391
392 # Save the PDF to a file
393 file = kit.to_file('/tmp/preso.pdf')
394 end

というわけで、今度は pdfkit のソースを読んでみる。

「quiet」で grep かけてみるとそれらしい箇所が出てきたのでコメントアウトした(35行目辺り)。



32 def command(path = nil)
33 args = [executable]
34 args += @options.to_a.flatten.compact
35 #args << '--quiet' # 不要なオプションなのでコメントアウト
36
37 if @source.html?
38 args << '-' # Get HTML from stdin
39 else
40 args << @source.to_s
41 end
42
43 args << (path || '-') # Write to file or stdout
44
45 args.map {|arg| %Q{"#{arg.gsub('"', '\"')}"}}
46 end

この状態で改めて http://localhost:9000/pdf にアクセスすると、今度はちゃんと PDF が生成された。