Search code examples
rubypdfpdftk

Ruby - using pdftk to split a multi page pdf into many single page pdf's?


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`

Solution

  • 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.