我的设置:在MacOS X 10.10.1本地Rails 4.1.1和ruby 2.1.1p76以及Heroku上的prod .
我安装 gem 'wicked_pdf'
和 gem 'wkhtmltopdf-binary'
在 config/initializers/mime_types.rb
,我补充说:
Mime::Type.register "application/pdf", :pdf
在 config/initializers/wicked_pdf.rb
中,我尝试了两种方法:
:exe_path => '/usr/local/bin/wkhtmltopdf'
和
:exe_path => '#{Rails.root}/bin/wkhtmltopdf'
当我在终端跑
$ wkhtmltopdf http://google.com google.pdf
从我的app目录,它确实生成一个pdf文件 . 所以我假设wkhtmltopdf设置正确 .
我的 object_controller.erb
包含:
def print_view
@teams = [Team.find(params[:team_id])]
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name",
:layout => "/layouts/print_layout.html.erb"
end
end
end
我的 views/layouts/print_layout.html.erb
(为清晰起见缩短):
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<%= wicked_pdf_stylesheet_link_tag('print', :media => :print) %>
</head>
<body class="<%= controller_name %> <%= action_name%>">
<%= yield %>
</body>
</html>
而我的 views/object/print_view.html.erb
(为清晰起见缩短):
<div id="schedule-tables">
<%= render 'weekview_print' %>
</div>
我有两个问题:
-
我得到一个html页面,而不是pdf
-
页面使用的是默认布局,而不是指定的布局
1 回答
您应该将视图重命名为
print_view.pdf.erb
,因为您允许控制器中的两种格式为了获得PDF格式,您也可以使用
.pdf
扩展名调用相同的网址,例如/objects/print_view.pdf