-
-
Save jfeust/973267 to your computer and use it in GitHub Desktop.
# NOTE: Prawn 1.0 now supports images in tables, and it works much better than my mock table below! | |
# As you may have noticed Prawn does not currently support images in table cells. | |
# There is an unofficial fork with initial table image support but it appears that | |
# it will be some time before this gets merged into the official branch. | |
# | |
# Instead of using an unofficial branch, I found a nice way to do a mock table with | |
# image support. You just need to keep track of your page breaks. | |
# size values | |
cell_width = 90 | |
row_height = 80 | |
# Build an array with your table data | |
pieces = @pieces_to_report.map do |piece| | |
[ | |
piece.image.url(:thumb), | |
piece.name, | |
piece.inventory_number, | |
piece.medium, | |
piece.size_str, | |
number_to_currency(piece.price, :unit => "$") | |
] | |
end | |
# Column Header Values | |
header = ['', 'Name', 'Inv #', 'Medium','Size', 'Price'] | |
pdf.bounding_box [0, pdf.cursor], :width => 500, :height => 20 do | |
header.each_with_index do |head,i| | |
pdf.bounding_box [cell_width*i,0], :width => cell_width do | |
pdf.text head, :size => 10 | |
end | |
end | |
end | |
pdf.move_down 15 | |
# Build the Mock Table | |
pieces.each_with_index do |p,i| | |
pdf.bounding_box [0, pdf.cursor], :height => row_height, :width => 500 do | |
pdf.stroke do | |
pdf.line pdf.bounds.top_left, pdf.bounds.top_right | |
pdf.line pdf.bounds.bottom_left, pdf.bounds.bottom_right | |
end | |
pdf.move_down 5 # a bit of padding | |
cursor = pdf.cursor # keep current cursor value for all cells in this row | |
p.each_with_index do |v, j| | |
pdf.bounding_box [cell_width*j, cursor], :height => row_height, :width => cell_width do | |
if j == 0 # handle image column | |
# might want logic here to handle empty/nil images | |
pdf.image open(v), :scale => 0.6 | |
else | |
pdf.text v, :size => 10 unless v.blank? | |
end | |
end | |
end | |
end | |
# create a new page every 5 rows | |
pdf.start_new_page if i > 0 && i % 5 = 0 | |
end |
ends_with? is a rails method that gets added to the String class. In fact that whole line there is just to handle empty images using the paperclip attachment gem. I removed that from the gist as it's not needed in a generic context.
I tried to run it and I'm getting error saying 'Undefined method ends_with?'. Weird.
You must not be running it from a Rails environment. See the updated gist.
Okay now.
Since it's a PhotoUploader class I'd to convert it before opening.
img_file = v.to_s
pdf.image open(img_file), :scale => 0.6
But now it's says the file was not found: (No such file or directory - /uploads/item/photo/2/tirupati1.jpg) even the file is present. Maybe removing the first slash.
Anyway, thank you.
Read up on the Prawn documentation. open(img) is only used for remote (web based) images.
You would want to just do :
pdf.image img_file
You right! just pdf.image v worked fine! Thank you again!
cursor isn't recognized :(
Hi, I would like to use your mock table but seems you are using a method that you just forgot to put it there.
Could you please share your "ends_with?" method too?
Thanks.