将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

Using Prawn with Sinatra
使用 Sinatra 与

How can i use Prawn to generate PDF of form data in Sinatra?

My form is at the index action of the site but Prawn doesn't recognize the variables passed in.i've found this code in the documentation at http://prawnpdf.org/docs/0.11.1/Prawn/Document.html.


get '/download' do 

 content ="Hello World"

 Prawn::Document.generate"example.pdf" do |pdf|
 pdf.font"Times-Roman"
 pdf.draw_text content, :at => [200,720], :size => 32
 end
end 

Now this is great, but when i try to pass in my variables from the parameters like this :


post '/resume' do 
 @name = params.fetch 'name' 
. . .
end 

And then if i add that into the/download route like this :


get '/download' do 

 Prawn::Document.generate"example.pdf" do |pdf|
 pdf.font"Times-Roman"
 pdf.draw_text @name, :at => [200,720], :size => 32
 end
end 

It it will draw nothing.Also, weirdly, it generates a pdf file but doesn't redirect to it and i have to manually load it.

Really stuck on this one - advice?

时间: 作者:

every request creates a new instance of your Sinatra app to serve it.Therefore, instance variables won't be the same across 2 different requests.you either need to persist the data using one of the many ways to do this (cookies, local storage, database, cache etc) or you need to put the Prawn logic in the POST route.

原作者:
...