pdf_paradise 0.1.43
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of pdf_paradise might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/README.md +376 -0
- data/bin/automatic_pdf_title +7 -0
- data/bin/convert_markdown_to_pdf +7 -0
- data/bin/convert_pdf_to_text +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 +7 -0
- data/bin/set_main_book +7 -0
- data/bin/set_title_of_this_pdf_file +15 -0
- data/doc/README.gen +348 -0
- data/doc/todo/todo.md +2 -0
- data/lib/pdf_paradise.rb +5 -0
- data/lib/pdf_paradise/base/base.rb +173 -0
- data/lib/pdf_paradise/commandline/help.rb +69 -0
- data/lib/pdf_paradise/commandline/menu.rb +160 -0
- data/lib/pdf_paradise/compress_this_pdf_file.rb +82 -0
- data/lib/pdf_paradise/constants/constants.rb +59 -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/djvu_to_pdf.rb +85 -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/delete_the_first_or_the_last_page_of_this_pdf_file/delete_the_first_or_the_last_page_of_this_pdf_file.rb +39 -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/gtk2/to_pdf/to_pdf.rb +32 -0
- data/lib/pdf_paradise/gui/gtk3/controller/controller.rb +197 -0
- data/lib/pdf_paradise/gui/gtk3/convert_pdf_to_text/convert_pdf_to_text.rb +34 -0
- data/lib/pdf_paradise/gui/gtk3/delete_the_first_or_the_last_page_of_this_pdf_file/delete_the_first_or_the_last_page_of_this_pdf_file.rb +39 -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/gtk3/statistics_widget/statistics_widget.rb +34 -0
- data/lib/pdf_paradise/gui/gtk3/to_pdf/to_pdf.rb +32 -0
- data/lib/pdf_paradise/gui/shared_code/convert_pdf_to_text/convert_pdf_to_text_module.rb +277 -0
- data/lib/pdf_paradise/gui/shared_code/delete_the_first_or_the_last_page_of_this_pdf_file/delete_the_first_or_the_last_page_of_this_pdf_file_module.rb +428 -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 +284 -0
- data/lib/pdf_paradise/gui/shared_code/split_pdf_file/split_pdf_file.css +0 -0
- data/lib/pdf_paradise/gui/shared_code/split_pdf_file/split_pdf_file_module.rb +294 -0
- data/lib/pdf_paradise/gui/shared_code/statistics_widget/statistics_widget_module.rb +333 -0
- data/lib/pdf_paradise/gui/shared_code/to_pdf/to_pdf_module.rb +273 -0
- data/lib/pdf_paradise/main_pdf/main_pdf.rb +437 -0
- data/lib/pdf_paradise/merge_pdf/menu.rb +63 -0
- data/lib/pdf_paradise/merge_pdf/merge_pdf.rb +292 -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/pdf_file_n_total_pages.rb +227 -0
- data/lib/pdf_paradise/project/project_base_directory.rb +22 -0
- data/lib/pdf_paradise/remove_pdf_password.rb +392 -0
- data/lib/pdf_paradise/requires/batch_require_toplevel_files.rb +22 -0
- data/lib/pdf_paradise/requires/colours.rb +7 -0
- data/lib/pdf_paradise/requires/colours_and_esystem_and_save_file_and_fileutils_and_opn.rb +11 -0
- data/lib/pdf_paradise/requires/esystem_and_colours.rb +10 -0
- data/lib/pdf_paradise/requires/esystem_and_opn_and_colours.rb +8 -0
- data/lib/pdf_paradise/requires/require_the_whole_project.rb +25 -0
- data/lib/pdf_paradise/requires/require_utility_scripts.rb +7 -0
- data/lib/pdf_paradise/rotate_pdf_file.rb +144 -0
- data/lib/pdf_paradise/set_main_book.rb +156 -0
- data/lib/pdf_paradise/set_pdf_title.rb +218 -0
- data/lib/pdf_paradise/sinatra/embeddable_interface.rb +315 -0
- data/lib/pdf_paradise/toplevel_methods/automatic_pdf_title.rb +55 -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 +43 -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 +19 -0
- data/lib/pdf_paradise/toplevel_methods/misc.rb +52 -0
- data/lib/pdf_paradise/toplevel_methods/number_pages.rb +38 -0
- data/lib/pdf_paradise/toplevel_methods/opened_pdf_files.rb +191 -0
- data/lib/pdf_paradise/toplevel_methods/query_pdf_title.rb +182 -0
- data/lib/pdf_paradise/toplevel_methods/reduce_size_of_this_pdf_file.rb +43 -0
- data/lib/pdf_paradise/toplevel_methods/roebe.rb +17 -0
- data/lib/pdf_paradise/toplevel_methods/to_pdf.rb +38 -0
- data/lib/pdf_paradise/utility_scripts/README.md +3 -0
- data/lib/pdf_paradise/utility_scripts/convert_pdf_to_text.rb +166 -0
- data/lib/pdf_paradise/utility_scripts/delete_first_page_of_this_pdf_file.rb +185 -0
- data/lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb +180 -0
- data/lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb +310 -0
- data/lib/pdf_paradise/utility_scripts/extract_pdf_page.rb +280 -0
- data/lib/pdf_paradise/utility_scripts/pdf_optimizer.rb +113 -0
- data/lib/pdf_paradise/utility_scripts/pdf_statistics.rb +150 -0
- data/lib/pdf_paradise/utility_scripts/remove_images.rb +110 -0
- data/lib/pdf_paradise/utility_scripts/split_pdf.rb +294 -0
- data/lib/pdf_paradise/utility_scripts/to_qdf.rb +82 -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 +276 -0
- data/pdf_paradise.gemspec +64 -0
- data/test/testing_pdf_paradise.rb +9 -0
- metadata +216 -0
@@ -0,0 +1,227 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# === PdfParadise::PdfFileNTotalPages
|
6
|
+
#
|
7
|
+
# This class can report how many pages are in a given .pdf file.
|
8
|
+
#
|
9
|
+
# Also note that an alternative exists:
|
10
|
+
#
|
11
|
+
# https://github.com/prawnpdf/pdf-inspector
|
12
|
+
#
|
13
|
+
# The code would be like this here:
|
14
|
+
#
|
15
|
+
# page_analysis = PDF::Inspector::Page.analyze(pdf)
|
16
|
+
# page_analysis.pages.size # <-- like 50 pages
|
17
|
+
#
|
18
|
+
# Usage example:
|
19
|
+
#
|
20
|
+
# PdfParadise::PdfFileNTotalPages.new(ARGV)
|
21
|
+
#
|
22
|
+
# =========================================================================== #
|
23
|
+
# require 'pdf_paradise/pdf_file_n_total_pages.rb'
|
24
|
+
# PdfParadise::PdfFileNTotalPages.new(ARGV)
|
25
|
+
# =========================================================================== #
|
26
|
+
require 'pdf_paradise/base/base.rb'
|
27
|
+
|
28
|
+
module PdfParadise
|
29
|
+
|
30
|
+
class PdfFileNTotalPages < Base # === PdfParadise::PdfFileNTotalPages
|
31
|
+
|
32
|
+
# ========================================================================= #
|
33
|
+
# === MATCH_AGAINST_THIS_REGEX
|
34
|
+
# ========================================================================= #
|
35
|
+
MATCH_AGAINST_THIS_REGEX = /Count (\d+)/
|
36
|
+
|
37
|
+
# ========================================================================= #
|
38
|
+
# === initialize
|
39
|
+
# ========================================================================= #
|
40
|
+
def initialize(
|
41
|
+
i = nil,
|
42
|
+
run_already = true
|
43
|
+
)
|
44
|
+
reset
|
45
|
+
set_commandline_arguments(i)
|
46
|
+
case run_already
|
47
|
+
when :be_silent
|
48
|
+
@be_verbose = false
|
49
|
+
run_already = false
|
50
|
+
end
|
51
|
+
run if run_already
|
52
|
+
end
|
53
|
+
|
54
|
+
# ========================================================================= #
|
55
|
+
# === reset (reset tag)
|
56
|
+
# ========================================================================= #
|
57
|
+
def reset
|
58
|
+
super()
|
59
|
+
# ======================================================================= #
|
60
|
+
# === @n_pages
|
61
|
+
#
|
62
|
+
# This variable will keep track as to how many pages the given .pdf
|
63
|
+
# page has.
|
64
|
+
# ======================================================================= #
|
65
|
+
@n_pages = 0
|
66
|
+
# ======================================================================= #
|
67
|
+
# === @be_verbose
|
68
|
+
# ======================================================================= #
|
69
|
+
@be_verbose = true
|
70
|
+
# ======================================================================= #
|
71
|
+
# === @use_this_program_to_determine_how_many_pages_are_part_of_the_pdf_file
|
72
|
+
# ======================================================================= #
|
73
|
+
@use_this_program_to_determine_how_many_pages_are_part_of_the_pdf_file = :pdfinfo
|
74
|
+
end
|
75
|
+
|
76
|
+
# ========================================================================= #
|
77
|
+
# === n_pages
|
78
|
+
# ========================================================================= #
|
79
|
+
def n_pages?
|
80
|
+
@n_pages
|
81
|
+
end; alias n_pages n_pages? # === n_pages
|
82
|
+
alias result? n_pages? # === result?
|
83
|
+
|
84
|
+
# ========================================================================= #
|
85
|
+
# === set_n_pages
|
86
|
+
# ========================================================================= #
|
87
|
+
def set_n_pages(i)
|
88
|
+
@n_pages = i
|
89
|
+
end
|
90
|
+
|
91
|
+
# ========================================================================= #
|
92
|
+
# === determine_dataset
|
93
|
+
# ========================================================================= #
|
94
|
+
def determine_dataset(of_this_pdf_file = @this_pdf_file)
|
95
|
+
@dataset = File.binread(of_this_pdf_file)
|
96
|
+
end
|
97
|
+
|
98
|
+
# ========================================================================= #
|
99
|
+
# === be_verbose?
|
100
|
+
# ========================================================================= #
|
101
|
+
def be_verbose?
|
102
|
+
@be_verbose
|
103
|
+
end
|
104
|
+
|
105
|
+
# ========================================================================= #
|
106
|
+
# === report_how_many_pages_were_found
|
107
|
+
# ========================================================================= #
|
108
|
+
def report_how_many_pages_were_found
|
109
|
+
e "#{rev}The pdf-file #{sfile(@this_pdf_file)} has exactly "\
|
110
|
+
"#{simp(n_pages?.to_s)} pages."
|
111
|
+
end
|
112
|
+
|
113
|
+
# ========================================================================= #
|
114
|
+
# === set_this_pdf_file
|
115
|
+
# ========================================================================= #
|
116
|
+
def set_this_pdf_file(i)
|
117
|
+
@this_pdf_file = i
|
118
|
+
end
|
119
|
+
|
120
|
+
# ========================================================================= #
|
121
|
+
# === determine_n_pages_via_pdfinfo
|
122
|
+
#
|
123
|
+
# We have to find an entry such as the following one:
|
124
|
+
#
|
125
|
+
# Pages: 35
|
126
|
+
#
|
127
|
+
# ========================================================================= #
|
128
|
+
def determine_n_pages_via_pdfinfo(
|
129
|
+
i = @this_pdf_file
|
130
|
+
)
|
131
|
+
if i.include? 'Pages:'
|
132
|
+
i = i.scan(/Pages: (.+)/).flatten.first.strip
|
133
|
+
end
|
134
|
+
set_n_pages(i)
|
135
|
+
end
|
136
|
+
|
137
|
+
# ========================================================================= #
|
138
|
+
# === determine_n_pages
|
139
|
+
# ========================================================================= #
|
140
|
+
def determine_n_pages
|
141
|
+
_ = @dataset
|
142
|
+
case @use_this_program_to_determine_how_many_pages_are_part_of_the_pdf_file
|
143
|
+
# ======================================================================= #
|
144
|
+
# === :pdfinfo
|
145
|
+
# ======================================================================= #
|
146
|
+
when :pdfinfo
|
147
|
+
result = `pdfinfo #{@this_pdf_file}`
|
148
|
+
determine_n_pages_via_pdfinfo(result)
|
149
|
+
else # else tag
|
150
|
+
# ===================================================================== #
|
151
|
+
# This here has the advantage (in theory) that we can just use
|
152
|
+
# a Regex and obtain the number of pages in the .pdf file. This
|
153
|
+
# works ok for many .pdf files, but not for all of them. This
|
154
|
+
# is also the reason why I switched to pdfinfo in March 2020 -
|
155
|
+
# it seems to be more reliable than the regex I am using.
|
156
|
+
# ===================================================================== #
|
157
|
+
scanned = _.scan(MATCH_AGAINST_THIS_REGEX)
|
158
|
+
# ===================================================================== #
|
159
|
+
# Note that the following may still return the wrong entry.
|
160
|
+
# I had this problem in March 2020.
|
161
|
+
# ===================================================================== #
|
162
|
+
max_value = scanned.map {|entry|
|
163
|
+
entry.first.to_i
|
164
|
+
}.max
|
165
|
+
set_n_pages(max_value)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# ========================================================================= #
|
170
|
+
# === run (run tag)
|
171
|
+
# ========================================================================= #
|
172
|
+
def run
|
173
|
+
return_files_from_the_commandline_arguments.each {|this_pdf_file|
|
174
|
+
reset
|
175
|
+
set_this_pdf_file(this_pdf_file)
|
176
|
+
determine_dataset
|
177
|
+
determine_n_pages # Determine how many pages are in that .pdf file.
|
178
|
+
report_how_many_pages_were_found if be_verbose?
|
179
|
+
}
|
180
|
+
end
|
181
|
+
|
182
|
+
# ========================================================================= #
|
183
|
+
# === PdfFileNTotalPages.return_n_pages
|
184
|
+
#
|
185
|
+
# This method is silent by default.
|
186
|
+
# ========================================================================= #
|
187
|
+
def self.return_n_pages(of_this_pdf_file)
|
188
|
+
_ = PdfFileNTotalPages.new(of_this_pdf_file, :be_silent)
|
189
|
+
_.run
|
190
|
+
return _.n_pages.to_i # Ought to return a number, as an integer.
|
191
|
+
end
|
192
|
+
|
193
|
+
# ========================================================================= #
|
194
|
+
# === PdfParadise::PdfFileNTotalPages[]
|
195
|
+
# ========================================================================= #
|
196
|
+
def self.[](i)
|
197
|
+
new(i).result?
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
# =========================================================================== #
|
203
|
+
# === PdfParadise.n_pdf_pages?
|
204
|
+
#
|
205
|
+
# This is the top-level method to find out how many pdf pages are
|
206
|
+
# part of the given .pdf file at hand.
|
207
|
+
#
|
208
|
+
# The first argument to this method is simply the name of the .pdf file
|
209
|
+
# or, rather, the path to it.
|
210
|
+
#
|
211
|
+
# Usage example:
|
212
|
+
#
|
213
|
+
# PdfParadise.n_pdf_pages?("/home/x/STUDIUM/UNI_WIEN/300260_Immunologie_und_zellul�re_Mikrobiologie_Teil_A/XIV-XVII_combined.pdf")
|
214
|
+
#
|
215
|
+
# =========================================================================== #
|
216
|
+
def self.n_pdf_pages?(of_this_pdf_file)
|
217
|
+
PdfFileNTotalPages.return_n_pages(of_this_pdf_file).to_i
|
218
|
+
end; self.instance_eval { alias n_pages? n_pdf_pages? } # === PdfParadise.n_pages?
|
219
|
+
self.instance_eval { alias n_pages n_pdf_pages? } # === PdfParadise.n_pages
|
220
|
+
self.instance_eval { alias n_pdf_pages n_pdf_pages? } # === PdfParadise.n_pdf_pages
|
221
|
+
self.instance_eval { alias n_pages_in_this_pdf_file? n_pdf_pages? } # === PdfParadise.n_pages_in_this_pdf_file?
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
if __FILE__ == $PROGRAM_NAME
|
226
|
+
PdfParadise::PdfFileNTotalPages.new(ARGV)
|
227
|
+
end # n_pages $UNI_WIEN/300609_Molekulare_Entwicklungsbiologie/Entwicklungsbiologie7.pdf
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# require 'pdf_paradise/project/project_base_directory.rb'
|
6
|
+
# =========================================================================== #
|
7
|
+
module PdfParadise
|
8
|
+
|
9
|
+
# ========================================================================= #
|
10
|
+
# === PdfParadise::PROJECT_BASE_DIRECTORY
|
11
|
+
# ========================================================================= #
|
12
|
+
PROJECT_BASE_DIRECTORY =
|
13
|
+
File.absolute_path("#{__dir__}/..")+'/'
|
14
|
+
|
15
|
+
# ========================================================================= #
|
16
|
+
# === PdfParadise.project_base_dir?
|
17
|
+
# ========================================================================= #
|
18
|
+
def self.project_base_dir?
|
19
|
+
PROJECT_BASE_DIRECTORY
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,392 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: false
|
4
|
+
# =========================================================================== #
|
5
|
+
# === PdfParadise::RemovePdfPassword
|
6
|
+
#
|
7
|
+
# Use this class to remove a password from a .pdf file.
|
8
|
+
#
|
9
|
+
# We can either use "qpdf" or "pdftk".
|
10
|
+
#
|
11
|
+
# We could also fetch the password from ENV['PASSWORD'].
|
12
|
+
#
|
13
|
+
# Usage example:
|
14
|
+
#
|
15
|
+
# PdfParadise::RemovePdfPassword.new(ARGV)
|
16
|
+
#
|
17
|
+
# =========================================================================== #
|
18
|
+
# require 'pdf_paradise/remove_pdf_password.rb'
|
19
|
+
# =========================================================================== #
|
20
|
+
require 'pdf_paradise/base/base.rb'
|
21
|
+
|
22
|
+
module PdfParadise
|
23
|
+
|
24
|
+
class RemovePdfPassword < ::PdfParadise::Base # === PdfParadise::RemovePdfPassword
|
25
|
+
|
26
|
+
include SaveFile
|
27
|
+
include FileUtils::Verbose
|
28
|
+
|
29
|
+
# ========================================================================= #
|
30
|
+
# === NAMESPACE
|
31
|
+
# ========================================================================= #
|
32
|
+
NAMESPACE = inspect
|
33
|
+
|
34
|
+
# ========================================================================= #
|
35
|
+
# === PASSWORD
|
36
|
+
#
|
37
|
+
# The password in use can be hardcoded here.
|
38
|
+
# ========================================================================= #
|
39
|
+
PASSWORD = 'Quellbrunn' # AGLMT15'
|
40
|
+
|
41
|
+
# ========================================================================= #
|
42
|
+
# === ARRAY_ALLOWED_PASSWORD_FILES
|
43
|
+
#
|
44
|
+
# A directory may include a PASSWORD.md file. In this case, we will use
|
45
|
+
# that particular file as the new password.
|
46
|
+
# ========================================================================= #
|
47
|
+
ARRAY_ALLOWED_PASSWORD_FILES = %w(
|
48
|
+
PASSWORD.md
|
49
|
+
PASSWORT.md
|
50
|
+
)
|
51
|
+
|
52
|
+
# ========================================================================= #
|
53
|
+
# === REGEX_FOR_PASSWORD
|
54
|
+
# ========================================================================= #
|
55
|
+
REGEX_FOR_PASSWORD = /--password(=| )(.+) ?/
|
56
|
+
|
57
|
+
# ========================================================================= #
|
58
|
+
# === AUTOMATICALLY_REPLACE
|
59
|
+
#
|
60
|
+
# If this constant is set to true then we will automatically
|
61
|
+
# replace the old, existing .pdf file with the newly generated
|
62
|
+
# file.
|
63
|
+
# ========================================================================= #
|
64
|
+
AUTOMATICALLY_REPLACE = false
|
65
|
+
|
66
|
+
# ========================================================================= #
|
67
|
+
# === COMMAND_NOT_FOUND
|
68
|
+
# ========================================================================= #
|
69
|
+
COMMAND_NOT_FOUND = "command not found\n"
|
70
|
+
|
71
|
+
# ========================================================================= #
|
72
|
+
# === initialize
|
73
|
+
# ========================================================================= #
|
74
|
+
def initialize(
|
75
|
+
i = nil,
|
76
|
+
run_already = true
|
77
|
+
)
|
78
|
+
reset
|
79
|
+
set_input(i)
|
80
|
+
run if run_already
|
81
|
+
end
|
82
|
+
|
83
|
+
# ========================================================================= #
|
84
|
+
# === reset (reset tag)
|
85
|
+
# ========================================================================= #
|
86
|
+
def reset
|
87
|
+
super()
|
88
|
+
set_password # Reset it here.
|
89
|
+
@output_file = nil
|
90
|
+
# ======================================================================= #
|
91
|
+
# === @automatically_replace
|
92
|
+
# ======================================================================= #
|
93
|
+
@automatically_replace = AUTOMATICALLY_REPLACE
|
94
|
+
end
|
95
|
+
|
96
|
+
# ========================================================================= #
|
97
|
+
# === input?
|
98
|
+
# ========================================================================= #
|
99
|
+
def input?
|
100
|
+
@input
|
101
|
+
end
|
102
|
+
|
103
|
+
# ========================================================================= #
|
104
|
+
# === run_sys_command
|
105
|
+
# ========================================================================= #
|
106
|
+
def run_sys_command(i = string?)
|
107
|
+
esystem i
|
108
|
+
end
|
109
|
+
|
110
|
+
# ========================================================================= #
|
111
|
+
# === string?
|
112
|
+
# ========================================================================= #
|
113
|
+
def string?
|
114
|
+
@string
|
115
|
+
end
|
116
|
+
|
117
|
+
# ========================================================================= #
|
118
|
+
# === set_password
|
119
|
+
# ========================================================================= #
|
120
|
+
def set_password(i = PASSWORD)
|
121
|
+
@password = i
|
122
|
+
end
|
123
|
+
|
124
|
+
# ========================================================================= #
|
125
|
+
# === mv
|
126
|
+
# ========================================================================= #
|
127
|
+
def mv(old, new)
|
128
|
+
new = new.strip # We don't want any newlines.
|
129
|
+
if File.exist? old
|
130
|
+
opnn; e "Now replacing `#{sfancy(old)}` with `#{simp(new)}`."
|
131
|
+
FileUtils.mv(old, new)
|
132
|
+
else
|
133
|
+
opnn; e "No file exists at `#{sfile(old)}`, thus can not rename/replace."
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# ========================================================================= #
|
138
|
+
# === password?
|
139
|
+
# ========================================================================= #
|
140
|
+
def password?
|
141
|
+
@password
|
142
|
+
end
|
143
|
+
|
144
|
+
# ========================================================================= #
|
145
|
+
# === tell_the_user_that_we_will_remove_the_password_next
|
146
|
+
# ========================================================================= #
|
147
|
+
def tell_the_user_that_we_will_remove_the_password_next(this_file = input?)
|
148
|
+
opnn; e 'Now removing the password ('+orange(password?.to_s)+') '\
|
149
|
+
'from the file `'+sfile(this_file)+'`.'
|
150
|
+
end
|
151
|
+
|
152
|
+
# ========================================================================= #
|
153
|
+
# === check_commandline (menu tag)
|
154
|
+
#
|
155
|
+
# Pass the commandline arguments through this method.
|
156
|
+
# ========================================================================= #
|
157
|
+
def check_commandline(i)
|
158
|
+
if i.is_a? Array
|
159
|
+
i.each {|entry|
|
160
|
+
check_commandline(entry)
|
161
|
+
}
|
162
|
+
else
|
163
|
+
case i.to_s # case tag
|
164
|
+
# ===================================================================== #
|
165
|
+
# === remove_password --batch
|
166
|
+
# ===================================================================== #
|
167
|
+
when /-?-?batch/
|
168
|
+
@input = Dir['*.pdf']
|
169
|
+
# ===================================================================== #
|
170
|
+
# === remove_password --password?
|
171
|
+
# ===================================================================== #
|
172
|
+
when '--password?',
|
173
|
+
'password?',
|
174
|
+
'password'
|
175
|
+
e password?
|
176
|
+
exit
|
177
|
+
# ===================================================================== #
|
178
|
+
# === remove_password --help
|
179
|
+
# ===================================================================== #
|
180
|
+
when 'HELP',
|
181
|
+
'--help',
|
182
|
+
'help'
|
183
|
+
show_help
|
184
|
+
exit
|
185
|
+
else
|
186
|
+
if i.include? '--'
|
187
|
+
if i.include?('--password ') or i.include?('--password')
|
188
|
+
i =~ REGEX_FOR_PASSWORD # See here for the test: http://rubular.com/r/P6LScCOVYZ
|
189
|
+
set_password($2.to_s.dup) # The second capture group holds our result.
|
190
|
+
i.gsub!(REGEX_FOR_PASSWORD, '')
|
191
|
+
end
|
192
|
+
end unless i.is_a? Symbol
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
# ========================================================================= #
|
198
|
+
# === show_help (help tag)
|
199
|
+
#
|
200
|
+
# Show some instructions in how to use this class.
|
201
|
+
#
|
202
|
+
# Invocation example:
|
203
|
+
#
|
204
|
+
# nopassword --help
|
205
|
+
#
|
206
|
+
# ========================================================================= #
|
207
|
+
def show_help
|
208
|
+
e
|
209
|
+
e 'You can set a different password via:'
|
210
|
+
e
|
211
|
+
e sfancy(' --password NEW_PASSWORD_HERE')
|
212
|
+
e
|
213
|
+
e 'nopdf *.pdf --password '+password?
|
214
|
+
e
|
215
|
+
e 'If you want to batch-process all .pdf files in the '\
|
216
|
+
'current directory,'
|
217
|
+
e 'and subsequently remove all password-protection '\
|
218
|
+
'from these files, do:'
|
219
|
+
e
|
220
|
+
e sfancy(' --batch')
|
221
|
+
e
|
222
|
+
end
|
223
|
+
|
224
|
+
# ========================================================================= #
|
225
|
+
# === remove_password_through_qpdf
|
226
|
+
#
|
227
|
+
# Here we make use of qpdf.
|
228
|
+
# ========================================================================= #
|
229
|
+
def remove_password_through_qpdf(
|
230
|
+
work_on_this_file = input?
|
231
|
+
)
|
232
|
+
if qpdf_exists?
|
233
|
+
set_output_file(work_on_this_file)
|
234
|
+
@string = 'qpdf --password='+password?+' --decrypt '+
|
235
|
+
work_on_this_file+' '+output_file?
|
236
|
+
else
|
237
|
+
e 'qpdf does not exist. Please install it. Exiting now.'
|
238
|
+
exit
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
# ========================================================================= #
|
243
|
+
# === set_output_file
|
244
|
+
# ========================================================================= #
|
245
|
+
def set_output_file(i)
|
246
|
+
i = file_name_without_extension?(i)
|
247
|
+
i = "no_password_#{i}.pdf"
|
248
|
+
@output_file = i
|
249
|
+
end
|
250
|
+
|
251
|
+
# ========================================================================= #
|
252
|
+
# === file_name_without_extension?
|
253
|
+
# ========================================================================= #
|
254
|
+
def file_name_without_extension?(i = input?)
|
255
|
+
i.strip.sub(/\.pdf$/,'') # Chop off trailing extension name.
|
256
|
+
end
|
257
|
+
|
258
|
+
# ========================================================================= #
|
259
|
+
# === output_file?
|
260
|
+
# ========================================================================= #
|
261
|
+
def output_file?
|
262
|
+
@output_file
|
263
|
+
end; alias output? output_file? # === output?
|
264
|
+
|
265
|
+
# ========================================================================= #
|
266
|
+
# === opnn
|
267
|
+
# ========================================================================= #
|
268
|
+
def opnn
|
269
|
+
super(NAMESPACE)
|
270
|
+
end
|
271
|
+
|
272
|
+
# ========================================================================= #
|
273
|
+
# === qpdf_exists?
|
274
|
+
#
|
275
|
+
# Query method to try to find out whether qpdf is installed.
|
276
|
+
# ========================================================================= #
|
277
|
+
def qpdf_exists?
|
278
|
+
result = `qpdf 2>&1`
|
279
|
+
! result.include?(COMMAND_NOT_FOUND)
|
280
|
+
end
|
281
|
+
|
282
|
+
# ========================================================================= #
|
283
|
+
# === remove_password_through_pdftk
|
284
|
+
# ========================================================================= #
|
285
|
+
def remove_password_through_pdftk(this_file)
|
286
|
+
# ======================================================================= #
|
287
|
+
# Decrypt a PDF called input.pdf with YOURPASSWORD-HERE password
|
288
|
+
# and create unencrypted output.pdf, enter:
|
289
|
+
# ======================================================================= #
|
290
|
+
@string = 'pdftk '+this_file+' output no_password_output.pdf user_pw '+password?
|
291
|
+
end
|
292
|
+
|
293
|
+
# ========================================================================= #
|
294
|
+
# === consider_replacing_the_old_file
|
295
|
+
# ========================================================================= #
|
296
|
+
def consider_replacing_the_old_file(
|
297
|
+
old_file = input?,
|
298
|
+
output_file = @output_file
|
299
|
+
)
|
300
|
+
if @automatically_replace
|
301
|
+
opnn; e 'The constant '+swarn('AUTOMATICALLY_REPLACE')+' is set to true.'
|
302
|
+
opnn; e 'This means that we will automatically try to replace '\
|
303
|
+
'the old .pdf with the new .pdf'
|
304
|
+
mv(output_file, old_file)
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
# ========================================================================= #
|
309
|
+
# === set_input
|
310
|
+
#
|
311
|
+
# Since as of Jul 2016, the input given to this class must always
|
312
|
+
# be ketp as an Array.
|
313
|
+
# ========================================================================= #
|
314
|
+
def set_input(
|
315
|
+
i = '', do_check_the_commandline = true
|
316
|
+
)
|
317
|
+
case do_check_the_commandline
|
318
|
+
when :do_not_check_the_commandline
|
319
|
+
do_check_the_commandline = false
|
320
|
+
end
|
321
|
+
# ======================================================================= #
|
322
|
+
# Next we will have special symbol-instructions here, in
|
323
|
+
# particular :all_pdf_files
|
324
|
+
# ======================================================================= #
|
325
|
+
if i.is_a? Symbol
|
326
|
+
case i
|
327
|
+
when :all_pdf_files
|
328
|
+
i = Dir['*.pdf']
|
329
|
+
end
|
330
|
+
end
|
331
|
+
if i.is_a? Array
|
332
|
+
i.flatten!
|
333
|
+
else
|
334
|
+
i = [i]
|
335
|
+
end
|
336
|
+
@input = i # This is guaranteed to be an Array.
|
337
|
+
check_commandline(i) if do_check_the_commandline
|
338
|
+
end
|
339
|
+
|
340
|
+
# ========================================================================= #
|
341
|
+
# === run (run tag)
|
342
|
+
# ========================================================================= #
|
343
|
+
def run
|
344
|
+
check_for_a_password_file_in_the_current_directory
|
345
|
+
# ======================================================================= #
|
346
|
+
# Iterate over the given input-files.
|
347
|
+
# ======================================================================= #
|
348
|
+
input?.each {|file|
|
349
|
+
remove_password_through_qpdf(file)
|
350
|
+
# remove_password_through_pdftk(file) # Pick this, or the other one.
|
351
|
+
tell_the_user_that_we_will_remove_the_password_next(file)
|
352
|
+
run_sys_command
|
353
|
+
consider_replacing_the_old_file(file)
|
354
|
+
}
|
355
|
+
end
|
356
|
+
|
357
|
+
# ========================================================================= #
|
358
|
+
# === check_for_a_password_file_in_the_current_directory (password tag)
|
359
|
+
#
|
360
|
+
# We can check for several password files.
|
361
|
+
# ========================================================================= #
|
362
|
+
def check_for_a_password_file_in_the_current_directory
|
363
|
+
_ = ARRAY_ALLOWED_PASSWORD_FILES
|
364
|
+
does_any_file_exist = _.select {|entry|
|
365
|
+
File.exist?(entry)
|
366
|
+
}
|
367
|
+
does_a_password_file_exist = !does_any_file_exist.empty?
|
368
|
+
if does_a_password_file_exist
|
369
|
+
this_selection = does_any_file_exist.first
|
370
|
+
opnn; e 'A file called `'+sfile(this_selection)+'` was found in the current directory.'
|
371
|
+
if File.zero? this_selection # Check whether it is empty.
|
372
|
+
opnn; e 'As it is, however had, empty, we will not change the '\
|
373
|
+
"password to the file's content."
|
374
|
+
else
|
375
|
+
# =================================================================== #
|
376
|
+
# Read in the new password from the existing file.
|
377
|
+
# =================================================================== #
|
378
|
+
use_this_dataset = File.readlines(this_selection).map(&:chomp)
|
379
|
+
set_password(
|
380
|
+
use_this_dataset.first # <- We will only honour the first line of this file.
|
381
|
+
)
|
382
|
+
opnn; e 'We will use the content of this file as the new password '\
|
383
|
+
'(Password thus being: '+orange(password?)+').'
|
384
|
+
end
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
end; end
|
389
|
+
|
390
|
+
if __FILE__ == $PROGRAM_NAME
|
391
|
+
PdfParadise::RemovePdfPassword.new(ARGV)
|
392
|
+
end # no_password
|