Rails Gem Prawn, Image and Anchor

Prawn: anchor middle of the text to the given point

If you use bounding_box instead of draw_text you can specify the width of the bounding box for the text. Then if you centre-align the text you should be able to specify the exact x-coordinate of the middle. (it's the x-position of the box plus half the width)

Let's say you had a point at x-coordinate 72 and you want to put the label "hello world!" so the centre of the word is the same x-coordinate. You don't know how wide the word "hello" is but you're sure it'll fit inside a box of width 500.

72 - (500/2) = -178

bounding_box([-178, 100], :width => 500) do
text "hello world!", align => :center
end

Rails prawn chart not rendered completely at the bottom of the page

To answer the question in your comment as to determining page size I will run through a few useful methods too long for a comment:

d = Prawn::Document.new
d.y #full page height
d.margin_box.bottom #actually top since prawn starts at the bottom
d.margin_box.absolute_bottom #actual top with margins included
d.margin_box.top #usable page height
d.margin_box.absolute_top #same as #y
d.cursor #where you are vertically on the page

So you can use some basic math to determine fit:

#this is all chart does excepting chart calls #draw 
#which we don't want to do until we determine if it fits
c = Squid::Graph.new(d, choices: question.answers.choice_counter)

#determine if the chart will fit vertically
#if not start a new page and move to the top
unless d.cursor + c.height < d.margin_box.top
d.start_new_page
d.move_cursor_to(0)
end

#draw the chart onto the appropriate page
c.draw

Hope this helps in some way

Wrap text around an image in rails and prawn

If you know the width and height of the image, you can use text_box to position a text box next to the image, and collect the returned string of the text that did not fit. Then create a second text box or an ordinary text() call below the image and text_box, and you should be good to go.

This example should help:
http://github.com/sandal/prawn/blob/0.9.1/examples/text/text_box_returning_excess.rb

Getting Prawn to layout text like an absolutely positioned HTML paragraph

You have to provide the width of the text box, otherwise the text_box will use the page width minus the start position.

In order to calculate the string width, you should use the width_of method provided by the Prawn library, but, for some reason, Prawn doesn't recognise the \n char in width_of method, then you must have to split it by hand and calculate the line widths separately, which is not so difficult:

def text_width(pdf, text)
# slip by new line char and find maximum text width
text.split("\n").map {|s| pdf.width_of(s)}.max
end

Inserting text_width in your code:

pdf.text_box s = "Here's some left text,\nbla bla bla", 
:at => [20, page_height],
:align => :left,
:width => text_width(pdf, s)

pdf.text_box s = "Here's some center text,\nbla bla bla",
:at => [20, page_height-50],
:align => :center,
:width => text_width(pdf, s)

pdf.text_box s = "Here's some right text,\nbla bla bla",
:at => [20, page_height-100],
:align => :right,
:width => text_width(pdf, s)


Related Topics



Leave a reply



Submit