PDFKit 给我一个 406,并渲染一个空的 pdf

发布于 2024-11-08 16:38:33 字数 2053 浏览 0 评论 0原文

  • Rails 3
  • PDFKit 0.5.0
  • 安装了 wkhtmltopdf 的 Windows 7

    #/config/initializers/pdfkit.rb
    需要“rbconfig”
    PDFKit.configure 做 |config|
      if !((Config::CONFIG['host_os'] =~ /mswin|mingw/).nil?)#if windows 环境这是 wkhtmltopdf 的路径,否则使用 gem 二进制文件
        config.wkhtmltopdf = "C:/Program\ Files\ (x86)/wkhtmltopdf/wkhtmltopdf.exe"
      结尾
    结尾
    
    #show 我正在处理的动作
    定义显示
      @work_order = WorkOrder.find_by_id(params[:id])
      respond_to do |格式|
        format.pdf #pdfkit 处理这个
        format.html { 渲染:部分 => “显示”}如果request.xhr? 
      结尾
    结尾
    
    #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)

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

  • Rails 3
  • PDFKit 0.5.0
  • windows 7 with wkhtmltopdf installed

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

And with all that I get a blank Pdf sent to the browser and the log output says this

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

I hope you can help me

EDIT: I removed everything from the show action to just

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

and now I get a 200 but the Page still renders blank

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:
I rendered without the layout now as well the page is no longer blank but the characters are all screwy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2024-11-15 16:38:33

如果您希望 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

If you want PDFKit to handle everything automatically whenever the url ends in .pdf, you will need to also configure the PDFKit middleware.

Source: https://github.com/jdpace/PDFKit

Middleware

PDFKit comes with a middleware that allows users to get a PDF view of any page on your site by appending .pdf to the URL.
Middleware Setup

Rails apps

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

With PDFKit options

# options will be passed to PDFKit.new
config.middleware.use PDFKit::Middleware, :print_media_type => true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文