Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert PDF files to images using RMagick and Ruby

I'd like to take a PDF file and convert it to images, each PDF page becoming a separate image.

"Convert a .doc or .pdf to an image and display a thumbnail in Ruby?" is a similar post, but it doesn't cover how to make separate images for each page.

like image 996
tybro0103 Avatar asked Jun 04 '10 13:06

tybro0103


People also ask

Can I turn a PDF into images?

Navigate to the convert PDF page on Acrobat online services. Click the Select A File button or drag and drop the file into the drop zone. Choose your preferred file format — PNG, JPG, or TIFF from the drop-down menu. Click the Convert To JPG button, or whichever format you selected.

How do I convert PDF to image in PyPDF2?

PyPDF2 also doesn't have any capabilities to convert a PDF file into an image, which is understandable since it does not use any core PDF libraries. So if you want to convert your PDF to an image file, the best you can do is extract text and write it to an image file.


1 Answers

Using RMagick itself, you can create images for different pages:

require 'RMagick'
pdf_file_name = "test.pdf"
im = Magick::Image.read(pdf_file_name)

The code above will give you an array arr[], which will have one entry for corresponding pages. Do this if you want to generate a JPEG image of the fifth page:

im[4].write(pdf_file_name + ".jpg")

But this will load the entire PDF, so it can be slow.

Alternatively, if you want to create an image of the fifth page and don't want to load the complete PDF file:

require 'RMagick'
pdf_file_name = "test.pdf[5]"
im = Magick::Image.read(pdf_file_name)
im[0].write(pdf_file_name + ".jpg")
like image 107
Akash Agrawal Avatar answered Sep 17 '22 08:09

Akash Agrawal