aashish
aashish

Reputation: 2603

Insert an image into a pdf in ruby

Does anyone know how to insert an image into a PDF with Ruby?

I have been searching on the internet for some time, but most of the gems do not support the requirement.

The image should be inserted into an existing PDF with content. The content should not be removed. May be you can say inserting an image overlay in the PDF.

Thank you for help.

Upvotes: 3

Views: 2831

Answers (3)

aashish
aashish

Reputation: 2603

Resolved!!, able to insert an image into an existing PDF having content using hexapdf gem

Here is the code sample.

Please feel free review and comment.

Upvotes: 2

justapilgrim
justapilgrim

Reputation: 6852

You can use the gem Prawn, which provides a DSL for PDF generation.

Simply put gem 'prawn', '~> 2.1' to your Gemfile, run bundle install and you're done.

Prawn::Document.generate("mydocument.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

The Prawn::Images module documentation will be useful for you.

Upvotes: 1

Graham Slick
Graham Slick

Reputation: 6870

Prawn PDF does.

Here is a snippet:

pdf.image("./avatar.jpg", at: [180, 680], width: 200)

Upvotes: 1

Related Questions