Wicked_pdf样式表不适用于Heroku

Joh*_*ohn 4 css pdf ruby-on-rails heroku wicked-pdf

我有一个Rails应用程序,我使用wicked_pdf生成PDF.这一切都很好并在本地工作,但是当推送到heroku时,PDF会渲染,但不应用样式表.

特别是对于PDF渲染,我有一个CSS文件:app/assets/stylesheets/pdf.css.scss.在form.pdf.haml我加载这样的样式表:

  !!!
  %html{lang: "en", "xml:lang" => "en", xmlns: "http://www.w3.org/1999/xhtml"}
    %head
      %meta{content: "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
      = wicked_pdf_stylesheet_link_tag "pdf"
Run Code Online (Sandbox Code Playgroud)

如上所述,它在本地工作得很好,但是当推送到heroku时,我收到这个错误:

ActionView::Template::Error (No such file or directory - /app/public/pdf.css)
Run Code Online (Sandbox Code Playgroud)

我需要做什么才能在heroku上完成这项工作?

编辑:我发现了这个Gihub repo https://github.com/jordan-brough/heroku-pdf,这是一个在heroku上使用wicked_pdf的示例应用程序.通过在请求PDF时调整environment.rb文件public夹中的css文件来帮助它真的很有帮助.

小智 6

我有类似的问题,我花了一段时间才解决.我最终使用了wkhtmltopdf-heroku gem以及以下设置,除了正确处理调试选项外,还可以在本地和heroku上运行:

在控制器中:

respond_to do |format|
  format.html
  format.pdf do
    render pdf: @profile.name + Date.today.to_s(:number),
           background: true,
           encoding: 'utf8',
           :show_as_html => params[:debug].present?
  end
end
Run Code Online (Sandbox Code Playgroud)

在show.pdf.erb中:

<% if params[:debug].present? %>
  <%= stylesheet_link_tag 'documents' %>
<% else %>
  <%= wicked_pdf_stylesheet_link_tag 'documents' %>
<% end %>
... rest of view ...
Run Code Online (Sandbox Code Playgroud)

在config/environments/production.rb中:

config.assets.precompile += ['documents.css.scss.erb']
Run Code Online (Sandbox Code Playgroud)

在config/initializers/assets.rb中

Rails.application.config.assets.precompile += %w( documents.css )
Run Code Online (Sandbox Code Playgroud)

希望这对某人有所帮助.