我在我的 Controller 中使用 PDFkit 来构建一系列 PDF,将它们压缩,然后将它们发送给用户。
为了控制输出样式,我告诉 PDFKit 在内容生成期间使用哪些样式表。我需要传递 CSS 文件的文件引用。由于 Rails 现在正在编译和重命名我的样式表,我不确定如何在我的 Controller 中引用编译后的 CSS Assets 。
这是我以前做的:
InvoicesController < ApplicationController
def download
kit = PDFKit.new(render_to_string(:show, :layout => false))
kit.stylesheets << "#{Sass::Plugin.options[:css_location]}/application.css"
kit.to_file("#{file_date_string}.pdf")
# snip
end
end
Sass::Plugin.options[:css_location] 现在返回不正确的位置,更不用说 application.css 不再是文件的有效名称。我应该提到我有一个 app/assets/application.css 文件作为我的 SCSS 文件的 list ,并且它通过 stylesheet_link_tag() 方法在我的 View 中正常工作。
基本上我正在寻找的是一个等效于asset_path()的 Controller ,以便执行以下操作:
kit = PDFKit.new(render_to_string(:show, :layout => false))
kit.stylesheets << asset_path('application.css')
kit.to_file("#{file_date_string}.pdf")
任何人都可以帮忙吗?
请您参考如下方法:
Rails.application.assets['application.css'].pathname
总是返回原始 Assets 的原始路径,而不是预编译文件,所以请您参考如下方法:对我不起作用。
但是,调用 to_s
在捆绑 Assets 上而不是 pathname
似乎确实正确地返回了预编译 Assets 的主体,因此您可以只使用内联样式而不是使用 kit.stylesheets <<
:<style> <%= Rails.application.assets["application.css"].to_s %> </style>