2
votes

My set up : Rails 4.1.1 and ruby 2.1.1p76 locally on MacOS X 10.10.1 and prod on Heroku.

I installed gem 'wicked_pdf'and gem 'wkhtmltopdf-binary'

In config/initializers/mime_types.rb, I added :

Mime::Type.register "application/pdf", :pdf

In config/initializers/wicked_pdf.rb, I tried both :

 :exe_path => '/usr/local/bin/wkhtmltopdf'

and

:exe_path => '#{Rails.root}/bin/wkhtmltopdf'

When I run in Terminal

$ wkhtmltopdf http://google.com google.pdf

from my app directory, it does generate a pdf file. So I assume wkhtmltopdf is set up properly.

My object_controller.erb contains :

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

My views/layouts/print_layout.html.erb (shorten for clarity) :

<!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>

And my views/object/print_view.html.erb (shorten for clarity) :

<div id="schedule-tables">
    <%= render 'weekview_print' %>
</div>

I have 2 problems :

  1. I get a html page, not a pdf
  2. the page is using the default layout, not the specified layout
1

1 Answers

1
votes

you should rename the view into print_view.pdf.erb as you allow both formats in the controller

And for getting the pdf you also have call the same url with the .pdf extension e.g /objects/print_view.pdf