Created
January 23, 2010 16:53
-
-
Save yuanying/284687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -wKU | |
# PDF to Zip file (including jpg files) script. | |
# sudo port install rb-cocoa | |
# sudo gem install zipruby | |
require 'osx/cocoa' | |
require 'rubygems' | |
require 'fileutils' | |
require 'zipruby' | |
class Pdf2Zip | |
def find_pdf_rep source | |
reps = source.representations | |
(0...reps.count).each do |index| | |
pdf = reps.objectAtIndex(index) | |
return pdf if pdf.isKindOfClass(OSX::NSPDFImageRep) | |
end | |
return nil | |
end | |
def calc_size pdf_rep, fit=800 | |
width = pdf_rep.pixelsWide | |
height = pdf_rep.pixelsHigh | |
if width > height | |
height = (fit.to_f / width.to_f * height.to_f).to_i | |
width = fit | |
else | |
width = (fit.to_f / height.to_f * width.to_f).to_i | |
height = fit | |
end | |
OSX::NSMakeSize(width, height) | |
end | |
def pdf2zip pdf_path, output_folder, suggestion_size=800, compression_factor=0.7 | |
output_file = File.join( output_folder, File.basename(pdf_path, File.extname(pdf_path)) ) | |
return false if File.file?("#{output_file}.zip") | |
temp_folder = "#{output_file}.temp" | |
puts "Creating zip: #{pdf_path} to #{output_file}" | |
source = OSX::NSImage.alloc.initWithContentsOfFile(pdf_path) | |
source.setScalesWhenResized(true) | |
source.setDataRetained(true) | |
pdf_rep = find_pdf_rep(source) | |
prop_jpeg = OSX::NSDictionary.dictionaryWithObjectsAndKeys( | |
OSX::NSNumber.numberWithFloat(compression_factor), | |
OSX::NSImageCompressionFactor, | |
nil | |
) | |
FileUtils.mkdir_p(temp_folder) unless File.directory?(temp_folder) | |
(0...pdf_rep.pageCount).each do |index| | |
path = File.join(temp_folder, "#{sprintf('%04d', index)}.jpg") | |
pdf_rep.setCurrentPage(index) | |
source.recache | |
size = calc_size(pdf_rep, suggestion_size) | |
destination_rect = OSX::NSMakeRect(0, 0, size.width, size.height) | |
image = OSX::NSImage.alloc.initWithSize(size) | |
image.lockFocus | |
# OSX::NSApplication.sharedApplication | |
OSX::NSGraphicsContext.currentContext.setImageInterpolation(OSX::NSImageInterpolationHigh) | |
OSX::NSColor.whiteColor.set | |
OSX::NSRectFillUsingOperation(destination_rect, OSX::NSCompositeClear) | |
pdf_rep.drawInRect(destination_rect) | |
bitmap = OSX::NSBitmapImageRep.alloc.initWithFocusedViewRect(destination_rect) | |
data = bitmap.representationUsingType_properties(OSX::NSJPEGFileType, prop_jpeg) | |
bitmap.release | |
data.writeToFile_atomically(path, true) | |
image.unlockFocus | |
image.release | |
end | |
source.release | |
Zip::Archive.open("#{output_file}.zip", Zip::CREATE) do |arc| | |
Dir.glob(File.join(temp_folder, '**', '*.jpg')) do |f| | |
arc.add_file(f) | |
end | |
end | |
FileUtils.rm_rf(temp_folder) | |
end | |
end | |
if __FILE__ == $0 | |
require 'optparse' | |
size = 800 | |
compression_factor = 0.7 | |
opt = OptionParser.new | |
opt.on('-s', '--size SIZE') { |v| size = v.to_i } | |
opt.on('-c', '--compression-factor COMPRESSION_FACTOR') { |v| compression_factor = v.to_f } | |
opt.parse! | |
input = File.expand_path(ARGV[0]) | |
output = File.expand_path(ARGV[1]) | |
if File.directory?(input) | |
Dir.glob(File.join(input, '**', '*.pdf')) do |file| | |
output_folder = File.join(output, File.dirname(file[input.bytesize .. -1])) | |
Pdf2Zip.new.pdf2zip(file, output_folder, size, compression_factor) | |
end | |
else | |
Pdf2Zip.new.pdf2zip(input, output, size, compression_factor) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment