How to Convert PDF Files to Images Using Rmagick and Ruby

How to convert PDF files to images using RMagick and Ruby

ImageMagick can do that with PDFs. Presumably RMagick can do it too, but I'm not familiar with it.

The code from the post you linked to:

require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")

pdf is an ImageList object, which according to the documentation delegates many of its methods to Array. You should be able to iterate over pdf and call write to write the individual images to files.

Converting a pdf to jpeg using Rmagick

There appears to have been an issue with the service that was providing the pdf to me. The pdf was recently changed to a secure state and I needed to utilize access keys. This resulted in rmagick not being able to access the image file and returning the stated error.

How to convert the pdf uploaded using Active Storage to image in rails

According to the rmagick docs, imagelist does not support urls for converting images. You need to use open-uri gem and URI.open method to open the PDF and pass it to the imagelist.



pdf_to_image.rb
class PdfToImage
require 'rmagick'
require 'open-uri'

attr_reader :pdf

def initialize(pdf)
@pdf = pdf
end

def perform
Magick::ImageList.new(URI.open(@pdf).path)
end
end



Related Topics



Leave a reply



Submit