Skip to content

Splitting a PDF into multiple images

Janko Marohnić edited this page Nov 16, 2019 · 4 revisions

This guide will show you how you can split a PDF into multiple images, one for each page.

MiniMagick

page_count = MiniMagick::Image.new(pdf_path).pages.count

magick = ImageProcessing::MiniMagick
  .source(pdf_path)
  .convert("jpg")

images = page_count.times.map do |page_number|
  magick.loader(page: page_number).call
end

images #=>
# [
#   #<File:/tmp/...>, (page 1)
#   #<File:/tmp/...>, (page 2)
#   ...
# ]

Vips

page_count = Vips::Image.new_from_file(pdf_path).get("pdf-n_pages")

vips = ImageProcessing::Vips
  .source(pdf_path)
  .convert("jpg")

images = page_count.times.map do |page_number|
  vips.loader(page: page_number).call
end

images #=>
# [
#   #<File:/tmp/...>, (page 1)
#   #<File:/tmp/...>, (page 2)
#   ...
# ]