I'm creating a pdf book where I need to put a background image for each page.
The size of the page is (576 x 576) and the size of the background image is 2700 x 2700 (300 dpi.) (These sizes are the requirements so cannot be adjusted).
My problem is - the background image appears out of proportion in the page. How can I fix this ?
Here's my code:
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,
:background => "#{Rails.root.to_s}/public/images/pdf/bg_blank_low.jpg" ) do |pdf|
....
....
....
)
Is there any other way by which I can place the image of 300 dpi as a background image.
I even tried adding pdf template as a background, but still no luck.
Any suggestion or hint will be much appreciated.
Thanks.
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.
If you only have one PDF page, a solution is to just don't use the background property at all, but just place the image at the first position of each page and define width as the full width with
pdf.bounds.width
If someone needs to add the picture after pages have been generated or just adding a transparent picture you can use :
pdf.transparent(0.5) do
pdf.image(file, options = {
:at => [200, 220],
:scale => 0.2
})
end
Related
I have an image here at this link: http://i711.photobucket.com/albums/ww116/xuanchienuit/1.png
I am trying to insert this image as background to the pdf using the following method:
image_path = "..." #path to the image
pdf = Prawn::Document.new
pdf.image image_path, width: pdf.bounds.width, height: pdf.bounds.height
pdf.render_file "test.png"
And the output PDF is at this link: https://drive.google.com/file/d/0B5iZPOjOn1osUzJSUTdDZGROZzQ/view?usp=sharing
The problem is in the PDF, you can see a gray border on top and bottom of the image (if you don't see the border, try zooming in/out and you would see the border).
What is going on here? I think there could be something wrong with the image but I am not sure. I hope you guys will have a better thought.
Thanks in advance!
Rather than setting height and width, try setting the position, and letting the image scale to best fit the available space (match height, match width, match both)? You might -- all conjecture here -- be seeing some artifact of the image getting stretched to the pdf region (if the image dimensions don't match the pdf precisely).
pdf.image image_path,
:position => :center,
:vposition => :center,
:fit => [pdf.bounds.width, pdf.bounds.height]
I am a programming and Ruby novice, trying to use Prawn to generate a PDF. I am following the Prawn manual but am having problems with putting text in a bounding box. Here is my code, which mostly comes from the Prawn manual... :
pdf.bounding_box([200, pdf.cursor - 100], :width => 200, :height => 100) do
pdf.text "Just your regular bounding box"
pdf.transparent(0.5) { pdf.stroke_bounds }
end
If I remove this line: pdf.transparent(0.5) { stroke_bounds },
The code will work but I can't see the text box. What do I need to change?
I'm not sure I fully understand your issue. When you say
the code will work, but I can't see the text box
do you mean that you can't see the box's borders, but you can still see the text? Or that you can't see the text either?
I would expect the former, since it is the stroke_bounds that causes the borders to be drawn.
So to answer your "what do I need to change", it depends on what you're trying to accomplish:
if you need the text but don't need the box borders, you can remove the `pdf.transparent(0.5) { pdf.stroke_bounds }, whose only job is to draw a semi-transparent (i.e. gray) border around the bounding box
if you want the border, leave that line in
if you want the border, but want the height of the box to conform to the height of the text, then remove the :height => 100 from the first line. The bounding_box height will then adjust to fit the text.
I've got pdf file and I want to add an image on it. There are two pages in my pdf file and I want to add image on every page.
Prawn::Document.generate("img/full_template.pdf", :template => "img/template.pdf") do |pdf|
avatar = "img/crop_after.png"
pdf.image avatar, :at => [100,10], :scale => 0.4
pdf.image avatar, :at => [300,30], :scale => 0.4
end
When I run this code I got four avatar images on first pdf page (two in full size and two scaled and in position I want).
Can anyone tell me, how can I add first image to the first pdf page and second image to second pdf page?
Try using start_new_page between the images.
That way your second image will be placed on a new page.
EDIT: If your template already has multiple pages you can use go_to_page to navigate between the pages
http://prawn.majesticseacreature.com/docs/0.11.1/Prawn/Document.html#method-i-go_to_page
I want to clip an image if it goes beyond the dimensions of a bounding box. Just like how CSS overflow: hidden would do it. Eg.
pdf.grid([0, 0], [3, 27]).bounding_box do
pdf.image image_file
end
Now currently, this image would overflow outside the bounding box if its larger than it. Is there any way to clip the image when it goes beyond the bounding box. ? I know this is possible for text when using text_box.
you can set the size of the image or get the image to scale so it fits within a certain area while maintaining proportions, do not believe you can crop an image.
If your page is not dynamic, that is the image area will always be the same this should be OK.
pdf.image "image_file_path_&_name", :position => :center, :fit => [100,450];
This is based on v0.8.4.
Unfortunately, there seems to exist no proper way to crop an image to a bounding box at the moment. Faced with this problem I figured out this beauty:
class SamplePdf
include Prawn::View
def initialize
crop_width = 100 # your width here
crop_height = 50 # your height here
image_path = '/path/to/your_image.jpg'
bounding_box [0, 0], width: crop_width, height: crop_height do
pdf_obj, _ = build_image_object(image_path)
x, y = document.send(:image_position, crop_width, crop_height, {})
document.send(:move_text_position, crop_height)
label = "I#{document.send(:next_image_id)}"
document.state.page.xobjects.merge!(label => pdf_obj)
cm_params = PDF::Core.real_params([crop_width, 0, 0, crop_height, x, y - crop_height])
document.renderer.add_content("\nq\n#{cm_params} cm\n/#{label} Do\nQ")
end
end
end
It basically adapts the Prawn::Images#image method, but skips the calculation of the image's dimensions and the scaling respectively.
It's not exactly a clean solution. Please keep me posted if you find a better one.
You should keep in mind though that this snippet leverages some implementation details which are not part of Prawn's public API and can change anytime.
At the time of writing Prawn 2.0.1 was the most recent version.
s it posible to align an image to the right and wrap text around the image like it is in html and css using the float:right property ?
If so how do you do this ?
I can align an image but dont't know how to wrap the text around it. The text is dynamic text therefore varies alot in length.
Thanks alot
Rick
One suggestion is to try nested bounding boxes. The main bounding box would have the text inside it. with at some point another bounding box for the image. Something along the lines of
bounding_box([x,y], :width => bounds.width, :height => 400) do
text "blah"
text "blah"
# image
bounding_box([bounds.right - image_width, 0], :width => image_width) do
image("path_to_file", :at => [0,0], :width => bounds)
text "more blah"
end
You may be able to simply use the image without the bounding box, but the bounding box would ensure that the text flows around it.