Prawn question

First off all, it’s important to say that I’m just using PRAWN gem, i’m
NOT using prawnto plugin.

So… It creates a PDF file, but the question is:

How can I use a list (for example, if I wanna use the name and the mail
adress of “@users” from “@users = User.find(:all)”) and put that list on
the PDF file.

If helps, the syntax i’m using is the following:

def pdf
Prawn::Document.generate(“name_of_file.pdf”) do

it’s here that the code should give me the list

end
end

Thanks for any help.

Problem solved.

But, now, i have a new problem.

It print the list, but also print, for every line of the list, the
headers.

The code is the following:

def pdf Prawn::Document.generate 'testa.pdf' do @permissao = Permissao.find(:all) for perm in @permissao @data = [["#{perm.perfil}", "#{perm.usuario_id}"]] table @data, :position => :center, :headers => ['Perfil', 'Usuário'], :row_colors => ['ffffff', '999999'], :vertical_padding => 2, :horizontal_padding => 30 end end redirect_to :action => "index" flash[:msg] = "Arquivo em PDF gerado com sucesso." end

And if someone think "is just put the “table @data” before the block,
well… If I do that, what I did, it will list just one of the
@permissao, not all.

Thanks any help.

On 9 July 2010 13:54, Bla … [email protected] wrote:

Prawn::Document.generate ‘testa.pdf’ do
@permissao = Permissao.find(:all)
for perm in @permissao
@data = [[“#{perm.perfil}”, “#{perm.usuario_id}”]]
table @data, :position => :center, :headers => [‘Perfil’, ‘Usuário’],

As you have this inside the loop it is outputting a table for each
perm item, so each one will have the headers. You need to build up
your @data array with data from all the rows from @permissao then (not
inside a loop) call table with the complete array.

Colin

Sorry, but how do I can insert all the @data values in other array?

On 9 July 2010 14:12, Bla … [email protected] wrote:

Sorry, but how do I can insert all the @data values in other array?

If you had just waited a few minutes before sending this then someone
called Bla would have answered the question for you.

Colin

I have a new question.

How do I make to save de PDF file in my desktop, for example?

Thanks again.

It’s just put the hole directory in the generate arguments…

But how i can verify if the person is using linux ou windows?

Or if i could make the user choose where to save it… Would be great.

Problem solved.

My controller ends like this:

def pdf
Prawn::Document.generate ‘testa.pdf’ do
@permissao = Permissao.find(:all)
@data = @permissao.collect { |perm| [perm.perfil, perm.usuario_id] }
table @data, :position => :center, :headers => [‘Perfil’, ‘Usuário’],
:row_colors => [‘ffffff’, ‘C6D8E2’],
:vertical_padding => 2,
:horizontal_padding => 30
end
redirect_to :action => “index”
flash[:msg] = “Arquivo em PDF gerado com sucesso.”
end

Thanks. :slight_smile:

Bla … wrote:

Or if i could make the user choose where to save it… Would be great.

I use something like:

def pdf
@project = Project.find(params[:id])
filename = @project.name+"-"+Time.now.strftime("%Y-%m-%d-%H%M")+".pdf"
filename = filename.gsub(" “,”_")
send_data @project.as_pdf(params[:type]), :filename => filename, :type
=> “application/pdf”
end

Let the user decide whether they will save or just open it in the
browser…

On 13 July 2010 12:48, Bla … [email protected] wrote:

Or if i could make the user choose where to save it… Would be great.

I suspect some of us at least have lost track of what you are asking.
Are you asking how to make the user choose where to save a pdf file
when you send it to him? If so you need do nothing. When you send a
pdf the browser will ask the user whether to open or save it.

Colin

I’ve just tried to do exactly what you said, but i got the same error

To be honest, i don’t get what you said quite well…
So i try adding the line “send_data(‘Projetos_Nao_Iniciados’)” at the
end of action “pdf”…
And do not asks if the user want to download it, but shows that some pdf
file was downloaded… But, when i open the file, it shows something
like:

r/dispatcher.rb:87:in dispatch' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:121:in_call’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:130:in
build_middleware_stack' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:incall’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:in
call' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/lib/ruby/1.8/webrick/server.rb:162:in start' /usr/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/lib/ruby/1.8/webrick/server.rb:92:ineach’
/usr/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/lib/ruby/1.8/webrick/server.rb:23:instart’
/usr/lib/ruby/1.8/webrick/server.rb:82:in start' /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:ingem_original_require’
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require’
script/server:3

<div id="Full-Trace" style="display: none;">
  <pre><code>/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1136:in 

redirect_to_full_url' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1131:inredirect_to’
/home/bianca/Projetos
SVN/CAOS/caos/dev/trunk/Project/Caos/Caos/app/controllers/projetos_controller.rb:408:in
pdf_projetos_naoiniciados' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:insend’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in
perform_action_without_filters' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:617:incall_filters’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:610:in
perform_action_without_benchmark' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
/var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in
ms' /usr/lib/ruby/1.8/benchmark.rb:308:inrealtime’
/var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in
ms' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:160:in
perform_action_without_flash' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/flash.rb:146:inperform_action’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in
send' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:inprocess_without_filters’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:606:in
process' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:391:inprocess’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:386:in
call' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:437:incall’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:87:in
dispatch' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:121:in_call’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:130:in
build_middleware_stack' /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:incall’
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in
call' /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:34:incache’
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:in
cache' /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:28:incall’
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in
call' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/string_coercion.rb:25:incall’
/var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:in call' /var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:incall’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:15:in
call' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:incall’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:in
call' /var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:incall’
/var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in synchronize' /var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:incall’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:in
call' /var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:inrun’
/var/lib/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:in
call' /var/lib/gems/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:incall’
/var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:in call' /var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:ineach’
/var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in call' /var/lib/gems/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:17:incall’
/var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:in
call' /var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:50:inservice’
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in service' /usr/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
/usr/lib/ruby/1.8/webrick/server.rb:173:in start_thread' /usr/lib/ruby/1.8/webrick/server.rb:162:instart’
/usr/lib/ruby/1.8/webrick/server.rb:162:in start_thread' /usr/lib/ruby/1.8/webrick/server.rb:95:instart’
/usr/lib/ruby/1.8/webrick/server.rb:92:in each' /usr/lib/ruby/1.8/webrick/server.rb:92:instart’
/usr/lib/ruby/1.8/webrick/server.rb:23:in start' /usr/lib/ruby/1.8/webrick/server.rb:82:instart’
/var/lib/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:in
run' /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111 /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:ingem_original_require’
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require’
script/server:3

Request

Parameters:

{"id"=>"projeto_id"}

Show session dump

---

Response

Headers:

{"Content-Transfer-Encoding"=>"binary",
 "Content-Type"=>"application/octet-stream",
 "Content-Disposition"=>"attachment",
 "Content-Length"=>"26",
 "Cache-Control"=>"private"}

Bla … wrote:

I’ve just tried to do exactly what you said, but i got the same error

Let’s see… what you’re sending isn’t a pdf.

def pdf
@project = Project.find(params[:id])
filename = @project.name+"-"+Time.now.strftime("%Y-%m-%d-%H%M")+".pdf"
filename = filename.gsub(" “,”_")
send_data @project.as_pdf(params[:type]), :filename => filename, :type
=> “application/pdf”
end

project class implements an as_pdf method, which encapsulates the
generation of the actual pdf (I used PDF::Writer, and have yet to update
to a newer method. If it ain’t broke, don’t fix it). The final
statement in that method is pdf.render (pdf is the local var, and render
causes the return to be the actual pdf file stream for lack of a better
name).

send_data sends the return from that method to the client, with a
filename, and the type as “application/pdf”, which should get the
browser to either ask the user what they want to do with it, or open it
in the browser.

I haven’t used Prawn, but given its genesis, I hope it isn’t too
different.