I would like to know is there any ruby gem or script to convert multipage pdf file into individual pdf files per pages in ruby. I tried with gems pdf-reader and prawn but not able to solve the problem. Help will be heartly appreciated. Thankyou.
Asked
Active
Viewed 613 times
1 Answers
1
The command-line utility PDFtk can do this easily
pdftk source.pdf cat 1-10 output first10.pdf
pdftk source.pdf cat 10-end output rest.pdf
or
pdftk source.pdf burst
# By default, the output files are named pg_0001.pdf, pg_0002.pdf, etc.
Build a utility method something like this:
def split_pdf(source_file, dest_dir)
Dir.mkdir(dest_dir.to_s)
exec("pdftk #{source_file} burst output #{dest_dir}/p%02d.pdf")
Dir.entries(dest_dir.to_s)
.select { |e| e.ends_with?('.pdf') }
.map { |f| "#{dest_dir}/#{f} }
end
and call it something like this:
source_file = Rails.root.join('public', 'source.pdf')
dest_dir = Rails.root.join('public', 'docs', doc_id)
page_files = split_pdf(source_file, dest_dir)

Unixmonkey
- 18,485
- 7
- 55
- 78