libis-format 1.0.5 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -1
  3. data/.travis.yml +32 -24
  4. data/README.md +2 -2
  5. data/base/Dockerfile +24 -2
  6. data/base/Dockerfile.alpine +20 -0
  7. data/base/Dockerfile.rvm +56 -0
  8. data/base/rework_path +20 -0
  9. data/docker_cfg.yml +1 -0
  10. data/lib/libis/format.rb +12 -3
  11. data/lib/libis/format/cli/convert.rb +4 -4
  12. data/lib/libis/format/config.rb +16 -12
  13. data/lib/libis/format/converter/audio_converter.rb +2 -36
  14. data/lib/libis/format/converter/base.rb +22 -8
  15. data/lib/libis/format/converter/chain.rb +3 -3
  16. data/lib/libis/format/converter/image_assembler.rb +82 -0
  17. data/lib/libis/format/converter/image_converter.rb +20 -138
  18. data/lib/libis/format/converter/image_splitter.rb +84 -0
  19. data/lib/libis/format/converter/image_watermarker.rb +261 -0
  20. data/lib/libis/format/converter/jp2_converter.rb +1 -1
  21. data/lib/libis/format/converter/office_converter.rb +2 -2
  22. data/lib/libis/format/converter/pdf_assembler.rb +66 -0
  23. data/lib/libis/format/converter/pdf_converter.rb +6 -132
  24. data/lib/libis/format/converter/pdf_metadata.rb +82 -0
  25. data/lib/libis/format/converter/pdf_optimizer.rb +67 -0
  26. data/lib/libis/format/converter/pdf_protecter.rb +147 -0
  27. data/lib/libis/format/converter/pdf_selecter.rb +83 -0
  28. data/lib/libis/format/converter/pdf_splitter.rb +70 -0
  29. data/lib/libis/format/converter/pdf_watermarker_header.rb +71 -0
  30. data/lib/libis/format/converter/pdf_watermarker_image.rb +76 -0
  31. data/lib/libis/format/converter/pdf_watermarker_text.rb +93 -0
  32. data/lib/libis/format/converter/spreadsheet_converter.rb +2 -2
  33. data/lib/libis/format/converter/video_converter.rb +1 -1
  34. data/lib/libis/format/identifier.rb +3 -3
  35. data/lib/libis/format/info.rb +27 -0
  36. data/lib/libis/format/library.rb +147 -0
  37. data/lib/libis/format/tool.rb +4 -1
  38. data/lib/libis/format/tool/extension_identification.rb +4 -4
  39. data/lib/libis/format/tool/identification_tool.rb +6 -6
  40. data/lib/libis/format/tool/pdf_merge.rb +3 -3
  41. data/lib/libis/format/tool/{pdf_copy.rb → pdf_metadata.rb} +5 -5
  42. data/lib/libis/format/tool/pdf_protect.rb +47 -0
  43. data/lib/libis/format/tool/pdf_select.rb +47 -0
  44. data/lib/libis/format/tool/pdf_split.rb +4 -4
  45. data/lib/libis/format/tool/pdf_watermark.rb +47 -0
  46. data/lib/libis/format/tool/spreadsheet_to_ods.rb +1 -0
  47. data/lib/libis/format/version.rb +1 -1
  48. data/lib/libis/format/yaml_loader.rb +71 -0
  49. data/libis-format.gemspec +3 -2
  50. data/tools/PdfTool.jar +0 -0
  51. data/tools/bcpkix-jdk15on-167.jar +0 -0
  52. data/tools/bcprov-jdk15on-167.jar +0 -0
  53. metadata +32 -13
  54. data/lib/libis/format/type_database.rb +0 -134
  55. data/lib/libis/format/type_database_impl.rb +0 -120
  56. data/tools/bcpkix-jdk15on-1.49.jar +0 -0
  57. data/tools/bcprov-jdk15on-1.49.jar +0 -0
@@ -15,7 +15,7 @@ module Libis
15
15
  end
16
16
 
17
17
  def self.output_types(format = nil)
18
- return [] unless input_types.include?(format)
18
+ return [] unless input_types.include?(format) if format
19
19
  [:JP2]
20
20
  end
21
21
 
@@ -3,7 +3,7 @@
3
3
  require_relative 'base'
4
4
 
5
5
  require 'libis/format/tool/office_to_pdf'
6
- require 'libis/format/type_database'
6
+ require 'libis/format/library'
7
7
 
8
8
  module Libis
9
9
  module Format
@@ -32,7 +32,7 @@ module Libis
32
32
  end
33
33
 
34
34
  def self.output_types(format = nil)
35
- return [] unless input_types.include?(format)
35
+ return [] unless input_types.include?(format) if format
36
36
  [:PDF]
37
37
  end
38
38
 
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative 'base'
4
+
5
+ require 'libis/format/tool/pdf_merge'
6
+
7
+ module Libis
8
+ module Format
9
+ module Converter
10
+
11
+ # noinspection DuplicatedCode
12
+ class PdfAssembler < Libis::Format::Converter::Base
13
+
14
+ def self.input_types
15
+ [:PDF]
16
+ end
17
+
18
+ def self.output_types(format = nil)
19
+ return [] unless input_types.include?(format) if format
20
+ [:PDF]
21
+ end
22
+
23
+ def self.category
24
+ :assembler
25
+ end
26
+
27
+ def pdf_assemnble(_)
28
+ #force usage of this converter
29
+ end
30
+
31
+ def convert(source, target, format, opts = {})
32
+ super
33
+
34
+ result = if source.is_a? Array
35
+ assemble(source, target)
36
+ elsif File.directory?(source)
37
+ source_list = Dir[File.join(source, '**', '*')].reject {|p| File.directory? p}
38
+ assemble(source_list, target)
39
+ else
40
+ assemble([source], target)
41
+ end
42
+ return nil unless result
43
+
44
+ result
45
+ end
46
+
47
+ private
48
+
49
+ def assemble(source, target)
50
+
51
+ result = Libis::Format::Tool::PdfMerge.run(source, target)
52
+
53
+ unless result[:err].empty?
54
+ error("PdfMerge encountered errors:\n%s", result[:err].join(join("\n")))
55
+ return nil
56
+ end
57
+
58
+ target
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
@@ -2,15 +2,13 @@
2
2
 
3
3
  require_relative 'base'
4
4
 
5
- require 'libis/tools/extend/hash'
6
- require 'libis/format/tool/pdf_copy'
7
- require 'libis/format/tool/pdf_to_pdfa'
8
- require 'libis/format/tool/pdf_optimizer'
5
+ require 'libis/format/tool/pdf_metadata'
9
6
 
10
7
  module Libis
11
8
  module Format
12
9
  module Converter
13
10
 
11
+ # noinspection DuplicatedCode
14
12
  class PdfConverter < Libis::Format::Converter::Base
15
13
 
16
14
  def self.input_types
@@ -18,144 +16,20 @@ module Libis
18
16
  end
19
17
 
20
18
  def self.output_types(format = nil)
21
- return [] unless input_types.include?(format)
22
- [:PDF, :PDFA]
23
- end
24
-
25
- def pdf_convert(_)
26
- #force usage of this converter
27
- end
28
-
29
- # Set metadata for Pdf file
30
- #
31
- # valid metadata keys are):
32
- # - title
33
- # - author
34
- # - creator
35
- # - keywords
36
- # - subject
37
- #
38
- # @param [Hash] values list of metadata values to set
39
- def metadata(values = {})
40
- values.key_strings_to_symbols!
41
- values.each do |k, v|
42
- next unless [:title, :author, :creator, :keywords, :subject].include?(k)
43
- @options["md_#{k}"] = v
44
- end
45
- end
46
-
47
- # Select a partial list of pages
48
- # @param [String] selection as described in com.itextpdf.text.pdf.SequenceList: [!][o][odd][e][even]start-end
49
- def range(selection)
50
- @options[:ranges] = selection
51
- end
52
-
53
- # Create or use a watermark image.
54
- #
55
- # The watermark options are (use symbols):
56
- # - text: text to create a watermark from
57
- # - file: watermark image to use
58
- # - rotation: rotation of the watermark text (in degrees; integer number)
59
- # - size: font size of the watermark text
60
- # - opacity: opacity of the watermark (fraction 0.0 - 1.0)
61
- # - gap: size of the gap between watermark instances. Integer value is absolute size in points (1/72 inch). Fractions are percentage of widht/height.
62
- # If both options are given, the file will be used as-is if it exists and is a valid image file. Otherwise the
63
- # file will be created or overwritten with a newly created watermark image.
64
- #
65
- # The created watermark file will be a PNG image with transparent background containing the supplied text
66
- # slanted by 30 degrees counter-clockwise.
67
- #
68
- # @param [Hash] options Hash of options for watermark creation.
69
- def watermark(options = {})
70
- options.key_strings_to_symbols!
71
- if options[:file] && File.exist?(options[:file])
72
- @options['wm_image'] = options[:file]
73
- else
74
- @options['wm_text'] = (options[:text] || '© LIBIS').split('\n')
75
- @options['wm_text_rotation'] = options[:rotation] if options[:rotation]
76
- @options['wm_font_size'] = options[:size] if options[:size]
77
- end
78
- @options['wm_opacity'] = options[:opacity] || '0.3'
79
- @options['wm_gap_ratio'] = options[:gap] if options[:gap].to_s =~ /^\s*(0+\.\d+|1\.0+)\s*$/
80
- @options['wm_gap_size'] = options[:gap] if options[:gap].to_s =~ /^\s*\d+\s*$/
81
- end
82
-
83
- # Optimize the PDF
84
- #
85
- # This reduces the graphics quality to a level in order to limit file size. This option relies on the
86
- # presence of ghostscript and takes one argument: the quality level. It should be one of:
87
- #
88
- # - 0 : lowest quality (Acrobat Distiller 'Screen Optimized' equivalent)
89
- # - 1 : medium quality (Acrobat Distiller 'eBook' equivalent)
90
- # - 2 : good quality
91
- # - 3 : high quality (Acrobat Distiller 'Print Optimized' equivalent)
92
- # - 4 : highest quality (Acrobat Distiller 'Prepress Optimized' equivalent)
93
- #
94
- # Note that the optimization is intended to be used with PDF's containing high-resolution images.
95
- #
96
- # @param [Integer] setting quality setting. [0-4]
97
- def optimize(setting = 1)
98
- @options['optimize'] = %w(screen ebook default printer prepress)[setting] if (0..4) === setting
19
+ return [] unless input_types.include?(format) if format
20
+ [:PDFA]
99
21
  end
100
22
 
101
23
  def convert(source, target, format, opts = {})
102
24
  super
103
25
 
104
- result = nil
105
-
106
- if (quality = @options.delete('optimize'))
107
- result = optimize_pdf(source, target, quality)
108
- return nil unless result
109
- source = result
110
- end
111
-
112
- unless @options.empty?
113
- result = convert_pdf(source, target)
114
- return nil unless result
115
- source = result
116
- end
117
-
118
- if format == :PDFA and source
119
- result = pdf_to_pdfa(source, target)
120
- end
26
+ result = pdf_to_pdfa(source, target)
27
+ return nil unless result
121
28
 
122
29
  result
123
30
 
124
31
  end
125
32
 
126
- def optimize_pdf(source, target, quality)
127
-
128
- using_temp(target) do |tmpname|
129
- result = Libis::Format::Tool::PdfOptimizer.run(source, tmpname, quality)
130
- unless result[:status] == 0
131
- error("Pdf optimization encountered errors:\n%s", (result[:err] + result[:out]).join("\n"))
132
- next nil
133
- end
134
- tmpname
135
- end
136
- end
137
-
138
- def convert_pdf(source, target)
139
-
140
- using_temp(target) do |tmpname|
141
- result = Libis::Format::Tool::PdfCopy.run(
142
- source, tmpname,
143
- @options.map {|k, v|
144
- if v.nil?
145
- nil
146
- else
147
- ["--#{k}", (v.is_a?(Array) ? v : v.to_s)]
148
- end}.flatten
149
- )
150
- unless result[:err].empty?
151
- error("Pdf conversion encountered errors:\n%s", result[:err].join(join("\n")))
152
- next nil
153
- end
154
- tmpname
155
- end
156
-
157
- end
158
-
159
33
  def pdf_to_pdfa(source, target)
160
34
 
161
35
  using_temp(target) do |tmpname|
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative 'base'
4
+
5
+ require 'libis/format/tool/pdf_metadata'
6
+
7
+ module Libis
8
+ module Format
9
+ module Converter
10
+
11
+ # noinspection DuplicatedCode
12
+ class PdfMetadata < Libis::Format::Converter::Base
13
+
14
+ def self.input_types
15
+ [:PDF]
16
+ end
17
+
18
+ def self.output_types(format = nil)
19
+ return [] unless input_types.include?(format) if format
20
+ [:PDF]
21
+ end
22
+
23
+ def title(v)
24
+ @options[:title] = v.blank? ? nil : v
25
+ end
26
+
27
+ def author(v)
28
+ @options[:author] = v.blank? ? nil : v
29
+ end
30
+
31
+ def creator(v)
32
+ @options[:creator] = v.blank? ? nil : v
33
+ end
34
+
35
+ def keywords(v)
36
+ @options[:keywords] = v.blank? ? nil : v
37
+ end
38
+
39
+ def subject(v)
40
+ @options[:subject] = v.blank? ? nil : v
41
+ end
42
+
43
+ def convert(source, target, format, opts = {})
44
+ super
45
+
46
+ result = nil
47
+
48
+ unless @options.empty?
49
+ result = convert_pdf(source, target)
50
+ return nil unless result
51
+ end
52
+
53
+ result
54
+
55
+ end
56
+
57
+ def convert_pdf(source, target)
58
+
59
+ using_temp(target) do |tmpname|
60
+ result = Libis::Format::Tool::PdfMetadata.run(
61
+ source, tmpname,
62
+ @options.map {|k, v|
63
+ if v.nil?
64
+ nil
65
+ else
66
+ ["--#{k}", v]
67
+ end}.compact.flatten
68
+ )
69
+ unless result[:err].empty?
70
+ error("Pdf conversion encountered errors:\n%s", result[:err].join(join("\n")))
71
+ next nil
72
+ end
73
+ tmpname
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative 'base'
4
+
5
+ require 'libis/format/tool/pdf_optimizer'
6
+
7
+ module Libis
8
+ module Format
9
+ module Converter
10
+
11
+ class PdfOptimizer < Libis::Format::Converter::Base
12
+
13
+ def self.input_types
14
+ [:PDF]
15
+ end
16
+
17
+ def self.output_types(format = nil)
18
+ return [] unless input_types.include?(format) if format
19
+ [:PDF]
20
+ end
21
+
22
+ def pdf_optimize(_)
23
+ #force usage of this converter
24
+ end
25
+
26
+ # Optimize the PDF
27
+ #
28
+ # This reduces the graphics quality to a level in order to limit file size. This option relies on the
29
+ # presence of ghostscript and takes one argument: the quality level. It should be one of:
30
+ #
31
+ # - 0 : lowest quality (Acrobat Distiller 'Screen Optimized' equivalent)
32
+ # - 1 : medium quality (Acrobat Distiller 'eBook' equivalent)
33
+ # - 2 : good quality
34
+ # - 3 : high quality (Acrobat Distiller 'Print Optimized' equivalent)
35
+ # - 4 : highest quality (Acrobat Distiller 'Prepress Optimized' equivalent)
36
+ #
37
+ # Note that the optimization is intended to be used with PDF's containing high-resolution images.
38
+ #
39
+ # @param [Integer] setting quality setting. [0-4]
40
+ def quality(setting = 1)
41
+ @quality = %w(screen ebook default printer prepress)[setting] if (0..4) === setting
42
+ end
43
+
44
+ def convert(source, target, format, opts = {})
45
+ super
46
+
47
+ optimize_pdf(source, target, @quality || 'ebook')
48
+
49
+ end
50
+
51
+ def optimize_pdf(source, target, quality)
52
+
53
+ using_temp(target) do |tmpname|
54
+ result = Libis::Format::Tool::PdfOptimizer.run(source, tmpname, quality)
55
+ unless result[:status] == 0
56
+ error("Pdf optimization encountered errors:\n%s", (result[:err] + result[:out]).join("\n"))
57
+ next nil
58
+ end
59
+ tmpname
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,147 @@
1
+ # encoding: utf-8
2
+ require 'securerandom'
3
+
4
+ require_relative 'base'
5
+ require 'libis/format/tool/pdf_protect'
6
+
7
+ module Libis
8
+ module Format
9
+ module Converter
10
+
11
+ RANDOM_CHARS = [('a'..'z'), ('A'..'Z'), ('0'..'9')].map(&:to_a).flatten
12
+
13
+ # noinspection DuplicatedCode
14
+ class PdfProtecter < Libis::Format::Converter::Base
15
+
16
+ def self.input_types
17
+ [:PDF]
18
+ end
19
+
20
+ def self.output_types(format = nil)
21
+ return [] unless input_types.include?(format) if format
22
+ [:PDF, :PDFA]
23
+ end
24
+
25
+ def pdf_protect(_)
26
+ #force usage of this converter
27
+ end
28
+
29
+ def initialize
30
+ super
31
+ @options[:edit_password] = SecureRandom.urlsafe_base64(31)
32
+ end
33
+
34
+ def edit_password(pwd)
35
+ @options[:edit_password] = pwd
36
+ end
37
+
38
+ def open_password(pwd)
39
+ @options[:open_password] = pwd
40
+ end
41
+
42
+ def assist(v)
43
+ @flags[:assist] = !!v
44
+ end
45
+
46
+ def copy(v)
47
+ @flags[:copy] = !!v
48
+ end
49
+
50
+ def print(v)
51
+ @flags[:print] = !!v
52
+ end
53
+
54
+ def comments(v)
55
+ @flags[:comments] = !!v
56
+ end
57
+
58
+ def fillin(v)
59
+ @flags[:fillin] = !!v
60
+ end
61
+
62
+ def manage(v)
63
+ @flags[:manage] = !!v
64
+ end
65
+
66
+ def edit(v)
67
+ @flags[:edit] = !!v
68
+ end
69
+
70
+ def convert(source, target, format, opts = {})
71
+ super
72
+
73
+ result = nil
74
+
75
+ unless @options.empty?
76
+ result = convert_pdf(source, target)
77
+ return nil unless result
78
+ source = result
79
+ end
80
+
81
+ if format == :PDFA and source
82
+ result = pdf_to_pdfa(source, target)
83
+ end
84
+
85
+ result
86
+
87
+ end
88
+
89
+ OPTIONS_TABLE = {
90
+ edit_password: 'edit-password',
91
+ open_password: 'open-password',
92
+ fillin: 'fill-in',
93
+ }
94
+
95
+ def convert_pdf(source, target)
96
+
97
+ using_temp(target) do |tmpname|
98
+ result = Libis::Format::Tool::PdfProtect.run(
99
+ source, tmpname,
100
+ [
101
+ @options.map { |k, v|
102
+ if v.nil?
103
+ nil
104
+ else
105
+ k = OPTIONS_TABLE[k] || k
106
+ ["--#{k}", v]
107
+ end
108
+ },
109
+ @flags.map { |k, v|
110
+ if !v
111
+ nil
112
+ else
113
+ k = OPTIONS_TABLE[k] || k
114
+ "--#{k}"
115
+ end
116
+ }
117
+ ].compact.flatten
118
+ )
119
+ unless result[:err].empty?
120
+ error("Pdf conversion encountered errors:\n%s", result[:err].join(join("\n")))
121
+ next nil
122
+ end
123
+ tmpname
124
+ end
125
+
126
+ end
127
+
128
+ def pdf_to_pdfa(source, target)
129
+
130
+ using_temp(target) do |tmpname|
131
+ result = Libis::Format::Tool::PdfToPdfa.run source, tmpname
132
+ if result[:status] != 0
133
+ error("Pdf/A conversion encountered errors:\n%s", result[:err].join("\n"))
134
+ next nil
135
+ else
136
+ warn("Pdf/A conversion warnings:\n%s", result[:err].join("\n")) unless result[:err].empty?
137
+ end
138
+ tmpname
139
+ end
140
+
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+ end
147
+ end