LoginSignup
4
5

More than 5 years have passed since last update.

wicked pdfで画像サイズをリサイズして表示

Last updated at Posted at 2015-05-11

忙しい人向けに

sizeのハッシュを付けてやれば良い

<%= wicked_pdf_image_tag("image_file.jpg", {size: "200x50"}) %>

詳しく

PDFを出力してくれるgemであるwicked_pdfは大変便利だ。
他のPrawnのように独自DSLを使うことなくErbやHamlでPDFを生成出来て非常に効率がよい

wicked_pdfを使って画像を読み込ませる場合はimage_tagではなく、
独自のwicked_pdf_image_tagを使用する必要がある

例えばこんなふうに使うことができる。

<%= wicked_pdf_image_tag("image_file.jpg" %>

しかしこれだと、画像がそのままの大きさで表示されてしまい、画像の大きさを調整した場合には有効ではない。

wicked_pdf_image_tagメソッドのコードを読んで確認すると以下のようである。

wicked_pdf_helper.rb
 def wicked_pdf_image_tag(img, options = {})
    image_tag "file:///#{WickedPdfHelper.root_path.join('public', 'images', img)}", options
  end

ref: https://github.com/mileszs/wicked_pdf/blob/5fa2d705c650751d2cea1dc85ea0d2a1416b48a4/lib/wicked_pdf/wicked_pdf_helper.rb

要するにimage_tagを内部的に呼んでいて、optionsはimage_tagメソッドに直接渡しているようだ。
そこでimage_tagの使い方を見ると

image_tag(画像ファイルへのパス, [, (オプション or HTMLオプション)])
:size 画像サイズ(幅x高さ)

ref: http://railsdoc.com/references/image_tag

つまり、こんな感じでオプションを付けてsizeシンボルのハッシュに縦横のピクセルを記した文字列を追加してやればよいのだ

<%= wicked_pdf_image_tag("image_file.jpg", {size: "200x50"}) %>
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5