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
How to Check If a Class Is Defined
Ruby on Rails Advanced JSON Serialization
Why Does Accessing a Ssl Site with Mechanize on Windows Fail, But on MAC Work
How to Backreference in Ruby Regular Expression (Regex) with Gsub When I Use Grouping
Convert an Array to Hash, Where Keys Are the Indices
Is There a Way Rails 3.0.X Can Default to Using Thin
Private Class (Not Class Method) in a Ruby Module
How to Handle Errors with Httparty
Executing User-Supplied Ruby Code on a Web Server
How to Display a PDF in Ror (Ruby)
How to Spawn a Child Process in Ruby
Ruby/Rails - .Each Iterator Is Printing Entire Array at the End of the Loop
Get Absolute (Base) Url in Sinatra
How to or Should I Find an Object by the Object_Id Attribute in Ruby