【问题标题】:Wicked_pdf stylesheet not working on HerokuWicked_pdf 样式表在 Heroku 上不起作用
【发布时间】:2013-12-29 19:04:42
【问题描述】:

我有一个 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 repo https://github.com/jordan-brough/heroku-pdf,这是一个在 heroku 上使用 wicked_pdf 的示例应用程序。在请求 PDF 时,通过调整 environment.rb 以提供来自 public 文件夹的 css 文件确实很有帮助。

【问题讨论】:

    标签: css ruby-on-rails pdf heroku wicked-pdf


    【解决方案1】:

    有一个 GEM 可以让 wkhtmltopdf 二进制文件在 heroku 上运行,而无需在自己的存储库中安装任何二进制文件。它还包含用于开发的 OSX (darwin) 二进制文件。它适用于 PDFKit,可能也应该适用于 WickedPDF

    https://github.com/bradphelan/wkhtmltopdf-heroku
    
      or in your Gemfile as
        gem "wkhtmltopdf-heroku"
    

    【讨论】:

    • 二进制不是这里的问题,它可以很好地呈现 PDF,只是没有 css 文件。此 gem 仅添加二进制文件。
    【解决方案2】:

    我尝试了this 解决方案和wicked_pdf 工作。但评论:

    “激活运行时编译不是解决方案,因为 我们受到的性能打击”@David Tuite

    并在this 使用non-stupid-digest-assets gem 评论中找到答案,并且完美运行。

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,我花了一段时间才解决。除了正确处理调试选项之外,我最终使用了 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
      

      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 )
      

      希望这对某人有所帮助。

      【讨论】:

      • 谢谢。这为我修好了!
      • 非常有帮助。这应该在 wicked_pdf 的README.md
      猜你喜欢
      • 2012-09-16
      • 2011-08-06
      • 2021-11-20
      • 2011-12-13
      • 2019-01-24
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2019-03-20
      相关资源
      最近更新 更多