css - Wicked_pdf 样式表不适用于 Heroku

标签 css ruby-on-rails pdf 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"

如前所述,它在本地运行良好,但是当推送到 heroku 时,出现此错误:

ActionView::Template::Error (No such file or directory - /app/public/pdf.css)

我需要做什么才能在 heroku 上运行?

编辑:我找到了这个 Gihub 存储库 https://github.com/jordan-brough/heroku-pdf,这是一个在 heroku 上使用 wicked_pdf 的示例应用程序。在请求 PDF 时,通过调整 environment.rb 来提供来自 public 文件夹的 css 文件,这确实很有帮助。

最佳答案

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

Controller 中:

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

show.pdf.erb 中:

<% if params[:debug].present? %>
  <%= stylesheet_link_tag 'documents' %>
<% else %>
  <%= wicked_pdf_stylesheet_link_tag 'documents' %>
<% end %>
... rest of view ...

config/environments/production.rb 中:

config.assets.precompile += ['documents.css.scss.erb']

config/initializers/assets.rb

Rails.application.config.assets.precompile += %w( documents.css )

希望对大家有所帮助。

关于css - Wicked_pdf 样式表不适用于 Heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20525387/

相关文章:

javascript - HTML 刷新时的随机图像

ruby-on-rails - 将 RSpec 的 `all` 匹配器与 Capybara 的 `have_css` 匹配器一起使用时出错

ruby-on-rails - 使用 Rails 3 在 Ubuntu 上启动 nginx/passenger(非独立)的正确方法?

css - 有没有办法使用 Assets 管道拆分 CSS?

java - 管理大型 PDF 存储和流式传输

ruby - 在 Ruby Search PDF 中,突出显示找到的文本,导出页面的 JPG

html - 如何让 li 下拉菜单占据视口(viewport)的全宽?

html - float 的 div 没有在中间垂直对齐?

javascript - 单击时避免菜单切换关闭

PDF - 去除白边