Wicked_pdf样式表不适用于Heroku [英] Wicked_pdf stylesheet not working on Heroku

查看:100
本文介绍了Wicked_pdf样式表不适用于Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,其中使用wicked_pdf生成PDF.一切都很好并且可以在本地工作,但是当将其推送到heroku时,PDF会呈现,但不应用样式表.

I have a Rails app where I use wicked_pdf to generate PDF's. This is all fine and working locally, but when pushed to heroku the PDF does render, but without applying the stylesheet.

尤其对于PDF渲染,我有一个CSS文件:app/assets/stylesheets/pdf.css.scss.在form.pdf.haml中,我像这样加载样式表:

Especially for PDF rendering I have a CSS file: app/assets/stylesheets/pdf.css.scss. And in form.pdf.haml I load the stylesheet like this:

  !!!
  %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时,出现此错误:

As said, it works great locally, but when pushed to heroku, I get this error:

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

要在Heroku上进行这项工作,我需要做什么?

What do I have to do to make this work on heroku?

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

I found this Gihub repo https://github.com/jordan-brough/heroku-pdf, which is an example app for using wicked_pdf on heroku. It really helped by adapting the environment.rb to serve a css file from the public folder when requesting a PDF.

推荐答案

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

I had a similar problem and it took me a while to crack. I ended up using the wkhtmltopdf-heroku gem and the following setup which works both locally and on heroku in addition to proper handling of the debug option:

控制器中:

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 )

希望这会对某人有所帮助.

Hope that this will be of help to someone.

这篇关于Wicked_pdf样式表不适用于Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆