使用PDFkit gem生成pdf挂在rails 4上 [英] generating pdf hangs on rails 4 using PDFkit gem

查看:95
本文介绍了使用PDFkit gem生成pdf挂在rails 4上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过以下方式下载pdf文件:

I'm able to download pdf file with:

curl google.com | wkhtmltopdf - test.pdf

这意味着wkhtmlpdf安装成功.

so it means, wkhtmlpdf installation was successful.

但是,当我尝试通过访问http://localhost:3000/contacts/1.pdf生成pdf文件时,它挂起了.在状态栏中,它显示:Waiting for localhost...

But, when I try to generate pdf file by accessing http://localhost:3000/contacts/1.pdf it hangs. In the status bar it shows: Waiting for localhost...

启用服务器输出:

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)

宝石文件:

gem 'pdfkit'

application.rb:

application.rb:

config.middleware.use "PDFKit::Middleware"

根据 PDFKit railscast ,只需添加.pdf ...

According to the PDFKit railscast this should be enough for generating pdf files just by adding .pdf ...

更新:

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:

layouts/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>


更新2:

感谢@Arman H帮助我弄清楚我必须为资产指定绝对路径,而不是相对路径.当我删除以下几行后,我便能够生成PDF文件:

Thanks to @Arman H for helping me to figure out that I have to specify absolute path for assets instead of a relative ones. When I removed the following lines I was able to generate PDF file:

<%= 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.

推荐答案

此问题是由于stylesheet_link_tagjavascript_include_tag使用相对URL引起的,当从与wkhtmltopdf正在运行.

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.

使用绝对URL 来解决资产问题.

在Rails的配置中设置asset_host,这也会影响stylesheet_link_tagjavascript_include_tag:

Set asset_host in Rails' config, which also affects stylesheet_link_tag and javascript_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"

这篇关于使用PDFkit gem生成pdf挂在rails 4上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆