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:

[code]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[/code]

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.

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:

[code]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'],

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?

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:

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.

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

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...

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:in
`call'
/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:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3</code></pre>
    </div>

    <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:in
`redirect_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:in
`send'
/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:in
`call_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:in
`perform_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:in `realtime'
/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:in
`perform_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:in
`perform_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:in
`process_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:in
`process'
/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:in
`call'
/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:in
`call'
/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:in
`cache'
/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:in
`call'
/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:in
`call'
/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:in
`call'
/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:in
`call'
/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:in `call'
/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:in `call'
/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:in
`run'
/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:in `call'
/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:in `each'
/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:in
`call'
/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:in
`service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/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:in
`gem_original_require'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3</code></pre>
    </div>

</div>

<h2 style="margin-top: 30px">Request</h2>
<p><b>Parameters</b>:
<pre>{&quot;id&quot;=&gt;&quot;projeto_id&quot;}</pre></p>

<p><a href="#"
onclick="document.getElementById('session_dump').style.display='block';
return false;">Show session dump</a></p>
<div id="session_dump" style="display:none"><pre class='debug_dump'>---
</pre></div>

<h2 style="margin-top: 30px">Response</h2>
<p><b>Headers</b>:
<pre>{&quot;Content-Transfer-Encoding&quot;=&gt;&quot;binary&quot;,
&quot;Content-Type&quot;=&gt;&quot;application/octet-stream&quot;,
&quot;Content-Disposition&quot;=&gt;&quot;attachment&quot;,
&quot;Content-Length&quot;=&gt;&quot;26&quot;,
&quot;Cache-Control&quot;=&gt;&quot;private&quot;}</pre></p>

</body>
</html>

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

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.