Convert .pdf to images using RMagick & Ruby

老子叫甜甜 提交于 2020-01-22 05:21:07

问题


I'd like to take a pdf and convert it to images...each pdf page becoming a separate image.

There's a similar post here: Convert a .doc or .pdf to an image and display a thumbnail in Ruby? But it doesn't cover how to make separate images for each page.


回答1:


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

EDIT:

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.




回答2:


Using RMagick itself, you can create image for different pages.

require 'RMagick'
pdf_file_name = "test.pdf"
im = Magick::Image.read(pdf_file_name)

Above will give you an array arr[], which will have one entry for corresponding pages. Do if you want to generate an image(jpg) of 5th page, you can do:

im[4].write(pdf_file_name + ".jpg")

But this will load complete pdf, so slow.

Alternatively, if you want to create image of 5th page and dont want to load the complete PDF file, do as below:

require 'RMagick'
pdf_file_name = "test.pdf[5]"
im = Magick::Image.read(pdf_file_name)
im[0].write(pdf_file_name + ".jpg")



回答3:


Since I can't find a way to deal with PDFs on a per-page basis in RMagick, I'd recommend first splitting the PDF into pages with pdftk's burst command, then dealing with the individual pages in RMagick. This is probably less performant than an all-in-one solution, but unfortunately no all-in-one solution presents itself.

There's also PDF::Toolkit for Ruby that hooks into pdftk but I've never used it.



来源:https://stackoverflow.com/questions/2974442/convert-pdf-to-images-using-rmagick-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!