Padding/margin in paragraphs

369 views
Skip to first unread message

esti

unread,
Aug 26, 2008, 6:01:34 AM8/26/08
to Prawn
Hello!

Sorry for my newbie question but, how can I add margin or padding in a
paragraph? I've tried this:

<pre>
answers.each_with_index do |answer, i|
pdf.bounding_box [100, 100], :width => 200 do
pdf.text "Answer + i.to_s + ":" + answer
end
end
</pre>

... but all the answers are placed in the same absolute position and
overlap with each other.

The only place in the API where I've found something related to
padding is in tables. Do I have to make a table for this?

Thank in advance!

Gregory Brown

unread,
Aug 26, 2008, 10:42:04 AM8/26/08
to prawn...@googlegroups.com
On Tue, Aug 26, 2008 at 6:01 AM, esti <esti.a...@gmail.com> wrote:
>
> Hello!
>
> Sorry for my newbie question but, how can I add margin or padding in a
> paragraph? I've tried this:
>
> <pre>
> answers.each_with_index do |answer, i|
> pdf.bounding_box [100, 100], :width => 200 do
> pdf.text "Answer + i.to_s + ":" + answer
> end
> end
> </pre>
>
> ... but all the answers are placed in the same absolute position and
> overlap with each other.

Your loop is *outside* the bounding box, re-setting the top left
corner each time.

try:

pdf.bounding_box [100,100], :width => 200 do
answers.each_with_index do |answers,i|
pdf.text "Answer + i.to_s" + i.to_s + ":" + answers
end
end

To put padding between the text, check out pad(), pad_bottom(),
pad_top(), and move_down()

-greg

--
Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
stuff at: http://metametta.blogspot.com

esti

unread,
Aug 28, 2008, 4:00:35 AM8/28/08
to Prawn
Hi Gregory!

I'm still not getting what I need. I tried what you said but now I'm
getting each answer in a different page, in (100, 100) position. What
I want is different paragraphs in the same page, with some blank space
on the left.

I knew about pad() functions but I want to add the padding to the
left, which is not possible with any of them.

The output that I want, is something like this (notice blank spaces in
the left):

Question: Is this a question?
Answers:
Answer 1: Yes it is
Answer 2: No it isn't

Any ideas?

Gregory Brown

unread,
Aug 28, 2008, 10:23:19 AM8/28/08
to prawn...@googlegroups.com
On Thu, Aug 28, 2008 at 4:00 AM, esti <esti.a...@gmail.com> wrote:
>
> Hi Gregory!
>
> I'm still not getting what I need. I tried what you said but now I'm
> getting each answer in a different page, in (100, 100) position. What
> I want is different paragraphs in the same page, with some blank space
> on the left.

If that's what you're getting, your code is actually more complicated
than the examples you've shared.

Gregory Brown

unread,
Aug 28, 2008, 10:28:58 AM8/28/08
to prawn...@googlegroups.com
On Thu, Aug 28, 2008 at 10:23 AM, Gregory Brown
<gregory...@gmail.com> wrote:
> On Thu, Aug 28, 2008 at 4:00 AM, esti <esti.a...@gmail.com> wrote:
>>
>> Hi Gregory!
>>
>> I'm still not getting what I need. I tried what you said but now I'm
>> getting each answer in a different page, in (100, 100) position. What
>> I want is different paragraphs in the same page, with some blank space
>> on the left.
>
> If that's what you're getting, your code is actually more complicated
> than the examples you've shared.

I'm not sure that you're intentionally putting your text at the bottom
of the page. Y=0 is at the bottom margin in Prawn. (Or the bottom of
any bounding box you create)

require "rubygems"
require "prawn"

pdf = Prawn::Document.new
answers = ["Yes it is", "No it isn't"]
pdf.bounding_box [100,pdf.bounds.top-100], :width => 200 do
answers.each_with_index do |answer,i|
pdf.text "Answer #{i}: #{answer}"
end
end
pdf.render_file "foo.pdf"

Esti Alvarez

unread,
Aug 31, 2008, 6:35:25 AM8/31/08
to prawn...@googlegroups.com
Hi Gregory!

Your example works exactly as I need, but I somehow can't get it to
work in my code. I'm travelling this week and don't have much time to
take a careful look, I'll get back to this next week.

Thanks a lot for your help!

esti

On Thu, Aug 28, 2008 at 4:28 PM, Gregory Brown

Esti Alvarez

unread,
Sep 8, 2008, 11:35:46 AM9/8/08
to prawn...@googlegroups.com
Hi again!

As I said, I can't get this to work. I think the problem is that I
have text before and after this block. Can you please have a look at
this example?

require "rubygems"
require "prawn"

pdf = Prawn::Document.new
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"

answers = ["Yes it is", "No it isn't"]

pdf.bounding_box [100,pdf.bounds.top-10], :width => 200 do


answers.each_with_index do |answer,i|
pdf.text "Answer #{i}: #{answer}"
end
end

pdf.text "More stuff"
pdf.render_file "foo.pdf"

I would expect "Some stuff" lines to go first, then "Answers" below,
and then "More stuff" below, but I see everything cluttered up.

Anybody?

Gregory Brown

unread,
Sep 8, 2008, 11:46:09 AM9/8/08
to prawn...@googlegroups.com

If you place the bounding box 10 points from the top of your page (as
you are here, look at the top-left corner of the box), why would you
expect this?

I think what you want is:

pdf.bounding_box [100, pdf.y - pdf.bounds.absolute_bottom], ...

-greg

esti

unread,
Sep 8, 2008, 11:50:00 AM9/8/08
to Prawn
Hey!

Forget my last post, I found the solution in this other thread!
http://groups.google.com/group/prawn-ruby/browse_thread/thread/cc0930099ff4c4aa?hl=en

So, what I needed was to replace "pdf.bounds.top-10" with "pdf.y -
pdf.bounds.absolute_bottom". Here is the whole script in case it is
useful for somebody else.

require "rubygems"
require "prawn"

pdf = Prawn::Document.new
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"
pdf.text "Some stuff"

answers = ["Yes it is", "No it isn't"]
pdf.bounding_box [100,pdf.y - pdf.bounds.absolute_bottom], :width =>
200 do
answers.each_with_index do |answer,i|
pdf.text "Answer #{i}: #{answer}"
end
end
pdf.text "More stuff"
pdf.render_file "foo.pdf"


Thank a lot for the help!

esti
Reply all
Reply to author
Forward
0 new messages