加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – PDFKit给了我一个406,并呈现一个空的pdf

发布时间:2020-12-17 01:59:16 所属栏目:百科 来源:网络整理
导读:Rails 3 PDFKit 0.5.0 安装了wkhtmltopdf的 Windows 7 #/config/initializers/pdfkit.rbrequire 'rbconfig'PDFKit.configure do |config| if !((Config::CONFIG['host_os'] =~ /mswin|mingw/).nil?)#if windows environment this is the path to wkhtmltopdf
> Rails 3
> PDFKit 0.5.0
>安装了wkhtmltopdf的 Windows 7

#/config/initializers/pdfkit.rb
require 'rbconfig'
PDFKit.configure do |config|
  if !((Config::CONFIG['host_os'] =~ /mswin|mingw/).nil?)#if windows environment this is the path to wkhtmltopdf otherwise use gem binaries
    config.wkhtmltopdf = "C:/Program Files (x86)/wkhtmltopdf/wkhtmltopdf.exe"
  end
end

#show action I am working with
def show
  @work_order = WorkOrder.find_by_id(params[:id])
  respond_to do |format|
    format.pdf #pdfkit handles this
    format.html { render :partial => "show" } if request.xhr? 
  end
end

#config/initializers/mime_types.rb
Mime::Type.register "application/pdf",:pdf

有了这一切,我得到一个空白的Pdf发送到浏览器,日志输出说明了这一点

Started GET "/work_orders/6.pdf" for 127.0.0.1 at 2011-05-17 15:51:31 -0400
Processing by WorkOrdersController#show as HTML
Parameters: {"id"=>"6"}
User Load (3.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
SQL (6.0ms)  describe `roles_users`
Role Load (3.0ms)  SELECT `roles`.* FROM `roles` WHERE (`roles`.`id` = 980190962) LIMIT 1
WorkOrder Load (4.0ms)  SELECT `work_orders`.* FROM `work_orders` WHERE (`work_orders`.`id` = 6) LIMIT 1
Completed 406 Not Acceptable in 265ms

我希望你能帮帮我

编辑:我删除了从show动作到just的所有内容

def show
  @work_order = WorkOrder.find_by_id(params[:id])
end

现在我得到200,但页面仍然呈现空白

Started GET "/work_orders/6.pdf" for 127.0.0.1 at 2011-05-17 17:15:26 -0400
Processing by WorkOrdersController#show as HTML
Parameters: {"id"=>"6"}
User Load (19.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
SQL (34.0ms)  describe `roles_users`
Role Load (17.0ms)  SELECT `roles`.* FROM `roles` WHERE (`roles`.`id` = 980190962) LIMIT 1
WorkOrder Load (9.0ms)  SELECT `work_orders`.* FROM `work_orders` WHERE (`work_orders`.`id` = 6) LIMIT 1
Rendered work_orders/show.html.erb within layouts/application (10.0ms)
Completed 200 OK in 741ms (Views: 58.0ms | ActiveRecord: 79.0ms)

EDIT2:
我现在没有布局渲染,页面不再是空白,但字符都是搞乱的

解决方法

如果您希望PDFKit在url以.pdf结尾时自动处理所有内容,您还需要配置PDFKit中间件.

资料来源:https://github.com/jdpace/PDFKit

中间件

PDFKit附带一个中间件,允许用户通过将.pdf附加到URL来获取您网站上任何页面的PDF视图.
中间件安装程序

Rails应用程序

# in application.rb(Rails3) or environment.rb(Rails2)
require 'pdfkit'
config.middleware.use PDFKit::Middleware

使用PDFKit选项

# options will be passed to PDFKit.new
config.middleware.use PDFKit::Middleware,:print_media_type => true

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读