Srinivasan
Srinivasan

Reputation: 72

Ruby + how to split pdf into separate pages?

I'm using Docsplit to split pdf into pages using

Docsplit.extract_pages("my.pdf").

But I want to limit the pages to 4. I tried

Docsplit.extract_pages("my.pdf", :pages => 1..4) 

which is not working..

Can anyone suggest me what to do

Upvotes: 2

Views: 1890

Answers (1)

kirkytullins
kirkytullins

Reputation: 152

  1. install pdftk in you machine if not already done and set your path accordingly
  2. remove the ESCAPEs from the lib/docscript/page_extractor.rb:18 file like so:

    pdftk #{ESCAPE[pdf]} burst output #{ESCAPE[page_path]} 2>&1"

change to :

pdftk #{pdf} burst output #{page_path} 2>&1"
  1. by default, the gem ignores the page range you give and it will create one pdf file per page. If you're happy with this, then the output pages are created in the same folder as your input file.

However, the easiest solution IMHO would be to just use to pdftk binary directly, it's quite straightforward: to extract pages 1-4, you could use this snippet :

in_file = 'IN.pdf'
range = 1..4
range_s = range.to_s.gsub('..', '-')
cmd = "pdftk.exe #{in_file} cat #{range_s} output pages#{range_s}.pdf"
res = `cmd`.chomp

This works, provided that the pdftk executable is in your PATH

Upvotes: 1

Related Questions