简体   繁体   English

使用PDFKit的Rails 3.1资产管道

[英]Rails 3.1 asset pipeline with PDFKit

I am using PDFkit with rails 3.1. 我正在使用带有Rails 3.1的PDFkit。 In the past I was able to use the render_to_string function and create a pdf from that string. 过去,我能够使用render_to_string函数并从该字符串创建pdf。 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) (这是我在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? 因此,我的问题是如何通过资产pipline从控制器直接访问CSS文件?

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. 我知道我可以将Rack Middleware与PDFkit一起使用,以将pdf呈现给浏览器,但是在这种情况下,我需要将pdf发送给第三方传真服务。

Thanks for your help. 谢谢你的帮助。

Ryan 瑞安

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. 刚好也遇到了这个问题,我没有使用资产管道就忽略了它,而是像以前在/ 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. 我猜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')

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 )

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? 如何从Rails 3.1的控制器中引用已编译资产?

I was even able to serve .css.erb files through this method. 我什至可以通过这种方法提供.css.erb文件。

In Rails 3.1.1 stylesheets are written to /public/assets with and without the digest fingerprint. 在Rails 3.1.1中,样式表会在带有或不带有摘要指纹的情况下写入/ public / assets。

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: 不过有一个陷阱:如果CSS清单中未引用PDF表,则必须将其添加到预编译配置中:

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. 作为一种更好的选择,请查看asset_path帮助程序是否在您的代码中起作用。 This will reference the correct file in dev and production. 这将在开发和生产中引用正确的文件。

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

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") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM