Skip to content

Instantly share code, notes, and snippets.

@airspeed
Last active December 14, 2015 07:09
Show Gist options
  • Save airspeed/5048827 to your computer and use it in GitHub Desktop.
Save airspeed/5048827 to your computer and use it in GitHub Desktop.
def remove_blanks(filename)
nonblank, ranges, range_first_val, range_last_val = [], [], 0, 0
File.open(filename, "rb") do |io|
reader = PDF::Reader.new(io)
reader.pages.each_with_index do |page, i|
nonblank << (i + 1) unless page.text.length.zero?
end
range_first_val, range_last_val = nonblank.first, nonblank.last
range_first_val = -1 if nonblank.length === reader.pages.length
end
return filename if range_first_val === -1 # Keine leeren Seiten.
return nil if nonblank.length.zero? # Keine nicht-leeren Seiten.
nonblank.each do |current_page_val|
if current_page_val > range_last_val + 1
ranges << "%s-%s" % [range_first_val, range_last_val]
range_first_val = current_page_val
end
range_last_val = current_page_val
end
ranges << "%s-%s" % [range_first_val, range_last_val]
puts ranges
FileUtils.mv filename, filename + '.blanks.pdf'
system("pdftk #{filename + '.blanks.pdf'} cat #{ranges.join(' ')} output #{filename}")
filename
end
# usage remove_blanks("/home/fpuglisi/Downloads/orig.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment