2

I have tried using a gem called docsplit and got nothing but errors; even when trying the documentation examples, they wouldnt work on my windows machine.

I ran across a blog post giving me hope that this could be done in pdftk: here

I have a pdf named multi_page.pdf

I need to split multi_page.pdf into many single page PDF's and output the many single page PDF's in a sub directory called destination

here is where I am, and its definitely not working

source_path = 'C:\Users\ALilland\Documents\split_test\one_page\destination'
source_file = 'C:\Users\ALilland\Documents\split_test\one_page\multi_page.pdf'
FileUtils.cd(source_path)
`pdftk #{source_file} burst output page_%02d.pdf`
ADL
  • 185
  • 8
  • 19
  • Tried your code on Ubuntu and it works ok. Please pass the errors you get if any present – K.M. Mar 11 '16 at 22:49
  • i just got it to work doing %x(pdftk C:\\Users\\ALilland\\Documents\\split_test\\one_page\\multi_page.pdf burst) – ADL Mar 11 '16 at 23:44

2 Answers2

14

You can try it using the combine_pdf gem:

require 'combine_pdf'

pages = CombinePDF.load("my_pdf.pdf").pages;
i = 0
pages.each do |page|
   pdf = CombinePDF.new
   pdf << page
   pdf.save("#{i}.pdf")
   i+=1
end

The combine_pdf gem (I'm the author) is a native Ruby solution, so you don't have to worry about pdftk's installation and requirements.

Myst
  • 18,516
  • 2
  • 45
  • 67
  • so much cleaner than pdftk thank you for writing it! – ADL Mar 21 '16 at 03:38
  • How is the performance of this vs Ruby Vips? https://github.com/libvips/ruby-vips Will it split a 100 page pdf no problem? I've had issues with ImageMagick performance splitting PDFs. – fatfrog Sep 01 '21 at 15:49
  • @fatfrog - I have no idea. It would be great if you could test it and post something about it – Myst Sep 01 '21 at 19:19
0

Another solution is to use hexapdf

$ gem install hexapdf
$ hexapdf modify -i 1-10 input.pdf output.pdf

where 1-10 is a PAGE SPECIFICATION according to their docs. It says "pages 1-10" will be in the output PDF.

Lance
  • 75,200
  • 93
  • 289
  • 503