PDFkit rails3.1 and development env
Asked Answered
I

3

3

My Rails 3.1 app is using PDFkit to render specific pages, and I'm running into (what seems like) a common problem with where trying to generate the pdf is causing the process to hang. I found this solution here on stackoverflow: rails 3 and PDFkit. Where I add a config.threadsafe! entry in my development.rb file and this works BUT it requires that for every change anywhere in the app I have to stop and restart my server to see my changes. NOT acceptable from a workflow - I'm currently setting up the styling for the PDF pages, and it's painfully slow process having to do this.

I also found the same issue reported here: https://github.com/jdpace/PDFKit/issues/110, and the issue points to this workaround: http://jguimont.com/post/2627758108/pdfkit-and-its-middleware-on-heroku.

 ActionController::Base.asset_host = Proc.new { |source, request|
  if request.env["REQUEST_PATH"].include? ".pdf"
    "file://#{Rails.root.join('public')}"
  else
    "#{request.protocol}#{request.host_with_port}"
  end
}

This removes the need to restart the change, BUT now when I load the pdf it's without the styles rendered from the asset pipeline because it's taking the assets from the public directory. I think I could work with this solution if I could know how to create the stylesheets for the pdf templates in the public folder. IS anyone developing with PDFKit and Rails3.1 where this is all working in sync?

Any help would be greatly appreciated! Thanks! Tony

Interjoin answered 11/10, 2011 at 7:28 Comment(1)
BTW, the "else" part is not needed; just return nil (as in my answer)Clemmer
S
2

Here is the setup I am using:

  1. I run a second instance of rails server with rails server -p 3001 -e test which will handle my assets for the PDF. The server will print the assets requests as they come in, so I can check that everything works as expected.

  2. I use the following asset_host in my config/environments/development file:

    config.action_controller.asset_host = ->(source, request = nil){
      "http://localhost:3001" if request && request.env['REQUEST_PATH'].include?(".pdf")
    }
    
Shend answered 22/11, 2011 at 22:31 Comment(0)
C
1

If you are using Pow, you can use multiple workers. Add this to your ~/.powconfig

export POW_WORKERS=3

(taken from Pow manual)

Careworn answered 21/11, 2012 at 10:3 Comment(0)
H
0

There's a problem with pdfkit in Rails 3.1. See my answer to this related question:

pdfkit does not style pdfs

Herra answered 22/2, 2012 at 21:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.