WickedPdf
SqaleでWickedPdfを使えた。
もともとSqaleサーバーにはwkhtmltopdfはインストールされていないようだが、Gemでバイナリをインストールしてしまうことで簡単に使えるようになる。'wkhtmltopdf-binary' gem。
Gemfile
gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
hoges_controller.rb
def show
respond_to do |format|
format.html{ redirect_to hoge_path(@hoge,format: :pdf,debug: 1)}
format.json { render json: @hoge }
format.pdf do
render :pdf=> "hoge_test_pdf",:encoding =>'utf-8',:layout=>'pdf.html',:footer=>{ :center=>'[page] / [topage]'},show_as_html: params[:debug].present?
end
end
end
参考サイトの方が考えた仕掛けを使わせていただく。感謝。
app/views/layout/pdf.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<%= wicked_pdf_stylesheet_link_tag "pdf" %>
</head>
<body>
<div id="content">
<%= yield %>
</div>
</body>
</html>
app/assets/stylesheets/pdf.css.scss
ご自由に。
config/environments/production.rb
config.assets.precompile += %w( pdf.css )
app/views/hoges/show.pdf.erb
ご自由に。
参考サイト