WickedPDF缺少布局 [英] WickedPDF missing layout

查看:114
本文介绍了WickedPDF缺少布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的控制器:

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>

我也有一个布局'application.html.erb';

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

我得到一份包含所有样式和布局的pdf。

I get a pdf with all the styles and layout properly.

然而,当我去 http://local.myapp.com:3000/professors/2.pdf 时,我得到一个错误:

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]}

我改变了我的控制器#show action就像这样:

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:


  1. 为什么需要指定模板?

  2. 为什么布局不在PDF中呈现?


推荐答案

您需要将您的视图重命名为show.pdf .html.erb或创建一个新的show.pdf.erb。

You need to rename your view to show.pdf.html.erb or create a new show.pdf.erb.

文件名中的.pdf让处理程序知道它可以使用它。

The .pdf in the filename lets the handler know it can use it.

另外,您可能需要使用wicked_pdf_stylesheet_link_tag助手来显示样式。如果你想有一个双重用途的视图,那么可能需要这样的东西:

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

这篇关于WickedPDF缺少布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆