wicked_pdf +导轨'资产管道 +生产中的 sass 导入

发布于 2024-12-08 17:17:17 字数 780 浏览 0 评论 0原文

我在开发中成功地将 wicked_pdf 与 SASS 结合使用。我包含一个 .scss 文件,其中包含其他 .sass.scss 的多个 import 规则文件,通过此帮助程序:

def wicked_pdf_stylesheet_link_tag(*sources)
  sources.collect { |source|
    "<style type='text/css'>#{Rails.application.assets.find_asset("#{source}.css").body}</style>"
  }.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe
end

但切换到生产应用程序仍然会查找未找到的导入文件。

然后,我添加了第二个清单文件,要在 production.rb 中预编译 (config.assets.precompile += %w(pdf.css)),其中包含单个 require< /code> 规则来获取提到的 .scss 文件。该文件编译得很好,但似乎帮助程序没有在生产中选择正确的文件,并且仍然会加载导入的 .sass 文件。

有谁有经验如何解决这个问题? PDF 创建需要绝对路径,这使得这项任务变得更加困难。

I'm successfully using wicked_pdf with SASS in development. I'm including a single .scss file, which contains several import rules for other .sass and .scss files, via this helper:

def wicked_pdf_stylesheet_link_tag(*sources)
  sources.collect { |source|
    "<style type='text/css'>#{Rails.application.assets.find_asset("#{source}.css").body}</style>"
  }.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe
end

But switching to production the app still looks for the imported files which aren’t found.

I've added then a second manifest file to be pre–compiled in production.rb (config.assets.precompile += %w(pdf.css)) which contains a single require rule to pick up the mentioned .scss file. This file is compiled just fine but it seems that the helper doesn't pick up the right file in production and still looks to load the imported .sass files.

Has anyone experience how to solve this? The PDF creation requires absolute paths, which makes this task a bit more difficult.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望喜 2024-12-15 17:17:17

我有一份邪恶的pdf文件正在开发和制作中。这是我的 wicked_pdf 配置的核心:

我已经根据 wicked_pdf pull request 来自 github 用户 antti

module WickedPdfHelper
  def wicked_pdf_stylesheet_link_tag(*sources)
    sources.collect { |source|
      "<style type='text/css'>#{Rails.application.assets.find_asset(source+".css")}</style>"
    }.join("\n").html_safe
  end

  def wicked_pdf_image_tag(img, options={})
    asset = Rails.application.assets.find_asset(img)
    image_tag "file://#{asset.pathname.to_s}", options
  end

  def wicked_pdf_javascript_src_tag(jsfile, options={})
    asset = Rails.application.assets.find_asset(jsfile)
    javascript_include_tag "file://#{asset.pathname.to_s}", options
  end

  def wicked_pdf_javascript_include_tag(*sources)
    sources.collect{ |source| "<script type='text/javascript'>#{Rails.application.assets.find_asset(source+".js")}</script>" }.join("\n").html_safe
  end
end

然后在 app/assets/stylesheets/pdf.css 中我需要一些 sass 样式表:

/* ...
*= require ./_colors
*= require_directory ./pdf
*= require_self
*/

(请记住,如果您要修改初始化程序或 config/ 中的任何内容,则需要重新启动 Rails 应用程序才能引入更改)

I have wicked pdf working in development and production. This is the core of my wicked_pdf config:

I've updated WickedPdfHelper (loaded from initializers/wicked_pdf.rb) based on a wicked_pdf pull request from github user antti

module WickedPdfHelper
  def wicked_pdf_stylesheet_link_tag(*sources)
    sources.collect { |source|
      "<style type='text/css'>#{Rails.application.assets.find_asset(source+".css")}</style>"
    }.join("\n").html_safe
  end

  def wicked_pdf_image_tag(img, options={})
    asset = Rails.application.assets.find_asset(img)
    image_tag "file://#{asset.pathname.to_s}", options
  end

  def wicked_pdf_javascript_src_tag(jsfile, options={})
    asset = Rails.application.assets.find_asset(jsfile)
    javascript_include_tag "file://#{asset.pathname.to_s}", options
  end

  def wicked_pdf_javascript_include_tag(*sources)
    sources.collect{ |source| "<script type='text/javascript'>#{Rails.application.assets.find_asset(source+".js")}</script>" }.join("\n").html_safe
  end
end

then in app/assets/stylesheets/pdf.css I require a few sass stylesheets:

/* ...
*= require ./_colors
*= require_directory ./pdf
*= require_self
*/

(remember that if you're modifying initializers or anything in config/, you'll need to re-start your rails app to pull in the changes)

缺⑴份安定 2024-12-15 17:17:17

我在以下位置写了一篇文章:http://anlek。 com/2011/09/wicked_pdf-working-in-rails-3-1/

它与 Philip 的解决方案非常相似,但做了一些修改。

I wrote a article on this at: http://anlek.com/2011/09/wicked_pdf-working-in-rails-3-1/

It's very similar to Philip's solution with a few modifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文