8

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

Richard Hulse
  • 10,383
  • 2
  • 33
  • 37
lundie
  • 337
  • 4
  • 24
  • Possible duplicate of [How does one reference compiled assets from the controller in Rails 3.1?](http://stackoverflow.com/questions/7409948/how-does-one-reference-compiled-assets-from-the-controller-in-rails-3-1) – Nathan Hughes Aug 09 '16 at 16:38

7 Answers7

3

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')
Constant Meiring
  • 3,285
  • 3
  • 40
  • 52
1

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 )
zenw0lf
  • 1,232
  • 1
  • 13
  • 22
0

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.

Community
  • 1
  • 1
Barry
  • 237
  • 1
  • 2
  • 11
0

Try

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

  • 4
    Can you add more details please ? Also, as it update an old post, it would be great to explain what new solution in comparison to previous answers. – amanin Aug 06 '21 at 17:39
0

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.

Richard Hulse
  • 10,383
  • 2
  • 33
  • 37
  • Thanks for the response. I am just using the application.css file so I don't believe I have to worry about the precompiling. I believe pdfkit runs a separate process, and therefor when I reference the path without the hash at the end of the file it cannot find it. IE this works: kit.stylesheets << "#{Rails.root.to_s}/public/assets/application-5c342d3a7328fd347c8ca581f6bdff60.css" this does not: kit.stylesheets << "#{Rails.root.to_s}/public/assets/application.css" Is there any way to get the correct file path? Thanks! – lundie Nov 21 '11 at 16:20
0

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

Community
  • 1
  • 1
lundie
  • 337
  • 4
  • 24
-1

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") 
Paul Simpson
  • 2,504
  • 16
  • 28