简体   繁体   English

使用PDFkit gem生成pdf在rails 4上挂起

[英]generating pdf hangs on rails 4 using PDFkit gem

I'm able to download pdf file with: 我可以下载pdf文件:

curl google.com | wkhtmltopdf - test.pdf

so it means, wkhtmlpdf installation was successful. 所以这意味着,wkhtmlpdf安装成功了。

But, when I try to generate pdf file by accessing http://localhost:3000/contacts/1.pdf it hangs. 但是,当我尝试通过访问http://localhost:3000/contacts/1.pdf生成pdf文件时,它会挂起。 In the status bar it shows: Waiting for localhost... 在状态栏中显示: Waiting for localhost...

Rails server output: Rails服务器输出:

Started GET "/contacts/1.pdf" for 127.0.0.1 at 2013-07-28 21:45:06 +0900
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ContactsController#show as HTML
  Parameters: {"id"=>"1"}
  Contact Load (0.3ms)  SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? LIMIT 1  [["id", "1"]]
  Rendered contacts/show.html.erb within layouts/application (1.4ms)
Completed 200 OK in 99ms (Views: 57.0ms | ActiveRecord: 0.7ms)

Gemfile: 的Gemfile:

gem 'pdfkit'

application.rb: application.rb中:

config.middleware.use "PDFKit::Middleware"

According to the PDFKit railscast this should be enough for generating pdf files just by adding .pdf ... 根据PDFKit railscast,这应该足以生成pdf文件,只需添加.pdf ...


UPDATE: 更新:

show.html.erb: show.html.erb:

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @contact.name %>
</p>

<p>
  <strong>Age:</strong>
  <%= @contact.age %>
</p>

<%= link_to 'Edit', edit_contact_path(@contact) %> |
<%= link_to 'Back', contacts_path %>

layouts/application.html.erb: 布局/ application.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title>Pdftest</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

UPDATE 2: 更新2:

Thanks to @Arman H for helping me to figure out that I have to specify absolute path for assets instead of a relative ones. 感谢@Arman H帮助我弄清楚我必须指定资产的绝对路径而不是相对路径。 When I removed the following lines I was able to generate PDF file: 当我删除以下行时,我能够生成PDF文件:

<%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

Now, I can't get how to substitute this with an absolute paths . 现在,我无法得到如何用绝对路径替换它。 It seems this post is what I need, but I still can't figure out how this would look like for my case. 看来这篇文章就是我所需要的,但我仍然无法弄清楚我的情况会是怎样的。

The issue was due to stylesheet_link_tag and javascript_include_tag using relative URLs, which often causes wkhtmltopdf to hang when loading assets from the same server that wkhtmltopdf is running on. 问题是由stylesheet_link_tagjavascript_include_tag使用相对URL,这通常会导致wkhtmltopdf在运行wkhtmltopdf的同一服务器上加载资源时挂起。

Using absolute URLs for assets solved the problem. 使用资产的绝对URL解决了问题。

Set asset_host in Rails' config, which also affects stylesheet_link_tag and javascript_include_tag : 在Rails的配置中设置asset_host ,这也会影响stylesheet_link_tagjavascript_include_tag

# Modify asset host config setting in `config/application.rb`
# Or create a new initializer: `config/initializers/wkhtmltopdf.rb`
config.action_controller.asset_host = "http://mysite.com"

# Or you can have different hosts for development (local) and production (CDN):
# In `config/environments/development.rb`
config.action_controller.asset_host = "http://localhost"
# In `config/environments/production.rb`
config.action_controller.asset_host = "http://d111111abcdef8.cloudfront.net"

Setting config.action_controller.asset_host = "http://localhost" in development.rb actually didn't work for me. 在development.rb中设置config.action_controller.asset_host = "http://localhost"实际上对我不起作用。 That is, the PDF generation would work, but then assets wouldn't come through when rendering HTML. 也就是说,PDF生成可以工作,但在呈现HTML时资产不会通过。

I followed the method here: http://jguimont.com/post/2627758108/pdfkit-and-its-middleware-on-heroku 我按照这里的方法: http//jguimont.com/post/2627758108/pdfkit-and-its-middleware-on-heroku

and it worked like a charm for me. 它对我来说就像一个魅力。 Hope this helps someone. 希望这有助于某人。 Just throw assets.rb in config/intializers and you're good to go. 只需在config / intializers中抛出assets.rb就可以了。

I had the same issue in which my log showed the page had rendered however no pdf was generated and the browser would hang. 我有同样的问题,我的日志显示页面已呈现但是没有生成pdf并且浏览器将挂起。 It ended up having nothing to do with OS compatability, missing librariers, gems nor dependencies but instead I needed to raise the max allowable thread count for my Puma server (which had been set to 1) upto 2 or more. 它最终与操作系统兼容性,缺少图书馆员,宝石和依赖关系无关,而是我需要将我的Puma服务器(已设置为1)的最大允许线程数提高到2或更多。 This then generated pdf's as normal. 然后生成pdf正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM