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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'bin/pdf2nook', line 52
def execute
clear_tmp
if OS.x?
begin
`gsort --version`
rescue Errno::ENOENT
puts 'It looks like you are on OS X and didn\'t install coreutils!'.red
puts 'Run '.white + 'brew install coreutils'.blue + ' please!'.white
end
end
@degree = (rotate?) ? 90 : 270
filename = File.basename(path)
puts "Loading document…".yellow
pdf = Magick::ImageList.new
pdf.ping(path)
pages_count = pdf.scene
odd_pages_count = pages_count % 10
puts "#{pages_count+1} pages readed!".yellow
decade = 0
progress = ProgressBar.create(
:title => "Rendering…",
:format => '%t |%b>%i| %p%% done',
:starting_at => 0,
:total => (pages_count / 10) + 1,
:length => 60
)
(0..pages_count).step(10){|from|
new_pdf = Magick::ImageList.new()
decade += 1
progress.increment
if(from == pages_count - odd_pages_count)
to = from + odd_pages_count
else
to = from + 10
end
pdf = Magick::ImageList.new("#{path}[#{from}-#{to}]") do
self.density = "167.0x167.0"
self.background_color = "white"
end
pdf.each_with_index{|img, i|
img.opacity = 0
img.background_color = "white"
new_pdf << render_page(img, :top)
new_pdf << render_page(img, :bottom)
}
@tmp_filename = filename.gsub(/\s/,'_')
new_pdf.write("/tmp/pdf2nook/#{@tmp_filename}#{decade}.png") do
self.quality = 100
self.density = "167.0x167.0"
self.compression = Magick::NoCompression
self.colorspace = Magick::GRAYColorspace
self.gravity = Magick::NorthWestGravity
end
new_pdf = nil
pdf = nil
GC.start
}
puts 'Compiling final document…'.yellow
sort_command = (OS.x?) ? 'gsort -V' : 'sort -V'
render = Thread.new do
if(`convert --version`.include? 'OpenMP')
system("find /tmp/pdf2nook/ -name '*.png' | #{sort_command} | xargs convert -limit thread 16 -limit memory 2GB -write '/tmp/pdf2nook/#{filename}'")
else
system("find /tmp/pdf2nook/ -name '*.png' | #{sort_command} | xargs convert -limit memory 2GB -write '/tmp/pdf2nook/#{filename}'")
end
end
render.join
FileUtils.copy_file("/tmp/pdf2nook/#{filename}", "#{File.dirname(path)}/nook_#{filename}")
clear_tmp if cleanup?
puts "Done! Your book located in #{File.dirname(path)}/nook_#{filename}".green
end
|