Write images in PDF format

Hi All,

My Environment:

OS: Windows, Ruby1.8.6, pdfwriter-1.1.8

My need:

I have 10 jpeg images in my local system(C:\jazzezravi\1.jpg).
I want to write all the 10 images into one PDF format. Just now i
installed pdf-writer gem.

Thank you.


On Thu, May 22, 2008 at 7:49 AM, Raveendran J.
[email protected] wrote:

installed pdf-writer gem.

Thank you.


it’s easy, I think you’ll find some examples either in pdfwriter docs
or somewhere else on the web. Just google a bit.

If you get stuck, post the code here and somebody will try to help you.

This is my code for something similar - I do some resizing too. You
should be able to produce similar
code in a short time - everything you need is in the docs (for me the
initial version took 1-2 hours max).


require ‘pdf/writer’
pdf = PDF::Writer.new(:paper => “A4”, :orientation => :portrait)

[‘1.jpg’, ‘2.jpg’, ‘3.jpg’].each do |jpg|
image_data = File.open(jpg, ‘rb’) {|f| f.read }
image_info = PDF::Writer::Graphics::ImageInfo.new(image_data)
p image_info.width, pdf.page_width
p image_info.height, pdf.page_height
ratio = [pdf.page_width/image_info.width,
p ratio
width = image_info.width * ratio
height = image_info.height * ratio
p width, height
offset_x = (pdf.page_width - width) / 2
offset_y = (pdf.page_height - height)

    pdf.add_image(image_data, offset_x, offset_y, width, height)



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs