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
Related
I have the following code:
begin
big_image = Magick::ImageList.new
#this is an image containing first row of images
first_row = Magick::ImageList.new
#adding images to the first row (Image.read returns an Array, this is why .first is needed)
first_row.push(Magick::Image.read(Rails.root.join("app","assets","images","logo.png")).first)
if #model.avatar.exists?
image = Magick::Image.read(#model.avatar.path).first
image = image.resize_to_fit("450", "401")
first_row.push(image)
end
#adding first row to big image and specify that we want images in first row to be appended in a single image on the same row - argument false on append does that
big_image.push (first_row.append(false))
fileName = #model.id.to_s + ".png"
big_image.append(true).write(Rails.root.join("app","assets","images","shared_logo",fileName))
rescue => e
puts "Errors! -- #{e.inspect}"
end
The codes puts two image on the same row. The images are png. The problem is that the second image has an height less than the first one. Image magick fill the remaining part with an unwanted white background. I want to keep the transparency on the combined image.
You could do something like:
manipulate! do |img|
img.combine_options do |c|
c.background "transparent"
c.gravity "center"
c.extent "450x401"
end
end
This is for RMagick if I'm not mistaken, the names might be slightly different (although I think combine options is using mongrifies methods?).
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]
Can you tell me how to insert image which will be a link to for example page 20? I know how to make with normal text:
text "<link anchor='page20'>Go to page 20</link>", :inline_format=>true
and then on page 20 I have
add_dest('page20', dest_fit(page.dictionary))
but how to do this with image ?
Partly thanks to lightswitch05 for some prodding in the right direction, I've found a way to get the effect I want through this inelegant way:
Insert an image in a bounding_box (the page cursor is at this point at the bottom of the image)
Move the cursor back up to the top of the image
Insert a text link over the image (in my case I just used however many vertical bars '|' were needed to cover the image)
Confirm visually that the clickable link area is about the same as the boundaries of the image
Make the text link transparent, and voilĂ , it looks like you're clicking the image.
Here's some example code (measurements not exact; there was a lot of tweaking involved):
bounding_box([0, cursor], width: 35) do
image open("http://mysite.com/remote_image.jpg"),
fit: [35, 35],
align: :center
move_up 35
transparent(0) do
formatted_text([{
text: "|||", # placeholder
size: 40,
link: "http://example.com/"
}], align: :center)
end
# stroke_bounds
end
Needless to say, this experience has got me looking a bit more at Wicked PDF in order to do what I think I want to do with PDFs.
I'm sure a better/more elegant solution exists, so I'm not planning on considering this my final answer.
Prawn does not support this functionality. In fact, even if you placed a formatted_text_box over an image and fill it with white space, it still will not work. An anchor has to include text to work. If you don't mind having text over your image, then that might be a solution.
Prawn's own readme states:
One thing Prawn is not, and will never be, is an HTML to PDF generator.
After dealing with many of Prawn's shortcomings, I've switched to using wicked_pdf for my Ruby on Rails PDF generation and have been very happy with it. If you can do it in html & css, it can be done with wicked_pdf.
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
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.