2017-10-30 15 views
0

私のアプリケーションの構築にはrails 5grape gemを使用しています。今私はPDFを生成し、grapeエンドポイントを使用して応答に返信する必要があります。私はwicked_pdf宝石とwkhtmltopdf-binaryを使用してPDFを生成しています。しかし、私はundefined method 'render_to_string'の問題に直面しています。 私はそれを解決するために様々な方法を試みました。解決策が見つかりませんでした。以下はRails 5 Grape APIエンドポイントでPDFを生成する方法と応答で送信しますか?

私のコードスニペットは、

APIエンドポイントである:

module Endpoints 
    class GeneratePdf < Grape::API 
    get do 
     users = User.all # I want this users list in my PDF 
     pdf = WickedPdf.new.pdf_from_string(render_to_string('users/index.html.erb')) # placed in app/views/users/pdf.html.erb 
     # some code here for send pdf back to response 
    end 
    end 
end 

Gemfile:

gem 'wicked_pdf' 
gem 'wkhtmltopdf-binary' 

コンフィグ>初期化子> mime_types.rb

Mime::Type.register "application/pdf", :pdf 

答えて

0

Grape::APIには、一般的なRailsコントローラとは異なり、呼び出すことができるrender_to_stringメソッドがありません。あなたはこれに似た何かにそれを置き換えることができるはずです:

require 'erb' 
binding_copy = binding 
binding_copy.local_variable_set(users: User.all) 
template = File.open(Rails.root.join('app/views/users/index.html.erb)) 
string = ERB.new(template).result(binding_copy) 
pdf = WickedPdf.new.pdf_from_string(string) 
関連する問題