【问题标题】:Ruby on rails wkhtmltopdf css style sheets not rendering on production (linux) serverRuby on rails wkhtmltopdf css 样式表不在生产(Linux)服务器上呈现
【发布时间】:2012-06-25 20:28:58
【问题描述】:

在本地开发环境(windows)中成功生成了css样式的pdf。但是在托管 linux 服务器环境中生成的 pdf 没有应用 css 样式。下面是我的 wkhtmltopdf (WickedPdf) 配置

WickedPdf.config = {
#:wkhtmltopdf => "#{RAILS_ROOT}/pdfbin/wkhtmltopdf-amd64",
:exe_path => "/home/software/.gems/bin/wkhtmltopdf",
:layout => "layouts/pdf.html.erb",
:margin => {    :top=> 40,
                :bottom => 20,
                :left=> 30,
                :right => 30},
:header => {:html => { :template=> 'layouts/pdf_header.html.erb'}},
:footer => {:html => { :template=> 'layouts/pdf_footer.html.erb'}}
#:exe_path => '/usr/bin/wkhtmltopdf'

}

更多信息:

这是我在 linux rails 托管 app\views\layouts 上的目录结构,在布局内部我有 pdf.html.erb、pdf_footer.html.erb、pdf_header.html.erb windows开发环境,但是在生产中生成的pdf没有样式。所以请大家帮我制作带有CSS样式的pdf

app/views/layouts/pdf.html.erb

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html dir="<%= (rtl?) ? 'rtl' : 'ltr' %>">
  <head>
    <% @direction = (rtl?) ? 'rtl/' : '' %>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <%= stylesheet_link_tag([@direction+'application', @direction+'_styles/ui.all'])%>
    <%= stylesheet_link_tag(*get_stylesheets) %>
    <%= stylesheet_link_tag  @direction+"_layouts/pdf" %>
    <link rel="stylesheet" type="text/css" href="<%="#{RAILS_ROOT}/public/stylesheets/#{@direction}_layouts/pdf.css" %>" media="all" />
    <link rel="stylesheet" type="text/css" href="<%="#{RAILS_ROOT}/public/stylesheets/#{get_stylesheets}.css"%>" media="all" />
    <link rel="stylesheet" type="text/css" href="<%= "#{RAILS_ROOT}/public/stylesheets/#{@direction}_styles/ui.all.css"%>" media="all" />


  </head>
  <body>

    <%= yield %>

  </body>
</html>

pdf.html.erb 包含渲染 pdf 的所有样式信息,在托管环境中,这些样式不是由 wkhtmltopdf 获取的。所以请大家帮帮我

【问题讨论】:

  • 无论您做什么,请先检查 wkthtmltopdf 库这将确保您在生产中出错的地方,而且我觉得样式表声明中的public 不需要指定您的hostname通过样式表路径忽略 public 方面 wkhtmltopdf 将为您获取这些 css
  • 嗨 Viren,我按照你的建议做了,但是输出的 pdf 没有 css 样式,有没有其他方法可以跟踪错误。为您提供开发环境中的信息 wkhtmltopdf lib 存在于我自己的 rails 应用程序中,例如 app/pdfbin/wkhtmltopdf.exe ,但在生产中我使用 wkhtmltopdf 提供的 bu 托管服务提供商 (/home/software/.gems/bin/wkhtmltopdf) ,这是我对本地和生产环境所做的唯一更改。
  • 在生成 pdf 时检查您的日志,查看对 css 的请求是否由 wkhtmltopdf 记录。另外,我还要求您在 wkhtmltopdf bcoz 中进行测试,过去我在 wkhtmltopdf 的各个版本中看到了不相似之处,所以只需检查您尝试实现的目标是否在该版本的 wkhtmltopdf 中得到实际支持

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 wkhtmltopdf


【解决方案1】:

当我在 Heroku 上部署我的应用程序时,我遇到了与 PDFKit 类似的问题。 现在,它工作正常(我已将二进制文件包含到项目中)。

在我的布局中:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Page title</title>
  <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <%= csrf_meta_tags %>
  <style type="text/css" media="all">
    <%= Rails.application.assets['my_css'].to_s.gsub(/\/assets/,"#{root_url}assets") %>
  </style>
</head>
<body>
  <%= yield %>
</body>
</html>

在初始化器中:

# config/initializers/pdfkit.rb
PDFKit.configure do |config|
  if Rails.env.staging? || Rails.env.production?
    config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s
  else
    config.wkhtmltopdf = '/Users/my_user_name/.rvm/gems/ruby-1.9.2-p0@my_project/bin/wkhtmltopdf'
  end
  config.default_options = {
    :page_size => 'A4',
    :margin_top => '0in',
    :margin_right => '0in',
    :margin_bottom => '0in',
    :margin_left => '0in',
    :orientation => 'Portrait'
  }
  # Use only if your external hostname is unavailable on the server.
  config.root_url = "http://localhost:3000" unless Rails.env.staging? || Rails.env.production?
end

也许这可以帮助你……或其他人……

【讨论】:

  • config.root_url = .... 行是我的 css 和图像出现的魔法酱。原因是我的生产服务器是一个 unix 套接字,手动指定一个 http 位置有助于它找到其他资源。
  • 很高兴我能帮上忙……以我无法想到的方式 ;-)
猜你喜欢
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-04
相关资源
最近更新 更多