【问题标题】:PDFKit is giving me a 406, and rendering an empty pdfPDFKit 给了我一个 406,并呈现一个空的 pdf
【发布时间】:2011-08-27 13:08:01
【问题描述】:
  • 导轨 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

希望你能帮到我

编辑:我删除了一切从表演动作到只是

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)

编辑2: 我现在也没有布局渲染,页面不再是空白,但字符都是扭曲的

【问题讨论】:

  • 显示操作生成的 html 是否有效 xhtml 我没有使用 pdfkit 但我知道我使用的一些 html->pdf gem 要求 html 是 xhtml-strict。尝试(在命令行)xmllint http://localhost:3000/work_orders/6,或使用其他一些 xhtml 验证器

标签: ruby-on-rails ruby-on-rails-3 wkhtmltopdf pdfkit


【解决方案1】:

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

来源: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

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 2021-10-08
    • 2016-08-07
    • 2016-02-03
    • 1970-01-01
    • 2012-06-08
    相关资源
    最近更新 更多