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
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')
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With