Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 asset pipeline with PDFKit

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

like image 226
lundie Avatar asked Nov 18 '11 18:11

lundie


2 Answers

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')
like image 178
Constant Meiring Avatar answered Oct 14 '22 09:10

Constant Meiring


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 )
like image 30
zenw0lf Avatar answered Oct 14 '22 09:10

zenw0lf