Write images in PDF format

R

Raveendran Jazzez

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.

Regards,
P.Raveendran
RailsFactory
http://raveendran.wordpress.com
 
J

Jano Svitok

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.

Hi,

it's easy, I think you'll find some examples either in pdfwriter docs
(http://ruby-pdf.rubyforge.org/pdf-writer/index.html),
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).

J.

require 'pdf/writer'
pdf = PDF::Writer.new:)paper => "A4", :eek:rientation => :portrait)

['1.jpg', '2.jpg', '3.jpg'].each do |jpg|
pdf.start_new_page
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,
pdf.page_height/image_info.height].min
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)
end

pdf.save_as("out.pdf")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,618
Latest member
Leemorton01

Latest Threads

Top