1

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

4

3 回答 3

1

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.

于 2010-08-04T01:35:55.623 回答
0

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)

于 2010-08-03T11:06:35.903 回答
0

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

于 2010-08-03T12:34:12.080 回答