Wicked_PDF无法在生产中呈现通过Asset Sync托管的HTML和CSS资产

时间:2013-04-23 01:03:40

标签: heroku amazon-s3 asset-pipeline wkhtmltopdf wicked-pdf

我在使用Wicked_PDF渲染PDF时遇到问题。我正在使用Amazon S3和CloudFront托管我的所有资产管道资源,并且在开发过程中生成的东西很好;但是在生产中,HTML和CSS资产不会在生成的PDF中呈现。

我正在运行Rails 3.2.11,Ruby 1.9.3p327,MAC OSX 10.8.2,wkhtmltopdf 0.9.9,以及当前版本的Wicked_PDF和wkhtmltopdf-heroku。

以下是所有相关信息:

Heroku Logs:

2013-04-21T14:13:19.482011+00:00 app[web.1]: Completed 302 Found in 212ms (ActiveRecord: 205.6ms)
2013-04-21T14:13:19.584108+00:00 app[web.1]: Started GET "/office/download/7" for ip_address at 2013-04-21 14:13:19 +0000
2013-04-21T14:13:21.551581+00:00 app[web.1]: Processing by OfficesController#show_pdf as HTML
2013-04-21T14:13:21.551581+00:00 app[web.1]: ***************WICKED***************
2013-04-21T14:13:21.551581+00:00 app[web.1]:   Parameters: {"office_id"=>"7"}
2013-04-21T14:13:21.551581+00:00 app[web.1]:   Rendered appointments/forms/_office.pdf.html.erb within layouts/pdf.html (13.6ms)
2013-04-21T14:13:21.551581+00:00 app[web.1]:   Rendered text template (0.0ms)
2013-04-21T14:13:21.551581+00:00 app[web.1]: Sent data Office.pdf (42.2ms)
2013-04-21T14:13:21.551581+00:00 app[web.1]: Completed 200 OK in 1961ms (Views: 41.9ms | ActiveRecord: 7.8ms)
2013-04-21T14:13:21.554211+00:00 heroku[router]: at=info method=GET path=/office/download/7         
host=www.foobar.com fwd="ip_address" dyno=web.1 connect=1ms service=1971ms status=200 bytes=22019

路线:

# => Route that generates the PDF

get "/office/download/:office_id" => 'offices#show_pdf'

控制器:

# => app/controllers/offices_controller.rb

def show_pdf
  @office = Office.find_by_id(params[:office_id])
  abrv = @office.abrv
  partial = "appointments/forms/_#{abrv}"
  @format = "pdf"

  respond_to do |format|
    format.pdf do
      render :pdf => "#{@office.name}",
             :template => "#{partial}.pdf.html.erb",
             :layout => "pdf.html",
             :page_size => "A4",
             :encoding => "UTF-8",
             :show_as_html => params[:debug].present?
     end
  end

end

查看:

# => app/views/offices/index

<div>
  # List of all offices
</div>

# PDF partials for each office

<%= render 'appointments/forms/office.pdf.html.erb' %>
<%= render 'appointments/forms/office_1.pdf.html.erb' %>
...

PDF模板:

# => app/views/appointments/forms/_office.pdf.html.erb:

<% if @format != 'pdf' %>
  <%= stylesheet_link_tag 'office' # => .css.scss.erb %>
<% else %>
  <%= wicked_pdf_stylesheet_link_tag 'office' # => .css.scss.erb %>
<% end %>

<div class="pdf_styling">
  <p>Bar</p>
</div>

CSS:

# => app/assets/stylesheets/office.css.scss.erb

.pdf_styling {
  background-image: url('https://s3.amazonaws.com/foo/assets/pdf_background_image.jpg');
  width: 100%;
  height: 100%;

  p {
    position: absolute;
    left: 100px;
    right: 100px;
  }
}

Wicked_PDF初始化程序:

# => config/initializers/wicked_pdf.rb

WICKED_PDF = {
  :wkhtmltopdf => (Rails.env.test? || Rails.env.development? ? '/usr/local/bin/wkhtmltopdf' : Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s),
  :exe_path => (Rails.env.test? || Rails.env.development? ? '/usr/local/bin/ ' : Rails.root.join('vendor','bin', 'wkhtmltopdf-amd64').to_s)
}

的Gemfile:

# PDF Gem
gem 'wicked_pdf'

# PDF to HTML utility
gem 'wkhtmltopdf-binary'

# PDF on heroku
group :production do
  gem "wkhtmltopdf-heroku", :git => 'git://github.com/camdez/wkhtmltopdf-heroku.git'
  gem 'thin'
end

# Asset Pipeline Optimization
gem "asset_sync"   

同样,一切都在开发中运行良好,但我只是在生产中得到一个空白的PDF页面。日志中不会引发任何错误。这可能是dynos太少的问题吗?

我承认这是所有这一切的n00b;原谅我。感谢。

1 个答案:

答案 0 :(得分:4)

在Heroku中部署到生产时遇到了同样的问题。

要解决的两个可能区域......首先,必须将每个Javascript或CSS资源文件指定为绝对URL,而不仅仅是您在模板代码中指定的URL。如果您在其他地方引用了任何JS / CSS文件,则还必须以绝对形式指定这些文件。因此,您还需要查看您的pdf布局文件

其次(这就是我在Heroku上的诀窍),我最后必须在config / application.rb中指定我的每一个资源:

config.assets.precompile += ['blueprint/screen.css', 'jquery.timepicker.css', 'pdf.css', 'jquery.ui.datepicker.js', 'pdf.js', 'jquery.autosize.js', 'jquery.timepicker.js']

我不确定为什么第二步是必需的,但没有它,Heroku上没有任何工作:我得到了相同的空白PDF,没有你报告的错误,甚至认为一切都在开发中。我的经验是,如果存在资源问题,wkhtmltopdf将无声地失败。我在博客上发布了问题here,并推出了对WickedPDF README文档的更新,这可能会有所帮助。