Same Quality Conversion From PDF to Image in RMagick

Today one of my tasks was to convert a pdf to a png of the same quality, but sadly, all my attempts were all low resolution.
So after a little research on the documentation i found out the density option, which by default is 72. PDF is around 300, so 200 is a good tradeoff (at least, for now).
Also, i found a little bit misguiding that in some guides , the density declaration is after the write function, while should be right after you read the file….by the way, here is a nice example! Enjoy!


Conversion from PDF to Image without quality loss.
1
2
3
4
5
6
7
8
require 'rmagick'
# Usage: MyFancyConverter.convert("path/to/your/file.pdf", ".png")
class MyFancyConverter
  def self.convert(file_path, format)
    image = Magick::ImageList.new(file_path) { self.density = 200 }
    image.write(file_path.gsub(".pdf", ".#{format}"))
  end
end