Prawn - Adding an image along with text in a table cell
Asked Answered
C

1

6

Is it possible to embed an image along with a text in a table cell using gem prawn?

I'm using gem "prawn", "~> 0.13.2"

I could embed either an image or a text only. But, not both. please help.

Rails version: 3.2.13 and Ruby 2.0.0

Cung answered 14/1, 2014 at 11:8 Comment(1)
It doesn't look like Prawn supports this out-of-the-box.Emplace
D
8

Here is code from the manual on how to insert an image, and a text into a PDF-document:

text "Scale by setting only the width"
image "#{Prawn::DATADIR}/images/pigs.jpg", :width => 150
move_down 20

text "Scale by setting only the height"
image "#{Prawn::DATADIR}/images/pigs.jpg", :height => 100
move_down 20

text "Stretch to fit the width and height provided"
image "#{Prawn::DATADIR}/images/pigs.jpg", :width => 500, :height => 100

Here is code from the manual on how to insert an image, and a text into a table cell of a PDF-document:

cell_1 = make_cell(:content => "this row content comes directly ")
cell_2 = make_cell(:content => "from cell objects")
two_dimensional_array = [ ["..."], ["subtable from an array"], ["..."] ]
my_table = make_table([ ["..."], ["subtable from another table"], ["..."] ])
image_path = "#{Prawn::DATADIR}/images/stef.jpg"

table([ ["just a regular row", "", "", "blah blah blah"],
    [cell_1, cell_2, "", ""],
    ["", "", two_dimensional_array, ""],
    ["just another regular row", "", "", ""],
    [{:image => image_path}, "", my_table, ""]])

For more information on customizing a table cell image: Check out the Prawn documentation

Dejected answered 14/1, 2014 at 19:23 Comment(7)
I'm asking about how to insert an image and a text to a single table cell. Is that what you explain above?Cung
@RajeshCO isn't the both second, and the first parts of code solution for it?Desdamonna
You can use a two row, one column subtable with no borders inside a single cell to get what you're looking for.Beeline
How can we do something like table([["`string1`"], [["`string2`", "`string2.5`"]], ["`string3`]]]) ?Humectant
@VishwasNahar what do ypu mean for `string1` ??Desdamonna
Like string >> "string"Humectant
@VishwasNahar I dont know weither the prawn supports joining the table cellsDesdamonna

© 2022 - 2024 — McMap. All rights reserved.