4
  • レール3
  • PDFキット 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)

EDIT2:レイアウトなしでレンダリングしたところ、ページは空白ではなくなりましたが、文字はすべてねじれています

4

1 に答える 1

1

URL が で終わるたびに PDFKit にすべてを自動的に処理させたい場合は.pdfPDFKit middleware.

ソース: https://github.com/jdpace/PDFKit

ミドルウェア

PDFKit にはミドルウェアが付属しており、ユーザーは URL に .pdf を追加することで、サイト上の任意のページの 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
于 2011-11-08T21:30:39.233 に答える