2017-07-03 95 views

回答

1

的命令行實用程序PDFtk可以做到這一點很容易

pdftk source.pdf cat 1-10 output first10.pdf 
pdftk source.pdf cat 10-end output rest.pdf 

pdftk source.pdf burst 
# By default, the output files are named pg_0001.pdf, pg_0002.pdf, etc. 

建立一個實用的方法是這樣的:

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 

,並調用它是這樣的:

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) 
+0

非常感謝@Unixmonkey .... – codemilan