Class: PdfParadise::SplitPdf
- Inherits:
-
Base
- Object
- Base
- PdfParadise::SplitPdf
show all
- Defined in:
- lib/pdf_paradise/utility_scripts/split_pdf.rb
Overview
Constant Summary
collapse
- SPLIT_UP_INTO_N_CHUNKS =
#
SPLIT_UP_INTO_N_CHUNKS
Split up the .pdf file at hand into n chunks. This constant used to be set to 10 rather than 2.
#
1
Constants inherited
from Base
Base::NAMESPACE
Instance Method Summary
collapse
Methods inherited from Base
#basename, #be_verbose?, #change_directory, #commandline_arguments?, #copy_file, #delete_file, #e, #ecomment, #esystem, #first_argument?, #gold, #infer_the_namespace, #input_without_leading_hyphens?, #internal_hash?, #is_an_image_file?, #is_on_roebe?, #lightsteelblue, #log_dir?, #mkdir, #mv, #n_pages?, #namespace?, #no_file_at, #opne, #opnn, #orange, #reset_the_internal_hash, #return_commandline_arguments_starting_with_hyphens, #return_files_from_the_commandline_arguments, #return_pwd, #rev, #set_be_quiet, #set_commandline_arguments, #steelblue, #try_to_ensure_that_this_directory_exists, #write_what_into
Constructor Details
#initialize(optional_arguments = nil, run_already = true, &block) ⇒ SplitPdf
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
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 30
def initialize(
optional_arguments = nil,
run_already = true,
&block
)
reset
set_commandline_arguments(
optional_arguments
)
if block_given?
yielded = yield
if yielded.is_a? Hash
if yielded.has_key? :split_into_n_pages
set_n_pages(
yielded.delete(:split_into_n_pages)
)
end
if yielded.has_key? :store_in_this_directory
set_store_here(
yielded.delete(:store_in_this_directory)
)
end
end
end
run if run_already
end
|
Instance Method Details
#array_these_files_were_created_anew? ⇒ Boolean
#
array_these_files_were_created_anew?
#
225
226
227
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 225
def array_these_files_were_created_anew?
@array_these_files_were_created_anew
end
|
#do_convert_this_file_via_hexapdf(i) ⇒ Object
#
do_convert_this_file_via_hexapdf
This can be done via “hexapdf split foobar.pdf”.
The argument to this method should be the existing .pdf file, such as “foobar.pdf”.
#
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 210
def do_convert_this_file_via_hexapdf(i)
cmd_to_use = "hexapdf split #{i}"
e
e(cmd_to_use)
e
`#{cmd_to_use}`
array_new_files = Dir[i.delete_suffix('.pdf')+'*']
@array_these_files_were_created_anew.clear
@array_these_files_were_created_anew << array_new_files
return array_new_files end
|
#ensure_that_the_directory_exists(this_directory = nil) ⇒ Object
#
ensure_that_the_directory_exists
#
108
109
110
111
112
113
114
115
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 108
def ensure_that_the_directory_exists(this_directory = nil)
if this_directory
unless File.directory? this_directory
require 'fileutils'
FileUtils.mkdir_p(this_directory)
end
end
end
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 120
def (
i = @commandline_arguments
)
if i.is_a? Array
i.each {|entry| (entry) }
else
case i
when /^-?-?n(_|-)?pages=(.+)$/i _ = $2.to_s.dup
opnn; e 'We will split the .pdf file into '+sfancy(_.to_s)+' pages each.'
set_split_n_pages(_)
end
end
end
|
#n_chunks? ⇒ Boolean
176
177
178
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 176
def n_chunks?
@split_up_into_chunks_of_n_pages
end
|
#path? ⇒ Boolean
303
304
305
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 303
def path?
@path
end
|
#report_the_output_file_will_be_at(output_file) ⇒ Object
#
report_the_output_file_will_be_at
#
183
184
185
186
187
188
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 183
def report_the_output_file_will_be_at(output_file)
opnn; e 'The output file will be at:'
e
e " #{sfile(output_file)}"
e
end
|
#reset ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 63
def reset
super()
set_split_up_into_chunks_of_n_pages
set_store_in_this_directory
infer_the_namespace
@path = nil
@use_convert_or_hexapdf = :hexapdf @array_these_files_were_created_anew = []
end
|
#return_n_pages(i = input?)
) ⇒ Object
#
return_n_pages
This will return the amount of pages in the given .pdf file.
#
169
170
171
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 169
def return_n_pages(i = input?)
::PdfParadise.n_pages?(i).to_i end
|
#return_name_of_the_output_file_based_on_this_filename(filename, index) ⇒ Object
#
return_name_of_the_output_file_based_on_this_filename
#
193
194
195
196
197
198
199
200
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 193
def return_name_of_the_output_file_based_on_this_filename(
filename, index
)
return @store_in_this_directory+
File.basename(filename).delete_suffix('.pdf')+
'_page'+(index.to_i+1).to_s+
'.pdf'
end
|
#run ⇒ Object
310
311
312
313
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 310
def run
try_to_split_up_the_pdf
end
|
#set_split_up_into_chunks_of_n_pages(i = SPLIT_UP_INTO_N_CHUNKS) ⇒ Object
Also known as:
set_split_n_pages, set_n_pages
#
set_split_up_into_chunks_of_n_pages
#
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 141
def set_split_up_into_chunks_of_n_pages(
i = SPLIT_UP_INTO_N_CHUNKS
)
if i.is_a? Array
i = i.first
end
case i
when :max
i = 1
set_split_n_pages(
PdfParadise.n_pdf_pages?(
return_n_pages - 1
)
)
when nil,
:default
i = SPLIT_UP_INTO_N_CHUNKS
end
i = i.to_i @split_up_into_chunks_of_n_pages = i
end
|
#set_store_in_this_directory(i = return_pwd) ⇒ Object
Also known as:
set_store_here
#
set_store_in_this_directory
By default we will store into the current working directory.
#
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 89
def set_store_in_this_directory(
i = return_pwd
)
case i
when :default
i = return_pwd
end
i = i.dup if i.frozen?
i << '/' unless i.end_with? '/'
@store_in_this_directory = i
ensure_that_the_directory_exists(i)
end
|
#try_to_split_up_the_pdf(i = input_without_leading_hyphens?
) ⇒ Object
#
try_to_split_up_the_pdf
#
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/pdf_paradise/utility_scripts/split_pdf.rb', line 232
def try_to_split_up_the_pdf(
i = input_without_leading_hyphens?
)
i = i.first if i.is_a? Array
filename = i
if File.exist? i
n_pages = return_n_pages(i)
opnn; e "The file at `#{sfile(i)}` exists. We will next split"
opnn; e 'it up into chunks of '+simp(n_chunks?.to_s)+' pages.'
case @use_convert_or_hexapdf
when :convert
cmd_to_use = 'convert -quality 98 '.dup
when :hexapdf
do_convert_this_file_via_hexapdf(i)
exit
end
old_index = 0
n_chunks?.step(n_pages+1, n_chunks?) {|entry|
range = old_index.to_s+'-'+(entry - 1).to_s
_ = filename+'['+range+']'
output_file = return_name_of_the_output_file_based_on_this_filename(filename, old_index+1)
report_the_output_file_will_be_at(output_file)
esystem(
"#{cmd_to_use}#{_} #{output_file}"
)
old_index = entry
}
n_possible_remaining_pages = n_pages % n_chunks?
if n_possible_remaining_pages > 0
range = old_index.to_s+'-'+((old_index+n_possible_remaining_pages)- 1).to_s
_ = filename+'['+range+']'+' '
output_file = @store_in_this_directory+'OUTPUT_PDF_FILE_'+range.tr('-','_')+'_'+File.basename(filename)
@path = output_file
opnn; e 'The output file will be at `'+sfile(output_file)+'`.'
esystem(
cmd_to_use+_+' '+output_file
)
end
else
opnn; no_file_at(i)
end
end
|