I have a controller like this:
def show
@professor = Professor.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name"
end
end
end
And a simple view like this:
<p>Professor: <%= @professor.first_name %></p>
<p>Email: <%= @professor.email if @professor.email %></p>
I also have a layout 'application.html.erb';
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Myapp" %></title>
<meta name="description" content="">
<meta name="author" content="">
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= yield(:head) %>
</head>
<body>
<header class="navbar navbar-fixed-top">
<nav class="navbar-inner">
<div class="container">
<%= render 'layouts/navigation' %>
</div>
</nav>
</header>
<div id="main" role="main">
<div class="container">
<div class="content">
<div class="row">
<div class="span12">
<%= render 'layouts/messages' %>
<%= yield %>
</div>
</div>
<footer>
</footer>
</div>
</div> <!--! end of .container -->
</div>
When I do the following command:
bundle exec wkhtmltopdf 'http://local.myapp.com:3000/professors/2' - > test.pdf
I get a pdf with all the styles and layout properly.
However, when I go to http://local.myapp.com:3000/professors/2.pdf
I get an error saying:
Missing template professors/show with {:locale=>[:en], :formats=>[:pdf], :handlers=>[:erb, :builder, :coffee]}
So, then I changed my controller#show action to be like this:
def show
@professor = Professor.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name",
:template => 'professors/show.html.erb'
end
end
end
That helps me to render the view, but unfortunatelly it doesn't render the styles and the layout. My questions are:
You need to rename your view to show.pdf.html.erb or create a new show.pdf.erb.
The .pdf in the filename lets the handler know it can use it.
Also, you probably need to use the wicked_pdf_stylesheet_link_tag helper to get your styles to show up. If you want to have a dual-purpose view, then something like this may be necessary:
<% if params[:format] && params[:format] == 'pdf' %>
<%= wicked_pdf_stylesheet_link_tag 'application' %>
<% else %>
<%= stylesheet_link_tag 'application' %>
<% end %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With