-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_optimize
81 lines (74 loc) · 2.81 KB
/
pdf_optimize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'optparse'
require 'ostruct'
# Process the script parameters.
options = OpenStruct.new
options.compression_level = 70
options.cover_format = ""
options.cover_page = 0
options.delete_dir = false
options.image_format = "jpeg"
options.resize_pct = 100
options.dimen_threshold = 0
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [-c format] [-f format] [-p cover_page] [-r resize_pct] [-t dimen_threshold] pdf_file [pdf_file...]"
opts.on('-c', '--cover_format [format]', 'Extract cover in the format [bmp|jpeg|jpeg2000|png].') do |cover_format|
options.cover_format = cover_format
end
opts.on('-d', '--delete_dir', 'Extract cover in the format [bmp|jpeg|jpeg2000|png].') do |dd|
options.delete_dir = true
end
opts.on('-f', '--image_format [format]', 'Resize images in the format [bmp|jpeg|jpeg2000|png]. The default is jpeg.') do |image_format|
options.image_format = image_format
end
opts.on('-l', '--compression_level [pct]', 'Compression level %. The default is 70.') do |compression_level|
options.compression_level = compression_level
end
opts.on('-p', '--cover_page [0-9]+', 'Cover page index [0-9]+. The --cover_format option must also be specified. Default is 0.') do |cover_page|
options.cover_page = cover_page
end
opts.on('-r', '--resize_pct [pct]', 'Resize %. The default is 100.') do |resize_pct|
options.resize_pct = resize_pct
end
opts.on('-t', '--dimen_threshold [dimen]', 'Dimension threshold [0-9]+. The default is 0.') do |dimen_threshold|
options.dimen_threshold = dimen_threshold
end
opts.on_tail('-h', '--help', 'Print this help message') do
puts opts
exit 0
end
end
begin
option_parser.parse!(ARGV)
rescue OptionParser::InvalidOption => e
puts "Syntax error: #{e.message}"
puts
puts option_parser.help
exit 1
end
if ARGV.count < 1
puts option_parser.help
exit 0
end
# Determine the root directory of the code base.
script_dir = File.expand_path(File.dirname(__FILE__))
root_dir = File.dirname(script_dir)
require_relative File.join(root_dir, "lib", "pdfutil", "pdfutil")
# Set JAVA_HOME. Explicitly set for my laptop (Windows) or tang (Linux).
if Gem.win_platform?
java_home = File.join(ENV['SYSTEMDRIVE'], "Program Files", "Java", "jdk1.8.0_241")
else
java_home = File.join(File::SEPARATOR, "usr", "lib", "jvm", "java-8-openjdk-amd64")
end
ENV['JAVA_HOME'] = java_home
ENV['PATH'] = File.join(java_home, "bin") + Gem.path_separator + ENV['PATH']
PdfUtil.optimize(
:compression_level => options.compression_level,
:delete_dir => options.delete_dir,
:cover_format => options.cover_format,
:cover_page => options.cover_page,
:resize_pct => options.resize_pct,
:dimen_threshold => options.dimen_threshold,
:pdf_file_list => ARGV
)