Search code examples
ruby-on-railsrubyruby-on-rails-3rmagick

Why do my RMagick renderings of PDFs look so bad?


Everything looks fine in the PDF before it goes through rmagick:

here

But after going through rmagick the quality is just horrible:

here

What's going on? I'm not compressing it at all. Here is the method:

def create_pdf_image
  @document = Document.find(params[:document_id])
  ruler = 400
  pdf = Prawn::Document.new(:page_size => [ruler, ruler], :margin => 0, :optimize_objects => true)
  @document.post_pdf(pdf, ruler)
  temp = "#{@document.user.name.downcase.parameterize.underscore}-#{@document.id}"
  pdf.render_file("#{::Rails.root}/public/#{temp}.pdf")
  image = Magick::ImageList.new("#{::Rails.root}/public/#{temp}.pdf")
  image.strip!
  image.write("#{::Rails.root}/public/#{temp}.jpg") { self.quality = 100 }
  send_file("#{::Rails.root}/public/#{temp}.jpg")
end

Any help'd be great. Thanks!


Solution

  • Your input file is PDF (vector file) and ImageMagick has to convert it to to bitmap. By default density of pixels for input file is set to 72x72 (horizontal x vertical). You can override it, but only when creating ImageList:

    path = "#{::Rails.root}/public/#{temp}.pdf"
    image = Magick::ImageList.new(path) { self.density = 300 }
    

    That should set density of input file to 300ppi, which should be enough.