Prawn Doesn't Seem to Push Layout Down When Using Repeat(:All)

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)

image and the code broke down when generating the pdf using prawn, rails

I haven't used the prawn gem. But you can try your luck with Wicked PDF. This is also one of the gem most developers use to generate pdf. You can have customized CSS for the pdf layout. Hope this might solve your problem.

Why does Prawn resize my image automatically?

you can try passing the width and height that the image actually is see if that helps

Prawn::Images

Public Instance Methods
image(file, options={}) click to toggle source

Add the image at filename to the current page. Currently only JPG and PNG files are supported.

NOTE: Prawn is very slow at rendering PNGs with alpha channels. The
workaround for those who don’t mind installing RMagick is to use:

github.com/amberbit/prawn-fast-png

Arguments:

file path to file or an object that responds to #

Options:

:at an array [x,y] with the location of the top left corner of the image.

:position One of (:left, :center, :right) or an x-offset

:vposition One of (:top, :center, :center) or an y-offset

:height the height of the image [actual height of the image]

:width the width of the image [actual width of the image]

:scale scale the dimensions of the image proportionally

:fit scale the dimensions of the image proportionally to fit inside [width,height]

Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do     
pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg"
image pigs, :at => [50,450], :width => 450

dice = "#{Prawn::BASEDIR}/data/images/dice.png"
image dice, :at => [50, 450], :scale => 0.75
end

If only one of :width / :height are provided, the image will be scaled proportionally. When both are provided, the image will be stretched to fit the dimensions without maintaining the aspect ratio.

If :at is provided, the image will be place in the current page but the text position will not be changed.

If instead of an explicit filename, an object with a read method is passed as file, you can embed images from IO objects and things that act like them (including Tempfiles and open-uri objects).

  require "open-uri"

Prawn::Document.generate("remote_images.pdf") do
image open("http://prawn.majesticseacreature.com/media/prawn_logo.png")
end

This method returns an image info object which can be used to check the dimensions of an image object if needed. (See also: Prawn::Images::PNG , Prawn::Images::JPG)

Prawn : adding background image of larger resolution

What you're trying to do can't be done using the :background option of Prawn::Document.generate since the :background option can't scale the image. If you want to use the :background option, you need to make sure your image is the same dpi as the PDF (normally 72dpi).

You can however simply imbed the image in the page, like you would a normal image and then float the text over the top of it. This works since when you embed an image you're able to scale it. The code might look something like this:

Prawn::Document.generate("#{Rails.root.to_s}/public/#{filename}.pdf", :page_size => [576,576], :left_margin => 50, :right_margin => 50, :page_layout => :portrait, :skip_page_creation => true, :skip_encoding => true) do |pdf|
bg_image = "#{Rails.root.to_s}/public/images/pdf/bg_blank_low.jpg"
pdf.image bg_image, :scale => 0.2311
pdf.move_up 576
end

This will make your 'background' fully cover the page (since we manually calculated the scale 2700/576), if you want to respect your margins (this is probably a better way in general), you might alter to something like:

pdf.image bg_image, :width => pdf.bounds.width

This should automatically scale the image based on the width of the bounding box of the page. Of course you would need to change your move_up as well, something like:

pdf.move_up pdf.bounds.height

After you did this you can start putting your text in and it should appear over the top of your image and so we get our simulated scaled background.

Update

This is an update with regards to the comment. If you have pages being automatically created and you want them to have the same background then you're out of luck if you're using the current prawn release the way it is. If you really need this functionality, then you have to patch prawn.

Grab the prawn source (from https://github.com/sandal/prawn) and have a look at it. What you're after is lib/document.rb, on line 244 there is a method start_new_page, this is the one you're after. Within this method on line 280, you can see where the background is being set. Unfortunately it is using canvas which means, your image already has to be of the right size. This is why the background image doesn't scale automatically.

You will need to override this behaviour. Since this is Ruby, all you have to do is reopen the class within your project and then copy paste this method in (if you need more info on how to do this, there is plenty around on monkey-patching Ruby classes). Now you edit this method to your hearts content. The easiest way is probably to remove the canvas all together and then use our image trick from above. So the line ends up as:

image(@background, :width => bounds.width) if @background
move_up bounds.height

You can now go back to using the standard way of setting the background and everything should work.

Infact, you may even be able to get away with changing line 280 to this:

canvas { image(@background, :width => bounds.width) } if @background

And everything should work fine, saving you having to type an extra line :). Using image with the :width option should automatically scale the image, while using the :at option as prawn does won't scale the image.

Note: I haven't actually done this so you may need to work the kinks out.



Related Topics



Leave a reply



Submit