【问题标题】:CSS don't work on wicked_pdfCSS 不适用于 wicked_pdf
【发布时间】:2016-02-29 18:15:26
【问题描述】:

我正在使用 wicked_pdf,但遇到了问题。 css 不工作。如何正确使用?

我的代码是这样的:

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>WickedPdfExample</title>
  <%= wicked_pdf_stylesheet_link_tag "bootstrap.min.css" -%>
  <%= wicked_pdf_stylesheet_link_tag "application.css" -%>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
  <div class="container">
    <%= yield %>
  </div>
</body>
</html>

layouts/pdf.html.erb

<!DOCTYPE html>
<html>
<head>
<title>PDF</title>
  <%= wicked_pdf_stylesheet_link_tag "bootstrap" -%>
</head>
<body>

  <div class='container'>
    <%= yield %>
  </div>

</body>
</html>

customers_controller.erb

  def index
    @customers = Customer.all
    respond_to do |format|
      format.html
      format.pdf do
        render :pdf => 'file_name',
        :layout => 'layouts/pdf.html.erb'
        :template => 'customers/index.pdf.erb',
        :show_as_html => params[:debug].present?
      end
    end
  end

index.pdf.erb

<h1>Listing Customers</h1>

<table class="table table-condensed table-bordered">
  <thead>
    <tr>
      <th>Name</th>
      <th>Cpf</th>
      <th>Occupation</th>
    </tr>
  </thead>

  <tbody>
    <% @customers.each do |customer| %>
      <tr>
        <td><%= customer.name %></td>
        <td><%= customer.cpf %></td>
        <td><%= customer.occupation %></td>
      </tr>
    <% end %>
  </tbody>
</table>

有时,会出现此错误:

未定义的方法`start_with?'对于 nil:NilClass

我做错了什么?

但是,我的引导程序不收费。我都试过了!!!

【问题讨论】:

  • 我最近经常看到这种情况。显然这是由您的 css 文件中的 url(.*) 之一引起的。任何机会你的项目是开源的,所以我可以更深入地调试它(因为我什至很难想象 wicked_pdf gem 中的代码可能会失败的场景)。
  • 供参考,有一个未解决的问题:github.com/mileszs/wicked_pdf/issues/470
  • 这是 git:github.com/eltonsantos/wicked_pdf_example,请拉取这个请求。我尝试了所有,但没有任何效果!
  • 它对我有用。 :( 你使用的是哪个 ruby​​ 版本?
  • ruby 2.3.0,rails 4.2.5 =l

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


【解决方案1】:

我的 CSS 是这样工作的:

<%= stylesheet_link_tag "http://#{request.host_with_port}/#{asset_path("application.css")}", rel: "stylesheet", media: "all" %>

【讨论】:

    猜你喜欢
    • 2013-01-06
    • 2011-12-13
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 2018-12-26
    相关资源
    最近更新 更多