views:

39

answers:

3

Hi Everyone,

I am using Prawn PDF to create PDF files on the fly in my Rails application.

I recently came across the rails cell.blank? function, which has proved to be really handy, I can hide any <li> rows I want if there is nothing to display - something I have been wondering about for a while!

Is it possible to do the same in Prawn? Hide a line if it's empty?

At the moment if I have the following:

Contact Name
Address Line 1
Address Line 2
Address Line 3
Postcode
Phone Number
Fax Number

If, for example, the user only entered the contact name and postcode there would be a significant gap where the Address Line's 1, 2 and 3 should be.

If I could ignore those rows because they are empty and display it as:

Contact Name
Postcode

it would tidy up my PDF and save the user having to use the incorrect field to ensure the address looks right in the PDF.

This is what my Prawn code looks like:

pdf.text_box "Client", :size => 9, :at => [50,673]
    pdf.text_box "#{@kase.company.companyname}", :size => 9, :at => [100,673]
    pdf.text_box "#{@kase.company.companyaddressline1}", :size => 9, :at => [100,665]
    pdf.text_box "#{@kase.company.companyaddressline2}", :size => 9, :at => [100,655]
    pdf.text_box "#{@kase.company.companyaddressline3}", :size => 9, :at => [100,645]
    pdf.text_box "#{@kase.company.companyaddresscity}", :size => 9, :at => [100,635]
    pdf.text_box "#{@kase.company.companyaddresspostcode}", :size => 9, :at => [100,625]

I have searched the Prawn google user group site but can't seem to find what I need.

Thanks in advance!

Danny

A: 

use a counter for the y coordinate of the current address line. If you add a line decrease it by 10. if your line is empty don't add it and don't increase the y coordinate.

y = 673
if @line1 then 
  pdf.text_box "#{@line1}", :size => 9, :at => [100,y]
  y+= 10
end 
if @line2 then 
  pdf.text_box "#{@line2}", :size => 9, :at => [100,y]
  y+= 10
end 

(hint: if it works the way you want - refactor it into a function that adds the line and increases your counter)

Nikolaus Gradwohl
Hi, would that go straight into the prawn.pdf file, where I have the "pdf.text_box"? Thanks, Danny
dannymcc
A: 

You should be able to use Ruby's 'empty?' method, which is available for most data structures and strings to determine if your string or data structure has any content. Something like (I don't know prawn that well, but this company name looks like it would be a string so I dont think you'd need to cast it):

pdf.text_box @kase.company.companyname, :size => 9, :at => [100,673] unless @kase.company.companyname.empty?

of course, you could make that into a helper method

def my_text_box (var, x, y)
  pdf.text_box var, :size => 9, :at => [x,y] if unless var.empty?
end

and call in your prawn document

my_text_box @kase.company.companyname, 100, 673

oops, forgot to add the ref to empty? http://apidock.com/ruby/String/empty%3F

Jed Schneider
i guess i didn't really address how to avoid blank lines. You might look at prawns pdf_text_box options, which i think you can provide a default height to, then you wouln't have to specify the y location of each box prawn would just add it in.http://prawn.majesticseacreature.com/docs/0.8.4/prawn-core/Prawn/Text/Box.html
Jed Schneider
Hi as you say this doesnt remove the blank lines - it just doesn't display the content if there isn't any. Which happens anyway. I have looked at the pdf_text_box documentation but it doesn't seem to mention hiding lines.
dannymcc
you're right, i was thinking of the bounding_box, not the text_box. if you dont give the bounding box a specific height it will expand to fit, http://prawn.majesticseacreature.com/docs/0.8.4/prawn-core/Prawn/Document.html#method-i-bounding_box(edited and added as answer below -- no syntax highlighting in the comment)
Jed Schneider
+1  A: 

pdf bounding box (shamelessly yanked from the prawn documentation).

   pdf.bounding_box([100,400], :width => 400) do
     pdf.text("The height of this box is #{pdf.bounds.height}")
     pdf.text('this is some text')
     pdf.text('this is some more text')
     pdf.text('and finally a bit more')
     pdf.text("Now the height of this box is #{pdf.bounds.height}")
   end

can be easily modified to print if the attribute is available.

Jed Schneider