Rails 3.1 conveyor with PDFKit

I am using PDFkit with 3.1 rails. In the past, I was able to use the render_to_string function and create a pdf file from this line. Then I add style sheets as follows. My problem is that I have no idea how to access them from the asset pipeline. (So ​​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 is how to get direct access from my controller to my css file through the asset pipeline?

I know that I can use Rack middleware with PDFkit to render a pdf file in a browser, but in this case I need to send the PDF file to a third-party fax service.

Thank you for your help.

Ryan

+8
ruby-on-rails pdfkit asset-pipeline
source share
6 answers

I just ran into this problem, and I walked past it, not using the asset pipeline, but gaining access to the file directly, as I used to be / public. I don’t know what are the possible disadvantages of using this approach.

I think that the LESS and SCSS files will not be processed as if they were accessible 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') 
+3
source share

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

I would 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 ) 
+1
source share

In Rails 3.1.1, stylesheets are written to / public / assets with and without a digest.

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

However, if: if the PDF does not specify in the CSS manifest, you will have to add it to the precompile configuration:

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

This tells the asterisks to compile this file on their own.

As an alternative (better), see if the resource_path helper works in your code. This will be the link to the correct file in dev and production.

0
source share

I have finished copying the css file to my shared directory and refer to it the same way as before, with rails 3. For more information, check out this question: Access stylesheet_link_tag from the controller

0
source share

I landed here trying to solve this problem, and none of the answers seemed to resolve this problem for me. I found that the accepted answer of this worked for me:

How to bind compiled assets from a controller in Rails 3.1?

I could even serve .css.erb files with this method.

0
source share

You must have access to the stylesheet using this method:

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

Code Generation:

 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") 
-one
source share

All Articles