使用 PDFKit 的 Rails 3.1 资产管道

发布于 2024-12-16 17:02:00 字数 556 浏览 10 评论 0原文

我正在使用带有 Rails 3.1 的 PDFkit。过去,我能够使用 render_to_string 函数并从该字符串创建 pdf。然后我添加样式表,如下所示。我的问题是我不知道如何从资产管道内访问它们。 (这就是我在 Rails 3.0 中所做的)

html_string = render_to_string(:template => "/faxes/show.html.erb", :layout => 'trade_request')
kit = PDFKit.new(html_string, :page_size => 'Letter')
kit.stylesheets << "#{Rails.root.to_s}/public/stylesheets/trade_request.css"

所以我的问题是如何通过 asset pipline 从控制器直接访问我的 css 文件?

我知道我可以使用带有 PDFkit 的机架中间件将 pdf 呈现到浏览器,但在这种情况下,我需要将 pdf 发送到第三方传真服务。

感谢您的帮助。

瑞安

I am using PDFkit with rails 3.1. In the past I was able to use the render_to_string function and create a pdf from that string. I then add the stylesheets as follows. My issue is that I have no idea how to access them from within the asset pipeline. (This is how I did it in rails 3.0)

html_string = render_to_string(:template => "/faxes/show.html.erb", :layout => 'trade_request')
kit = PDFKit.new(html_string, :page_size => 'Letter')
kit.stylesheets << "#{Rails.root.to_s}/public/stylesheets/trade_request.css"

So my question in how do i get direct access from my controller to my css file through the asset pipline?

I know I can use the Rack Middleware with PDFkit to render the pdf to the browser, but in this case i need to send the pdf off to a third party fax service.

Thanks for your help.

Ryan

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

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

发布评论

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

评论(7

初熏 2024-12-23 17:02:00

刚刚也遇到了这个问题,我没有使用资产管道就解决了这个问题,而是像以前在 /public 中一样直接访问文件。不知道使用这种方法可能有什么缺点。

我猜想 LESS 和 SCSS 文件不会像通过资产管道访问时那样得到处理。

      html = render_to_string(:layout => false , :action => 'documents/invoice.html.haml')
      kit = PDFKit.new(html, :encoding=>"UTF-8")
      kit.stylesheets << "#{Rails.root.to_s}/app/assets/stylesheets/pdf_styles.css"
      send_data(kit.to_pdf, :filename => "test_invoice", :type => 'application/pdf')

Just ran into this issue as well, and I got past it without using the asset pipeline, but accessing the file directly as I would have previously in /public. Don't know what the possible cons are to using this approach.

I guess LESS and SCSS files won't be processed as they would have if accessed through the asset pipeline.

      html = render_to_string(:layout => false , :action => 'documents/invoice.html.haml')
      kit = PDFKit.new(html, :encoding=>"UTF-8")
      kit.stylesheets << "#{Rails.root.to_s}/app/assets/stylesheets/pdf_styles.css"
      send_data(kit.to_pdf, :filename => "test_invoice", :type => 'application/pdf')
装迷糊 2024-12-23 17:02:00

有点晚了,但迟到总比不到好,嗯。

我会这样做:

found_asset = Rails.application.assets.find_asset( "trade_request.css" ).digest_path
kit.stylesheets << File.join( Rails.root, "public", "assets", found_asset )

A little late, but better late than never, eh.

I'd do it like this:

found_asset = Rails.application.assets.find_asset( "trade_request.css" ).digest_path
kit.stylesheets << File.join( Rails.root, "public", "assets", found_asset )
墨离汐 2024-12-23 17:02:00

在 Rails 3.1.1 中,样式表被写入 /public/assets 中,无论是否带有摘要指纹。

这意味着您应该能够仅通过更改代码中的路径来引用这些文件。

但有一个问题:如果 CSS 清单中未引用 PDF 工作表,则必须将其添加到预编译配置中:


config.assets.precompile += ['trade_request.css']

这告诉 sprockets 自行编译该文件。

作为(更好的)替代方案,请查看 asset_path 帮助程序是否在您的代码中工作。这将在开发和生产中引用正确的文件。

In Rails 3.1.1 stylesheets are written to /public/assets with and without the digest fingerprint.

This means that you should be able to reference these files just by changing the path in your code.

One gotcha though: if the PDF sheet is not referenced in a CSS manifest you'll have to add it to the precompile config:


config.assets.precompile += ['trade_request.css']

This tells sprockets to compile that file on its own.

As a (better) alternative, see if the asset_path helper works in your code. This will reference the correct file in dev and production.

野心澎湃 2024-12-23 17:02:00

我最终将 css 文件复制到我的公共目录中,并以与之前使用 Rails 3 相同的方式引用它。有关更多信息,请查看此问题: 从控制器访问 stylesheet_link_tag

I ended up copying the css file to my public directory and referring to it in the same way i did before with rails 3. For more information check out this question: Access stylesheet_link_tag from controller

欢烬 2024-12-23 17:02:00

我来到这里试图解决这个问题,但似乎没有一个答案可以为我解决问题。我发现这个堆栈溢出帖子的接受答案对我有用:

如何从 Rails 3.1 中的控制器引用已编译的资源?

我什至能够通过这种方法提供 .css.erb 文件。

I landed here trying to solve this problem and none of the answers seemed to solve the issue for me. I found the accepted answer of this stack overflow post worked for me:

How does one reference compiled assets from the controller in Rails 3.1?

I was even able to serve .css.erb files through this method.

如痴如狂 2024-12-23 17:02:00

尝试

= stylesheet_link_tag "application", 'data-turbolinks-track': 'reload'

Try

= stylesheet_link_tag "application", 'data-turbolinks-track': 'reload'

白龙吟 2024-12-23 17:02:00

您应该能够使用以下方法访问样式表:

ActionController::Base.helpers.asset_path("trade_request.css")

编写代码:

html_string = render_to_string(:template => "/faxes/show.html.erb", :layout => 'trade_request') 
kit = PDFKit.new(html_string, :page_size => 'Letter') 
kit.stylesheets = ActionController::Base.helpers.asset_path("trade_request.css") 

You should be able to access the stylesheet using this method:

ActionController::Base.helpers.asset_path("trade_request.css")

Making your code:

html_string = render_to_string(:template => "/faxes/show.html.erb", :layout => 'trade_request') 
kit = PDFKit.new(html_string, :page_size => 'Letter') 
kit.stylesheets = ActionController::Base.helpers.asset_path("trade_request.css") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文