Rails 4上的wicked_pdf没有生成pdf

时间:2014-12-29 17:08:31

标签: ruby-on-rails-4 wkhtmltopdf wicked-pdf

我的设置:在MacOS X 10.10.1上本地Rails 4.1.1和ruby 2.1.1p76并在Heroku上生成。

我安装了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>

我有两个问题:

  1. 我得到一个html页面,而不是pdf
  2. 页面使用的是默认布局,而不是指定的布局

1 个答案:

答案 0 :(得分:1)

您应该将视图重命名为print_view.pdf.erb,因为您允许控制器中的两种格式

为了获得pdf,您还可以使用.pdf扩展名调用相同的网址,例如  /objects/print_view.pdf

相关问题