pdf_paradise 0.3.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +933 -0
- data/bin/automatic_pdf_title +7 -0
- data/bin/burst_this_pdf_file +7 -0
- data/bin/combine_these_pdf_pages +7 -0
- data/bin/compress_via_hexapdf +7 -0
- data/bin/convert_markdown_to_pdf +7 -0
- data/bin/convert_pdf_to_text +7 -0
- data/bin/delete_first_page_of_this_pdf_file +7 -0
- data/bin/djvu_to_pdf +7 -0
- data/bin/merge_then_open +7 -0
- data/bin/n_pages +10 -0
- data/bin/open_main_pdf +7 -0
- data/bin/pdf_paradise +9 -0
- data/bin/rotate_pdf +7 -0
- data/bin/set_main_book +7 -0
- data/bin/set_title_of_this_pdf_file +15 -0
- data/doc/README.gen +871 -0
- data/doc/todo/todo.md +13 -0
- data/images/Logo_for_the_pdf_paradise_project.avif +0 -0
- data/lib/pdf_paradise/base/base.rb +344 -0
- data/lib/pdf_paradise/base/colours.rb +67 -0
- data/lib/pdf_paradise/colours/colours.rb +27 -0
- data/lib/pdf_paradise/commandline/commandline.rb +109 -0
- data/lib/pdf_paradise/commandline/help.rb +77 -0
- data/lib/pdf_paradise/commandline/menu.rb +173 -0
- data/lib/pdf_paradise/compress/compress_this_pdf_file.rb +108 -0
- data/lib/pdf_paradise/compress/compress_via_hexapdf.rb +27 -0
- data/lib/pdf_paradise/compress/compress_via_qpdf.rb +32 -0
- data/lib/pdf_paradise/constants/constants.rb +76 -0
- data/lib/pdf_paradise/convert_text_to_pdf.rb +94 -0
- data/lib/pdf_paradise/css/project.css +17 -0
- data/lib/pdf_paradise/fpdf/README.md +2 -0
- data/lib/pdf_paradise/fpdf/bookmark.rb +129 -0
- data/lib/pdf_paradise/fpdf/chinese.rb +454 -0
- data/lib/pdf_paradise/fpdf/fpdf.rb +1902 -0
- data/lib/pdf_paradise/fpdf/fpdf_eps.rb +138 -0
- data/lib/pdf_paradise/fpdf/makefont.rb +1794 -0
- data/lib/pdf_paradise/gui/README.md +6 -0
- data/lib/pdf_paradise/gui/fox/split_pdf_file.rb +77 -0
- data/lib/pdf_paradise/gui/gtk2/pdf_viewer/pdf_viewer.rb +34 -0
- data/lib/pdf_paradise/gui/gtk2/split_pdf_file/split_pdf_file.rb +34 -0
- data/lib/pdf_paradise/gui/gtk2/statistics_widget/statistics_widget.rb +34 -0
- data/lib/pdf_paradise/gui/gtk3/controller/controller.rb +214 -0
- data/lib/pdf_paradise/gui/gtk3/pdf_viewer/pdf_viewer.rb +34 -0
- data/lib/pdf_paradise/gui/gtk3/split_pdf_file/split_pdf_file.rb +34 -0
- data/lib/pdf_paradise/gui/jruby/delete_the_first_or_the_last_page_of_this_pdf_file/delete_the_first_or_the_last_page_of_this_pdf_file.rb +167 -0
- data/lib/pdf_paradise/gui/jruby/remove_the_first_page_of_this_pdf_file/remove_the_first_page_of_this_pdf_file.rb +103 -0
- data/lib/pdf_paradise/gui/libui/extract_all_images_from_this_pdf_file/extract_all_images_from_this_pdf_file.rb +223 -0
- data/lib/pdf_paradise/gui/libui/remove_the_first_page_of_this_pdf_file/remove_the_first_page_of_this_pdf_file.rb +267 -0
- data/lib/pdf_paradise/gui/libui/rotate_pdf_file/rotate_pdf_file.rb +219 -0
- data/lib/pdf_paradise/gui/libui/statistics_widget/statistics_widget.rb +233 -0
- data/lib/pdf_paradise/gui/shared_code/pdf_viewer/pdf_viewer.css +5 -0
- data/lib/pdf_paradise/gui/shared_code/pdf_viewer/pdf_viewer_module.rb +287 -0
- data/lib/pdf_paradise/gui/shared_code/remove_the_first_page_of_this_pdf_file_module/remove_the_first_page_of_this_pdf_file_module.rb +31 -0
- data/lib/pdf_paradise/gui/shared_code/split_pdf_file/split_pdf_file_module.rb +295 -0
- data/lib/pdf_paradise/gui/universal_widgets/convert_pdf_to_text/convert_pdf_to_text.rb +366 -0
- data/lib/pdf_paradise/gui/universal_widgets/delete_the_first_or_the_last_page_of_this_pdf_file/delete_the_first_or_the_last_page_of_this_pdf_file.rb +776 -0
- data/lib/pdf_paradise/gui/universal_widgets/statistics_widget/statistics_widget.rb +407 -0
- data/lib/pdf_paradise/gui/universal_widgets/to_pdf/to_pdf.rb +351 -0
- data/lib/pdf_paradise/hexapdf/001_rainbow_pattern_example.rb +0 -0
- data/lib/pdf_paradise/hexapdf/hexapdf.rb +123 -0
- data/lib/pdf_paradise/images/PDF_PARADISE_LOGO.png +0 -0
- data/lib/pdf_paradise/main_pdf/main_pdf.rb +474 -0
- data/lib/pdf_paradise/merge_pdf/menu.rb +63 -0
- data/lib/pdf_paradise/merge_pdf/merge_pdf.rb +307 -0
- data/lib/pdf_paradise/merge_pdf_namespace.rb +9 -0
- data/lib/pdf_paradise/merge_then_open/merge_then_open.rb +105 -0
- data/lib/pdf_paradise/prawn_addons/README.md +2 -0
- data/lib/pdf_paradise/prawn_addons/prawn_addons.rb +17 -0
- data/lib/pdf_paradise/project/project.rb +22 -0
- data/lib/pdf_paradise/remove_pdf_password.rb +391 -0
- data/lib/pdf_paradise/requires/batch_require_toplevel_files.rb +22 -0
- data/lib/pdf_paradise/requires/colours.rb +11 -0
- data/lib/pdf_paradise/requires/colours_and_esystem_and_save_file_and_fileutils_and_opn.rb +13 -0
- data/lib/pdf_paradise/requires/esystem_and_colours.rb +11 -0
- data/lib/pdf_paradise/requires/esystem_and_opn_and_colours.rb +10 -0
- data/lib/pdf_paradise/requires/require_the_whole_project.rb +30 -0
- data/lib/pdf_paradise/requires/require_utility_scripts.rb +9 -0
- data/lib/pdf_paradise/set_main_book.rb +156 -0
- data/lib/pdf_paradise/set_pdf_title.rb +220 -0
- data/lib/pdf_paradise/sinatra/embeddable_interface.rb +389 -0
- data/lib/pdf_paradise/toplevel_methods/convert_epub_to_pdf.rb +27 -0
- data/lib/pdf_paradise/toplevel_methods/convert_markdown_to_pdf.rb +45 -0
- data/lib/pdf_paradise/toplevel_methods/convert_ppt_to_pdf.rb +35 -0
- data/lib/pdf_paradise/toplevel_methods/e.rb +16 -0
- data/lib/pdf_paradise/toplevel_methods/esystem.rb +20 -0
- data/lib/pdf_paradise/toplevel_methods/misc.rb +228 -0
- data/lib/pdf_paradise/toplevel_methods/number_pages.rb +38 -0
- data/lib/pdf_paradise/toplevel_methods/opened_pdf_files.rb +221 -0
- data/lib/pdf_paradise/toplevel_methods/query_pdf_title.rb +201 -0
- data/lib/pdf_paradise/toplevel_methods/reduce_size_of_this_pdf_file.rb +46 -0
- data/lib/pdf_paradise/toplevel_methods/roebe.rb +17 -0
- data/lib/pdf_paradise/toplevel_methods/to_pdf.rb +12 -0
- data/lib/pdf_paradise/utility_scripts/README.md +3 -0
- data/lib/pdf_paradise/utility_scripts/automatic_pdf_title.rb +104 -0
- data/lib/pdf_paradise/utility_scripts/check_syntax_of_pdf_files.rb +106 -0
- data/lib/pdf_paradise/utility_scripts/combine_these_pdf_pages.rb +118 -0
- data/lib/pdf_paradise/utility_scripts/convert_pdf_to_text.rb +179 -0
- data/lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb +180 -0
- data/lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb +429 -0
- data/lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb +356 -0
- data/lib/pdf_paradise/utility_scripts/djvu_to_pdf.rb +87 -0
- data/lib/pdf_paradise/utility_scripts/extract_all_images_from_this_pdf_file.rb +129 -0
- data/lib/pdf_paradise/utility_scripts/extract_pdf_page.rb +283 -0
- data/lib/pdf_paradise/utility_scripts/pdf_file_n_total_pages.rb +348 -0
- data/lib/pdf_paradise/utility_scripts/pdf_optimizer.rb +111 -0
- data/lib/pdf_paradise/utility_scripts/pdf_statistics.rb +148 -0
- data/lib/pdf_paradise/utility_scripts/pdf_to_html.rb +75 -0
- data/lib/pdf_paradise/utility_scripts/remove_images.rb +110 -0
- data/lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb +303 -0
- data/lib/pdf_paradise/utility_scripts/split_pdf.rb +364 -0
- data/lib/pdf_paradise/utility_scripts/to_pdf.rb +130 -0
- data/lib/pdf_paradise/utility_scripts/to_qdf.rb +66 -0
- data/lib/pdf_paradise/version/version.rb +19 -0
- data/lib/pdf_paradise/www/README.md +2 -0
- data/lib/pdf_paradise/www/sinatra/app.rb +304 -0
- data/lib/pdf_paradise/yaml/working_on_these_pdf_files.yml +4 -0
- data/lib/pdf_paradise.rb +5 -0
- data/pdf_paradise.gemspec +61 -0
- data/test/fpdf/001_minimal_example.rb +12 -0
- data/test/fpdf/002.pdf +0 -0
- data/test/fpdf/002_header_and_footer_example.rb +64 -0
- data/test/fpdf/003.pdf +98 -0
- data/test/fpdf/003_justified_paragraphs.rb +96 -0
- data/test/fpdf/file1.md +3 -0
- data/test/fpdf/file2.md +3 -0
- data/test/fpdf/test.pdf +0 -0
- data/test/testing_pdf_paradise.rb +12 -0
- metadata +239 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
#!/usr/bin/ruby -w
|
|
2
|
+
# Encoding: UTF-8
|
|
3
|
+
# frozen_string_literal: true
|
|
4
|
+
# =========================================================================== #
|
|
5
|
+
# === PdfParadise::RotatePdfFile
|
|
6
|
+
#
|
|
7
|
+
# This class can be used to rotate a .pdf file. By default this will rotate
|
|
8
|
+
# it by 90%.
|
|
9
|
+
#
|
|
10
|
+
# Usage example:
|
|
11
|
+
#
|
|
12
|
+
# PdfParadise::RotatePdfFile.new(ARGV)
|
|
13
|
+
#
|
|
14
|
+
# =========================================================================== #
|
|
15
|
+
# require 'pdf_paradise/utility_scripts/rotate_pdf_file.rb'
|
|
16
|
+
# PdfParadise::RotatePdfFile.new(ARGV)
|
|
17
|
+
# =========================================================================== #
|
|
18
|
+
require 'pdf_paradise/base/base.rb'
|
|
19
|
+
|
|
20
|
+
module PdfParadise
|
|
21
|
+
|
|
22
|
+
class RotatePdfFile < Base # === PdfParadise::RotatePdfFile
|
|
23
|
+
|
|
24
|
+
# ========================================================================= #
|
|
25
|
+
# === ROTATE_BY_N_DEGREES
|
|
26
|
+
# ========================================================================= #
|
|
27
|
+
ROTATE_BY_N_DEGREES = 90
|
|
28
|
+
|
|
29
|
+
# ========================================================================= #
|
|
30
|
+
# === ROTATE_REGEX
|
|
31
|
+
# ========================================================================= #
|
|
32
|
+
ROTATE_REGEX = /(--rotate (\d+))/ # URL: https://rubular.com/r/ds3tk7DPPw
|
|
33
|
+
|
|
34
|
+
# ========================================================================= #
|
|
35
|
+
# === initialize
|
|
36
|
+
# ========================================================================= #
|
|
37
|
+
def initialize(
|
|
38
|
+
i = nil,
|
|
39
|
+
run_already = true
|
|
40
|
+
)
|
|
41
|
+
reset
|
|
42
|
+
case i
|
|
43
|
+
# ======================================================================= #
|
|
44
|
+
# === :do_not_run_yet
|
|
45
|
+
# ======================================================================= #
|
|
46
|
+
when :do_not_run_yet
|
|
47
|
+
i = nil
|
|
48
|
+
run_already = false
|
|
49
|
+
end
|
|
50
|
+
set_commandline_arguments(i)
|
|
51
|
+
set_input(
|
|
52
|
+
i.reject {|entry|
|
|
53
|
+
entry.start_with?('--')
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
case run_already
|
|
57
|
+
# ======================================================================= #
|
|
58
|
+
# === :run_already
|
|
59
|
+
# ======================================================================= #
|
|
60
|
+
when :run_already
|
|
61
|
+
run_already = true
|
|
62
|
+
# ======================================================================= #
|
|
63
|
+
# === :do_not_run_yet
|
|
64
|
+
# ======================================================================= #
|
|
65
|
+
when :do_not_run_yet
|
|
66
|
+
run_already = false
|
|
67
|
+
end
|
|
68
|
+
run if run_already
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# ========================================================================= #
|
|
72
|
+
# === reset (reset tag)
|
|
73
|
+
# ========================================================================= #
|
|
74
|
+
def reset
|
|
75
|
+
infer_the_namespace
|
|
76
|
+
# ======================================================================= #
|
|
77
|
+
# === @use_this_command
|
|
78
|
+
# ======================================================================= #
|
|
79
|
+
@use_this_command = :qpdf # :convert
|
|
80
|
+
set_rotate # Set the default value, after @use_this_command was set.
|
|
81
|
+
# ======================================================================= #
|
|
82
|
+
# === @overwrite_the_original_file
|
|
83
|
+
#
|
|
84
|
+
# Other users may want to set the next variable to false.
|
|
85
|
+
# ======================================================================= #
|
|
86
|
+
@overwrite_the_original_file = true
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# ========================================================================= #
|
|
90
|
+
# === qpdf_was_used?
|
|
91
|
+
# ========================================================================= #
|
|
92
|
+
def qpdf_was_used?
|
|
93
|
+
@use_this_command == :qpdf
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# ========================================================================= #
|
|
97
|
+
# === show_help (help tag)
|
|
98
|
+
# ========================================================================= #
|
|
99
|
+
def show_help
|
|
100
|
+
opne 'The available options are:'
|
|
101
|
+
e
|
|
102
|
+
eparse ' --rotate N # rotate by n degrees (into '\
|
|
103
|
+
'clockwise direction. Example: rotatepdf --rotate 90 foo.pdf)'
|
|
104
|
+
e
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# ========================================================================= #
|
|
108
|
+
# === set_input
|
|
109
|
+
# ========================================================================= #
|
|
110
|
+
def set_input(i = '')
|
|
111
|
+
i = i.join(' ').strip if i.is_a? Array
|
|
112
|
+
i = i.to_s.dup
|
|
113
|
+
if i.start_with? 'file://'
|
|
114
|
+
i.sub!(/^file:\/\//,'')
|
|
115
|
+
end
|
|
116
|
+
case i
|
|
117
|
+
# ======================================================================= #
|
|
118
|
+
# We add support for --rotate next. When there is a match, we
|
|
119
|
+
# will get rid of that part of the input.
|
|
120
|
+
# ======================================================================= #
|
|
121
|
+
when ROTATE_REGEX, /--rotate/ # Usage: rotate --rotate 100 5-Kapitel_5_AllgMikrobiologie_SS2015_students_2S.pdf
|
|
122
|
+
i =~ ROTATE_REGEX
|
|
123
|
+
set_rotate($2.to_s.dup)
|
|
124
|
+
i.gsub!(/#{$1.to_s}/, '')
|
|
125
|
+
end unless File.exist? i
|
|
126
|
+
i = i.to_s.strip
|
|
127
|
+
@input = i
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# ========================================================================= #
|
|
131
|
+
# === set_rotate
|
|
132
|
+
# ========================================================================= #
|
|
133
|
+
def set_rotate(
|
|
134
|
+
i = ROTATE_BY_N_DEGREES
|
|
135
|
+
)
|
|
136
|
+
i = i.to_i
|
|
137
|
+
case @use_this_command
|
|
138
|
+
# ======================================================================= #
|
|
139
|
+
# === :qpdf
|
|
140
|
+
# ======================================================================= #
|
|
141
|
+
when :qpdf
|
|
142
|
+
i = "+#{i}" if i > 0
|
|
143
|
+
end
|
|
144
|
+
@rotate_by_n_degrees = i.to_s.dup # Always .dup it, just in case.
|
|
145
|
+
end; alias set_rotate_level set_rotate # === set_rotate_level
|
|
146
|
+
|
|
147
|
+
# ========================================================================= #
|
|
148
|
+
# === input?
|
|
149
|
+
# ========================================================================= #
|
|
150
|
+
def input?
|
|
151
|
+
@input
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# ========================================================================= #
|
|
155
|
+
# === output_file?
|
|
156
|
+
# ========================================================================= #
|
|
157
|
+
def output_file?(
|
|
158
|
+
i = input?
|
|
159
|
+
)
|
|
160
|
+
return i if @overwrite_the_original_file # Early return.
|
|
161
|
+
_ = basename(i).delete_suffix('.pdf')
|
|
162
|
+
_.tr(' ','') if _.include? ' '
|
|
163
|
+
return "rotated_pdf_file_#{_}.pdf"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# ========================================================================= #
|
|
167
|
+
# === opnn
|
|
168
|
+
# ========================================================================= #
|
|
169
|
+
def opnn
|
|
170
|
+
Opn.opn(namespace: namespace?)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# ========================================================================= #
|
|
174
|
+
# === rotate_by_n_degrees?
|
|
175
|
+
# ========================================================================= #
|
|
176
|
+
def rotate_by_n_degrees?
|
|
177
|
+
@rotate_by_n_degrees
|
|
178
|
+
end; alias rotate? rotate_by_n_degrees? # === rotate?
|
|
179
|
+
|
|
180
|
+
# ========================================================================= #
|
|
181
|
+
# === add_quality
|
|
182
|
+
# ========================================================================= #
|
|
183
|
+
def add_quality
|
|
184
|
+
return '-quality 100 '
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# ========================================================================= #
|
|
188
|
+
# === notify_the_user_that_the_conversion_has_finished
|
|
189
|
+
# ========================================================================= #
|
|
190
|
+
def notify_the_user_that_the_conversion_has_finished
|
|
191
|
+
e 'Finished converting! The file should now reside at:'
|
|
192
|
+
e
|
|
193
|
+
e sfile(" #{output_file?}")
|
|
194
|
+
e
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# ========================================================================= #
|
|
198
|
+
# === menu
|
|
199
|
+
# ========================================================================= #
|
|
200
|
+
def menu(
|
|
201
|
+
i = @commandline_arguments
|
|
202
|
+
)
|
|
203
|
+
if i.is_a? Array
|
|
204
|
+
i.each {|entry|
|
|
205
|
+
menu(entry)
|
|
206
|
+
}
|
|
207
|
+
else
|
|
208
|
+
case i
|
|
209
|
+
# ===================================================================== #
|
|
210
|
+
# === --help
|
|
211
|
+
#
|
|
212
|
+
# Since as of February 2023, empty input, aka '', will no longer call
|
|
213
|
+
# this method.
|
|
214
|
+
# ===================================================================== #
|
|
215
|
+
when /-?-?help$/i # '' means empty.
|
|
216
|
+
show_help
|
|
217
|
+
exit
|
|
218
|
+
# ===================================================================== #
|
|
219
|
+
# === --rotate=5
|
|
220
|
+
#
|
|
221
|
+
# Usage example:
|
|
222
|
+
#
|
|
223
|
+
# rpdf 13.06.1991_Scan_EVVA_Schließplan_für_Zylinder_Schließanlagen.pdf --rotate=1
|
|
224
|
+
#
|
|
225
|
+
# ===================================================================== #
|
|
226
|
+
when /-?-?rotate=(\d{1,5})$/i
|
|
227
|
+
set_rotate_level($1.to_s)
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# ========================================================================= #
|
|
233
|
+
# === do_convert_this_pdf_file
|
|
234
|
+
# ========================================================================= #
|
|
235
|
+
def do_convert_this_pdf_file(
|
|
236
|
+
i, be_verbose = true
|
|
237
|
+
)
|
|
238
|
+
i = i.to_s
|
|
239
|
+
output_file = output_file?
|
|
240
|
+
# ======================================================================= #
|
|
241
|
+
# Use convert by default.
|
|
242
|
+
# ======================================================================= #
|
|
243
|
+
_ = @use_this_command.to_s
|
|
244
|
+
_ = _.dup
|
|
245
|
+
if _.start_with? 'qpdf'
|
|
246
|
+
_ << ' --rotate='+rotate_by_n_degrees?+' '
|
|
247
|
+
if @overwrite_the_original_file
|
|
248
|
+
# =================================================================== #
|
|
249
|
+
# --replace-input may be given in place of outfile.
|
|
250
|
+
# =================================================================== #
|
|
251
|
+
_ << ' --replace-input'
|
|
252
|
+
end
|
|
253
|
+
else
|
|
254
|
+
_ << ' -rotate '+rotate_by_n_degrees?+' '
|
|
255
|
+
end
|
|
256
|
+
_ << add_quality if _.start_with? 'convert'
|
|
257
|
+
_ << " #{i}" # Here we add the input-file.
|
|
258
|
+
unless _.include? '--replace-input'
|
|
259
|
+
_ << " #{output_file}" # Add the output file here.
|
|
260
|
+
end
|
|
261
|
+
_.rstrip!
|
|
262
|
+
_.squeeze!(' ')
|
|
263
|
+
case be_verbose
|
|
264
|
+
# ======================================================================= #
|
|
265
|
+
# === :be_quiet
|
|
266
|
+
# ======================================================================= #
|
|
267
|
+
when :be_quiet
|
|
268
|
+
be_verbose = false
|
|
269
|
+
end
|
|
270
|
+
if be_verbose
|
|
271
|
+
esystem _
|
|
272
|
+
else
|
|
273
|
+
system _
|
|
274
|
+
end
|
|
275
|
+
# ======================================================================= #
|
|
276
|
+
# Do cleanup if qpdf was used.
|
|
277
|
+
# ======================================================================= #
|
|
278
|
+
if _.include?('--replace-input') and qpdf_was_used?
|
|
279
|
+
delete_these_files = Dir['*.pdf.~qpdf-orig']
|
|
280
|
+
delete_these_files.each {|this_file|
|
|
281
|
+
delete_this_file(this_file)
|
|
282
|
+
}
|
|
283
|
+
end
|
|
284
|
+
if File.exist?(output_file) and is_on_roebe? and be_verbose
|
|
285
|
+
require 'open'
|
|
286
|
+
Open[output_file] # Open the .pdf file here on a roebe system.
|
|
287
|
+
end
|
|
288
|
+
end; alias do_rotate_the_pdf_file do_convert_this_pdf_file # === do_rotate_the_pdf_file
|
|
289
|
+
|
|
290
|
+
# ========================================================================= #
|
|
291
|
+
# === run (run tag)
|
|
292
|
+
# ========================================================================= #
|
|
293
|
+
def run
|
|
294
|
+
menu
|
|
295
|
+
do_convert_this_pdf_file(input?)
|
|
296
|
+
notify_the_user_that_the_conversion_has_finished
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
end; end
|
|
300
|
+
|
|
301
|
+
if __FILE__ == $PROGRAM_NAME
|
|
302
|
+
PdfParadise::RotatePdfFile.new(ARGV)
|
|
303
|
+
end # rotatepdf
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
#!/usr/bin/ruby -w
|
|
2
|
+
# Encoding: UTF-8
|
|
3
|
+
# frozen_string_literal: true
|
|
4
|
+
# =========================================================================== #
|
|
5
|
+
# === PdfParadise::SplitPdf
|
|
6
|
+
#
|
|
7
|
+
# Use this class to split up a .pdf file into several new documents.
|
|
8
|
+
# =========================================================================== #
|
|
9
|
+
# require 'pdf_paradise/utility_scripts/split_pdf.rb'
|
|
10
|
+
# =========================================================================== #
|
|
11
|
+
require 'pdf_paradise/base/base.rb'
|
|
12
|
+
|
|
13
|
+
module PdfParadise
|
|
14
|
+
|
|
15
|
+
class SplitPdf < PdfParadise::Base # === PdfParadise::SplitPdf
|
|
16
|
+
|
|
17
|
+
require 'pdf_paradise/utility_scripts/pdf_file_n_total_pages.rb'
|
|
18
|
+
|
|
19
|
+
# ========================================================================= #
|
|
20
|
+
# === SPLIT_UP_INTO_N_CHUNKS
|
|
21
|
+
#
|
|
22
|
+
# Split up the .pdf file at hand into n chunks. This constant used to
|
|
23
|
+
# be set to 10 rather than 2.
|
|
24
|
+
# ========================================================================= #
|
|
25
|
+
SPLIT_UP_INTO_N_CHUNKS = 1 # 10
|
|
26
|
+
|
|
27
|
+
# ========================================================================= #
|
|
28
|
+
# === initialize
|
|
29
|
+
# ========================================================================= #
|
|
30
|
+
def initialize(
|
|
31
|
+
optional_arguments = nil,
|
|
32
|
+
run_already = true,
|
|
33
|
+
&block
|
|
34
|
+
)
|
|
35
|
+
reset
|
|
36
|
+
set_commandline_arguments(
|
|
37
|
+
optional_arguments
|
|
38
|
+
)
|
|
39
|
+
# ======================================================================= #
|
|
40
|
+
# === Hande blocks passed to this method next
|
|
41
|
+
# ======================================================================= #
|
|
42
|
+
if block_given?
|
|
43
|
+
yielded = yield
|
|
44
|
+
if yielded.is_a? Hash
|
|
45
|
+
if yielded.has_key? :split_into_n_pages
|
|
46
|
+
set_n_pages(
|
|
47
|
+
yielded.delete(:split_into_n_pages)
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
if yielded.has_key? :store_in_this_directory
|
|
51
|
+
set_store_here(
|
|
52
|
+
yielded.delete(:store_in_this_directory)
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
run if run_already
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# ========================================================================= #
|
|
61
|
+
# === reset
|
|
62
|
+
# ========================================================================= #
|
|
63
|
+
def reset
|
|
64
|
+
super()
|
|
65
|
+
set_split_up_into_chunks_of_n_pages
|
|
66
|
+
set_store_in_this_directory
|
|
67
|
+
infer_the_namespace
|
|
68
|
+
# ======================================================================= #
|
|
69
|
+
# === @path
|
|
70
|
+
# ======================================================================= #
|
|
71
|
+
@path = nil
|
|
72
|
+
# ======================================================================= #
|
|
73
|
+
# === @use_convert_or_hexapdf
|
|
74
|
+
#
|
|
75
|
+
# Since as of February 2023 the default is now hexapdf.
|
|
76
|
+
# ======================================================================= #
|
|
77
|
+
@use_convert_or_hexapdf = :hexapdf # or :convert
|
|
78
|
+
# ======================================================================= #
|
|
79
|
+
# === @array_these_files_were_created_anew
|
|
80
|
+
# ======================================================================= #
|
|
81
|
+
@array_these_files_were_created_anew = []
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# ========================================================================= #
|
|
85
|
+
# === set_store_in_this_directory
|
|
86
|
+
#
|
|
87
|
+
# By default we will store into the current working directory.
|
|
88
|
+
# ========================================================================= #
|
|
89
|
+
def set_store_in_this_directory(
|
|
90
|
+
i = return_pwd
|
|
91
|
+
)
|
|
92
|
+
case i
|
|
93
|
+
when :default
|
|
94
|
+
i = return_pwd
|
|
95
|
+
end
|
|
96
|
+
i = i.dup if i.frozen?
|
|
97
|
+
# ======================================================================= #
|
|
98
|
+
# We require a String.
|
|
99
|
+
# ======================================================================= #
|
|
100
|
+
i << '/' unless i.end_with? '/'
|
|
101
|
+
@store_in_this_directory = i
|
|
102
|
+
ensure_that_the_directory_exists(i)
|
|
103
|
+
end; alias set_store_here set_store_in_this_directory # === set_store_here
|
|
104
|
+
|
|
105
|
+
# ========================================================================= #
|
|
106
|
+
# === ensure_that_the_directory_exists
|
|
107
|
+
# ========================================================================= #
|
|
108
|
+
def ensure_that_the_directory_exists(this_directory = nil)
|
|
109
|
+
if this_directory
|
|
110
|
+
unless File.directory? this_directory
|
|
111
|
+
require 'fileutils'
|
|
112
|
+
FileUtils.mkdir_p(this_directory)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# ========================================================================= #
|
|
118
|
+
# === menu (menu tag)
|
|
119
|
+
# ========================================================================= #
|
|
120
|
+
def menu(
|
|
121
|
+
i = @commandline_arguments
|
|
122
|
+
)
|
|
123
|
+
if i.is_a? Array
|
|
124
|
+
i.each {|entry| menu(entry) }
|
|
125
|
+
else
|
|
126
|
+
case i
|
|
127
|
+
# ===================================================================== #
|
|
128
|
+
# === splitpdf --n-pages=1
|
|
129
|
+
# ===================================================================== #
|
|
130
|
+
when /^-?-?n(_|-)?pages=(.+)$/i # $2
|
|
131
|
+
_ = $2.to_s.dup
|
|
132
|
+
opnn; e 'We will split the .pdf file into '+sfancy(_.to_s)+' pages each.'
|
|
133
|
+
set_split_n_pages(_)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# ========================================================================= #
|
|
139
|
+
# === set_split_up_into_chunks_of_n_pages
|
|
140
|
+
# ========================================================================= #
|
|
141
|
+
def set_split_up_into_chunks_of_n_pages(
|
|
142
|
+
i = SPLIT_UP_INTO_N_CHUNKS
|
|
143
|
+
)
|
|
144
|
+
if i.is_a? Array
|
|
145
|
+
i = i.first
|
|
146
|
+
end
|
|
147
|
+
case i
|
|
148
|
+
when :max
|
|
149
|
+
i = 1
|
|
150
|
+
set_split_n_pages(
|
|
151
|
+
PdfParadise.n_pdf_pages?(
|
|
152
|
+
return_n_pages - 1
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
when nil,
|
|
156
|
+
:default
|
|
157
|
+
i = SPLIT_UP_INTO_N_CHUNKS
|
|
158
|
+
end
|
|
159
|
+
i = i.to_i # We need to keep this variable as Integer value.
|
|
160
|
+
@split_up_into_chunks_of_n_pages = i
|
|
161
|
+
end; alias set_split_n_pages set_split_up_into_chunks_of_n_pages # === set_split_n_pages
|
|
162
|
+
alias set_n_pages set_split_up_into_chunks_of_n_pages # === set_n_pages
|
|
163
|
+
|
|
164
|
+
# ========================================================================= #
|
|
165
|
+
# === return_n_pages
|
|
166
|
+
#
|
|
167
|
+
# This will return the amount of pages in the given .pdf file.
|
|
168
|
+
# ========================================================================= #
|
|
169
|
+
def return_n_pages(i = input?)
|
|
170
|
+
::PdfParadise.n_pages?(i).to_i # We need an integer here.
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# ========================================================================= #
|
|
174
|
+
# === n_chunks?
|
|
175
|
+
# ========================================================================= #
|
|
176
|
+
def n_chunks?
|
|
177
|
+
@split_up_into_chunks_of_n_pages
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# ========================================================================= #
|
|
181
|
+
# === report_the_output_file_will_be_at
|
|
182
|
+
# ========================================================================= #
|
|
183
|
+
def report_the_output_file_will_be_at(output_file)
|
|
184
|
+
opnn; e 'The output file will be at:'
|
|
185
|
+
e
|
|
186
|
+
e " #{sfile(output_file)}"
|
|
187
|
+
e
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# ========================================================================= #
|
|
191
|
+
# === return_name_of_the_output_file_based_on_this_filename
|
|
192
|
+
# ========================================================================= #
|
|
193
|
+
def return_name_of_the_output_file_based_on_this_filename(
|
|
194
|
+
filename, index
|
|
195
|
+
)
|
|
196
|
+
return @store_in_this_directory+
|
|
197
|
+
File.basename(filename).delete_suffix('.pdf')+
|
|
198
|
+
'_page'+(index.to_i+1).to_s+
|
|
199
|
+
'.pdf'
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# ========================================================================= #
|
|
203
|
+
# === do_convert_this_file_via_hexapdf
|
|
204
|
+
#
|
|
205
|
+
# This can be done via "hexapdf split foobar.pdf".
|
|
206
|
+
#
|
|
207
|
+
# The argument to this method should be the existing .pdf file, such
|
|
208
|
+
# as "foobar.pdf".
|
|
209
|
+
# ========================================================================= #
|
|
210
|
+
def do_convert_this_file_via_hexapdf(i)
|
|
211
|
+
cmd_to_use = "hexapdf split #{i}"
|
|
212
|
+
e
|
|
213
|
+
e(cmd_to_use)
|
|
214
|
+
e
|
|
215
|
+
`#{cmd_to_use}`
|
|
216
|
+
array_new_files = Dir[i.delete_suffix('.pdf')+'*']
|
|
217
|
+
@array_these_files_were_created_anew.clear
|
|
218
|
+
@array_these_files_were_created_anew << array_new_files
|
|
219
|
+
return array_new_files # Perhaps we may need this at a later time.
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# ========================================================================= #
|
|
223
|
+
# === array_these_files_were_created_anew?
|
|
224
|
+
# ========================================================================= #
|
|
225
|
+
def array_these_files_were_created_anew?
|
|
226
|
+
@array_these_files_were_created_anew
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# ========================================================================= #
|
|
230
|
+
# === try_to_split_up_the_pdf
|
|
231
|
+
# ========================================================================= #
|
|
232
|
+
def try_to_split_up_the_pdf(
|
|
233
|
+
i = input_without_leading_hyphens?
|
|
234
|
+
)
|
|
235
|
+
i = i.first if i.is_a? Array
|
|
236
|
+
filename = i
|
|
237
|
+
if File.exist? i
|
|
238
|
+
n_pages = return_n_pages(i)
|
|
239
|
+
opnn; e "The file at `#{sfile(i)}` exists. We will next split"
|
|
240
|
+
opnn; e 'it up into chunks of '+simp(n_chunks?.to_s)+' pages.'
|
|
241
|
+
# ===================================================================== #
|
|
242
|
+
# We currently depend on "bin/convert" from ImageMagick. We
|
|
243
|
+
# will also try to preserve as much quality as possible.
|
|
244
|
+
# Unfortunately convert really has a lousy quality, so as
|
|
245
|
+
# of May 2022 we add hexapdf as option too.
|
|
246
|
+
# ===================================================================== #
|
|
247
|
+
# cmd_to_use = 'convert '.dup
|
|
248
|
+
case @use_convert_or_hexapdf
|
|
249
|
+
# ===================================================================== #
|
|
250
|
+
# === :convert
|
|
251
|
+
# ===================================================================== #
|
|
252
|
+
when :convert
|
|
253
|
+
cmd_to_use = 'convert -quality 98 '.dup
|
|
254
|
+
# ===================================================================== #
|
|
255
|
+
# === :hexapdf
|
|
256
|
+
# ===================================================================== #
|
|
257
|
+
when :hexapdf
|
|
258
|
+
do_convert_this_file_via_hexapdf(i)
|
|
259
|
+
exit
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
old_index = 0
|
|
263
|
+
n_chunks?.step(n_pages+1, n_chunks?) {|entry|
|
|
264
|
+
range = old_index.to_s+'-'+(entry - 1).to_s
|
|
265
|
+
_ = filename+'['+range+']'
|
|
266
|
+
# =================================================================== #
|
|
267
|
+
# Keep track of the name of the output file at hand.
|
|
268
|
+
# =================================================================== #
|
|
269
|
+
output_file = return_name_of_the_output_file_based_on_this_filename(filename, old_index+1)
|
|
270
|
+
report_the_output_file_will_be_at(output_file)
|
|
271
|
+
esystem(
|
|
272
|
+
"#{cmd_to_use}#{_} #{output_file}"
|
|
273
|
+
)
|
|
274
|
+
old_index = entry
|
|
275
|
+
}
|
|
276
|
+
# ===================================================================== #
|
|
277
|
+
# We may have to correct the command a bit to account for the
|
|
278
|
+
# very last .pdf pages.
|
|
279
|
+
# ===================================================================== #
|
|
280
|
+
n_possible_remaining_pages = n_pages % n_chunks?
|
|
281
|
+
if n_possible_remaining_pages > 0
|
|
282
|
+
# =================================================================== #
|
|
283
|
+
# Ok, now we know that we must do a modification because we know
|
|
284
|
+
# that there are n remaining pages left.
|
|
285
|
+
# =================================================================== #
|
|
286
|
+
range = old_index.to_s+'-'+((old_index+n_possible_remaining_pages)- 1).to_s
|
|
287
|
+
_ = filename+'['+range+']'+' '
|
|
288
|
+
output_file = @store_in_this_directory+'OUTPUT_PDF_FILE_'+range.tr('-','_')+'_'+File.basename(filename)
|
|
289
|
+
@path = output_file
|
|
290
|
+
opnn; e 'The output file will be at `'+sfile(output_file)+'`.'
|
|
291
|
+
esystem(
|
|
292
|
+
cmd_to_use+_+' '+output_file
|
|
293
|
+
)
|
|
294
|
+
end
|
|
295
|
+
else
|
|
296
|
+
opnn; no_file_at(i)
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# ========================================================================= #
|
|
301
|
+
# === path?
|
|
302
|
+
# ========================================================================= #
|
|
303
|
+
def path?
|
|
304
|
+
@path
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# ========================================================================= #
|
|
308
|
+
# === run
|
|
309
|
+
# ========================================================================= #
|
|
310
|
+
def run
|
|
311
|
+
menu
|
|
312
|
+
try_to_split_up_the_pdf
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# =========================================================================== #
|
|
318
|
+
# === PdfParadise.split_this_pdf_file
|
|
319
|
+
#
|
|
320
|
+
# The first argument to this method should be the path to a locally
|
|
321
|
+
# existing .pdf file.
|
|
322
|
+
# =========================================================================== #
|
|
323
|
+
def self.split_this_pdf_file(
|
|
324
|
+
i = ARGV,
|
|
325
|
+
split_into_n_pages = 1,
|
|
326
|
+
store_in_this_directory = :default
|
|
327
|
+
)
|
|
328
|
+
_ = PdfParadise::SplitPdf.new(i) {{
|
|
329
|
+
split_into_n_pages: split_into_n_pages,
|
|
330
|
+
store_in_this_directory: store_in_this_directory
|
|
331
|
+
}}
|
|
332
|
+
_ # Return the object here.
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# =========================================================================== #
|
|
336
|
+
# === PdfParadise.burst
|
|
337
|
+
#
|
|
338
|
+
# This will "burst" all pdf files out of a single .pdf file.
|
|
339
|
+
# =========================================================================== #
|
|
340
|
+
def self.burst(
|
|
341
|
+
i = ARGV,
|
|
342
|
+
split_into_n_pages = :max,
|
|
343
|
+
store_in_this_directory = :default
|
|
344
|
+
)
|
|
345
|
+
_ = PdfParadise::SplitPdf.new(i) {{
|
|
346
|
+
split_into_n_pages: split_into_n_pages,
|
|
347
|
+
store_in_this_directory: store_in_this_directory
|
|
348
|
+
}}
|
|
349
|
+
_ # Return the object here.
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# =========================================================================== #
|
|
353
|
+
# === PdfParadise.convert
|
|
354
|
+
# =========================================================================== #
|
|
355
|
+
def self.convert(pdf_files)
|
|
356
|
+
handle = PdfParadise.split_this_pdf_file(pdf_files)
|
|
357
|
+
handle.path? # Return the file path as well.
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
if __FILE__ == $PROGRAM_NAME
|
|
363
|
+
PdfParadise::SplitPdf.new(ARGV)
|
|
364
|
+
end # split_pdf XIV-XVII_combined.pdf
|