Skip to content

Instantly share code, notes, and snippets.

@arika
Created April 11, 2012 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arika/2359030 to your computer and use it in GitHub Desktop.
Save arika/2359030 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
require 'mini_magick'
require 'origami'
include Origami
def fixup!(pdf)
pdf.root_objects.each do |obj|
next unless obj.is_a?(Graphics::ImageXObject)
Dir.mktmpdir do |dir|
ext, data = obj.to_image_file
tf = "#{dir}/tmp.#{ext}"
open(tf, 'w:binary') {|io| io.print data }
img = MiniMagick::Image.open(tf)
cs = img['colorspace']
img.combine_options do |c|
if cs == 'DirectClassRGB'
c.auto_level
else
c.level '20%,80%'
c.sharpen '5'
c.colorspace 'gray'
end
c.resize 'x1400'
end
img.write(tf)
open(tf, 'r:binary') do |io|
obj.rawdata = io.read
end
obj.Width, obj.Height = img['dimensions']
end
end
end
ARGV.each do |fn|
pdf = PDF.read(fn)
fixup!(pdf)
pdf.saveas("t_#{fn}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment