PDFkit rails3.1 and development env

徘徊边缘 提交于 2019-12-12 15:02:31

问题


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


回答1:


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")
    }
    



回答2:


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

export POW_WORKERS=3

(taken from Pow manual)




回答3:


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

pdfkit does not style pdfs



来源:https://stackoverflow.com/questions/7722681/pdfkit-rails3-1-and-development-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!