将PDF转换为JPG时质量低

wil*_*lex 3 ruby pdf rmagick imagemagick

我正在尝试使用Imagemagic(RMAgick)将PDF文档转换为图像.原始PDF也是从图像创建的(不是原生矢量PDF).

image = Magick::Image::from_blob(original_pdf) { self.format = 'PDF' }
image[0].format = 'JPG'
image[0].to_blob
image[0].write(to_file.jpg) {
  self.quality = 100
  self.density = 144
}
Run Code Online (Sandbox Code Playgroud)

但是在打印时,产生的图像质量太低.原始PDF同时具有良好的质量.我正在尝试使用这些选项

self.quality = 100
self.density = 144
Run Code Online (Sandbox Code Playgroud)

或使用PNG而不是JPG,但所有这些都不起作用,只增加kb中的图像大小).

Ale*_*nts 9

假设original_pdf是pdf文件的内容,例如:

original_pdf = File.open('from_file.pdf', 'rb').read
Run Code Online (Sandbox Code Playgroud)

然后在方法块中应用质量选项from_blob 而不是方法块write:

image = Magick::Image::from_blob(original_pdf) do
  self.format = 'PDF'
  self.quality = 100
  self.density = 144
end
image[0].format = 'JPG'
image[0].to_blob
image[0].write('to_file.jpg')
Run Code Online (Sandbox Code Playgroud)

看一下方法的质量选择Magick::ImageList.new.