libis-format 1.3.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +2 -0
- data/.gitignore +20 -0
- data/.travis.yml +70 -0
- data/Gemfile +0 -12
- data/README.md +2 -2
- data/Rakefile +8 -0
- data/base/Dockerfile +35 -0
- data/base/Dockerfile.alpine +20 -0
- data/base/Dockerfile.rvm +56 -0
- data/base/rework_path +20 -0
- data/bin/{pdf_tool → pdf_copy} +2 -3
- data/data/PDFA_def.ps +3 -3
- data/data/eciRGB_v2.icc +0 -0
- data/data/types.yml +4 -17
- data/docker_cfg.yml +1 -0
- data/lib/libis/format/cli/convert.rb +4 -4
- data/lib/libis/format/cli/prompt_helper.rb +24 -32
- data/lib/libis/format/command_line.rb +3 -2
- data/lib/libis/format/config.rb +23 -19
- data/lib/libis/format/converter/audio_converter.rb +31 -56
- data/lib/libis/format/converter/base.rb +36 -16
- data/lib/libis/format/converter/chain.rb +32 -52
- data/lib/libis/format/converter/fop_pdf_converter.rb +8 -4
- data/lib/libis/format/converter/image_assembler.rb +82 -0
- data/lib/libis/format/converter/image_converter.rb +40 -153
- data/lib/libis/format/converter/image_splitter.rb +80 -0
- data/lib/libis/format/converter/image_watermarker.rb +261 -0
- data/lib/libis/format/converter/jp2_converter.rb +38 -36
- data/lib/libis/format/converter/office_converter.rb +28 -22
- data/lib/libis/format/converter/pdf_assembler.rb +66 -0
- data/lib/libis/format/converter/pdf_converter.rb +52 -200
- data/lib/libis/format/converter/pdf_optimizer.rb +70 -0
- data/lib/libis/format/converter/pdf_splitter.rb +65 -0
- data/lib/libis/format/converter/pdf_watermarker.rb +110 -0
- data/lib/libis/format/converter/repository.rb +13 -7
- data/lib/libis/format/converter/spreadsheet_converter.rb +16 -10
- data/lib/libis/format/converter/video_converter.rb +58 -47
- data/lib/libis/format/converter/xslt_converter.rb +11 -13
- data/lib/libis/format/converter.rb +1 -1
- data/lib/libis/format/identifier.rb +46 -44
- data/lib/libis/format/info.rb +27 -0
- data/lib/libis/format/library.rb +147 -0
- data/lib/libis/format/tool/droid.rb +30 -29
- data/lib/libis/format/tool/extension_identification.rb +26 -24
- data/lib/libis/format/tool/{ff_mpeg.rb → ffmpeg.rb} +10 -17
- data/lib/libis/format/tool/fido.rb +27 -22
- data/lib/libis/format/tool/file_tool.rb +24 -11
- data/lib/libis/format/tool/fop_pdf.rb +14 -25
- data/lib/libis/format/tool/identification_tool.rb +40 -38
- data/lib/libis/format/tool/office_to_pdf.rb +18 -30
- data/lib/libis/format/tool/pdf_copy.rb +47 -0
- data/lib/libis/format/tool/pdf_merge.rb +19 -25
- data/lib/libis/format/tool/pdf_optimizer.rb +19 -22
- data/lib/libis/format/tool/pdf_split.rb +33 -6
- data/lib/libis/format/tool/pdf_to_pdfa.rb +31 -45
- data/lib/libis/format/tool/pdfa_validator.rb +30 -24
- data/lib/libis/format/tool/spreadsheet_to_ods.rb +18 -29
- data/lib/libis/format/tool.rb +3 -4
- data/lib/libis/format/version.rb +1 -3
- data/lib/libis/format/yaml_loader.rb +71 -0
- data/lib/libis/format.rb +7 -5
- data/lib/libis-format.rb +0 -2
- data/libis-format.gemspec +18 -24
- data/tools/PdfTool.jar +0 -0
- data/tools/pdfbox/pdfbox-app-2.0.13.jar +0 -0
- data/tools/pdfbox/{preflight-app-3.0.3.jar → preflight-app-2.0.13.jar} +0 -0
- metadata +83 -125
- data/data/AdobeRGB1998.icc +0 -0
- data/lib/libis/format/converter/email_converter.rb +0 -35
- data/lib/libis/format/tool/msg_to_pdf.rb +0 -270
- data/lib/libis/format/tool/pdf_tool.rb +0 -52
- data/lib/libis/format/type_database.rb +0 -156
- data/lib/libis/format/type_database_impl.rb +0 -153
- data/tools/pdf2pdfa +0 -395
- data/tools/pdfbox/pdfbox-app-3.0.3.jar +0 -0
- /data/bin/{droid_tool → droid} +0 -0
- /data/bin/{fido_tool → fido} +0 -0
@@ -1,5 +1,4 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# encoding: utf-8
|
3
2
|
require 'libis-tools'
|
4
3
|
require 'fileutils'
|
5
4
|
require 'libis/format/config'
|
@@ -8,31 +7,32 @@ require_relative 'base'
|
|
8
7
|
module Libis
|
9
8
|
module Format
|
10
9
|
module Converter
|
10
|
+
|
11
11
|
class Jp2Converter < Libis::Format::Converter::Base
|
12
|
+
|
12
13
|
def self.input_types
|
13
|
-
|
14
|
+
[:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF]
|
14
15
|
end
|
15
16
|
|
16
17
|
def self.output_types(format = nil)
|
17
|
-
return [] unless input_types.include?(format)
|
18
|
-
|
18
|
+
return [] unless input_types.include?(format) if format
|
19
19
|
[:JP2]
|
20
20
|
end
|
21
21
|
|
22
22
|
def initialize
|
23
23
|
super
|
24
24
|
@options = {
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
color_xform: false,
|
26
|
+
error_resilience: :ALL,
|
27
|
+
lossless: true,
|
28
|
+
progression_order: 'RLCP',
|
29
|
+
tile_size: [1024, 1024],
|
30
|
+
codeblock_size: [6, 6],
|
31
31
|
}
|
32
32
|
end
|
33
33
|
|
34
34
|
def j2kdriver(_)
|
35
|
-
#
|
35
|
+
#force usage of this converter
|
36
36
|
end
|
37
37
|
|
38
38
|
def color_xform(flag = true)
|
@@ -68,36 +68,38 @@ module Libis
|
|
68
68
|
|
69
69
|
@options.each do |key, value|
|
70
70
|
case key
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
71
|
+
when :color_xform
|
72
|
+
options << '--set-output-j2k-color-xform' << (value ? 'YES' : 'NO')
|
73
|
+
when :error_resilience
|
74
|
+
options << '--set-output-j2k-error-resilience' << value.to_s
|
75
|
+
when :lossless
|
76
|
+
if value
|
77
|
+
options << '--set-output-j2k-xform' << 'R53' << '5' << '--set-output-j2k-ratio' << '0'
|
78
|
+
else
|
79
|
+
options << '--set-output-j2k-xform' << 'I97' << '--set-output-j2k-psnr' << '46'
|
80
|
+
end
|
81
|
+
when :progression_order
|
82
|
+
options << '--set-output-j2k-progression-order' << value.to_s
|
83
|
+
when :tile_size
|
84
|
+
options << '--set-output-j2k-tile-size' << value[0].to_s << value[1].to_s
|
85
|
+
when :codeblock_size
|
86
|
+
options << '--set-output-j2k-codeblock-size' << value[0].to_s << value[1].to_s
|
78
87
|
else
|
79
|
-
|
80
|
-
end
|
81
|
-
when :progression_order
|
82
|
-
options << '--set-output-j2k-progression-order' << value.to_s
|
83
|
-
when :tile_size
|
84
|
-
options << '--set-output-j2k-tile-size' << value[0].to_s << value[1].to_s
|
85
|
-
when :codeblock_size
|
86
|
-
options << '--set-output-j2k-codeblock-size' << value[0].to_s << value[1].to_s
|
87
|
-
else # rubocop:disable Style/EmptyElse
|
88
|
-
# do nothing
|
88
|
+
#do nothing
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
92
|
+
|
93
|
+
Libis::Tools::Command.run(
|
94
|
+
Libis::Format::Config[:j2k_cmd],
|
95
|
+
'--input-file-name', source,
|
96
|
+
'--set-output-type', 'JP2',
|
97
|
+
*options,
|
98
|
+
'--output-file-name', target,
|
99
|
+
|
98
100
|
)
|
99
101
|
|
100
|
-
|
102
|
+
target
|
101
103
|
end
|
102
104
|
end
|
103
105
|
end
|
@@ -1,50 +1,56 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative 'base'
|
4
4
|
|
5
5
|
require 'libis/format/tool/office_to_pdf'
|
6
|
-
require 'libis/format/
|
6
|
+
require 'libis/format/library'
|
7
7
|
|
8
8
|
module Libis
|
9
9
|
module Format
|
10
10
|
module Converter
|
11
|
+
|
11
12
|
class OfficeConverter < Libis::Format::Converter::Base
|
13
|
+
|
12
14
|
def self.input_types
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
15
|
+
[
|
16
|
+
:TXT,
|
17
|
+
:RTF,
|
18
|
+
:HTML,
|
19
|
+
:MSDOC,
|
20
|
+
:MSDOT,
|
21
|
+
:MSXLS,
|
22
|
+
:MSPPT,
|
23
|
+
:MSDOCX,
|
24
|
+
:MSDOTX,
|
25
|
+
:MSXLSX,
|
26
|
+
:MSPPTX,
|
27
|
+
:WORDPERFECT,
|
28
|
+
:OO_WRITER,
|
29
|
+
:OO_IMPRESS,
|
30
|
+
:OO_CALC
|
29
31
|
]
|
30
32
|
end
|
31
33
|
|
32
34
|
def self.output_types(format = nil)
|
33
|
-
return [] unless input_types.include?(format)
|
34
|
-
|
35
|
+
return [] unless input_types.include?(format) if format
|
35
36
|
[:PDF]
|
36
37
|
end
|
37
38
|
|
38
39
|
def office_convert(_)
|
39
|
-
#
|
40
|
+
#force usage of this converter
|
40
41
|
end
|
41
42
|
|
42
43
|
def convert(source, target, format, opts = {})
|
43
44
|
super
|
44
45
|
|
45
|
-
Format::Tool::OfficeToPdf.run(source, target)
|
46
|
+
return nil unless Format::Tool::OfficeToPdf.run(source, target)
|
47
|
+
|
48
|
+
target
|
49
|
+
|
46
50
|
end
|
51
|
+
|
47
52
|
end
|
53
|
+
|
48
54
|
end
|
49
55
|
end
|
50
56
|
end
|
@@ -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
|
@@ -1,143 +1,52 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative 'base'
|
4
4
|
|
5
5
|
require 'libis/tools/extend/hash'
|
6
|
-
require 'libis/format/tool/
|
6
|
+
require 'libis/format/tool/pdf_copy'
|
7
7
|
require 'libis/format/tool/pdf_to_pdfa'
|
8
|
-
require 'libis/format/tool/pdfa_validator'
|
9
8
|
require 'libis/format/tool/pdf_optimizer'
|
10
9
|
|
11
10
|
module Libis
|
12
11
|
module Format
|
13
12
|
module Converter
|
13
|
+
|
14
|
+
# noinspection DuplicatedCode
|
14
15
|
class PdfConverter < Libis::Format::Converter::Base
|
16
|
+
|
15
17
|
def self.input_types
|
16
18
|
[:PDF]
|
17
19
|
end
|
18
20
|
|
19
21
|
def self.output_types(format = nil)
|
20
|
-
return [] unless input_types.include?(format)
|
21
|
-
|
22
|
-
%i[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 %i[title author creator keywords subject].include?(k)
|
43
|
-
(@options[:metadata] ||= {})[k] = v
|
44
|
-
end
|
22
|
+
return [] unless input_types.include?(format) if format
|
23
|
+
[:PDF, :PDFA]
|
45
24
|
end
|
46
25
|
|
47
|
-
|
48
|
-
|
49
|
-
def range(selection)
|
50
|
-
@options[:select] = {range: [selection].flatten.compact.join(',')}
|
26
|
+
def title(v)
|
27
|
+
@options[:title] = v.blank? ? nil : v
|
51
28
|
end
|
52
29
|
|
53
|
-
|
54
|
-
|
55
|
-
# The watermark options are (use symbols):
|
56
|
-
# - text: text to create a watermark from
|
57
|
-
# - file: watermark image to use
|
58
|
-
# - image: same as above
|
59
|
-
# - rotation: rotation of the watermark text (in degrees; integer number)
|
60
|
-
# - size: font size of the watermark text
|
61
|
-
# - opacity: opacity of the watermark (fraction 0.0 - 1.0)
|
62
|
-
# - gap: size of the gap between watermark instances. Integer value is absolute size in points (1/72 inch).
|
63
|
-
# Fractions are percentage of widht/height.
|
64
|
-
# If both options are given, the file will be used as-is if it exists and is a valid image file. Otherwise the
|
65
|
-
# file will be created or overwritten with a newly created watermark image.
|
66
|
-
#
|
67
|
-
# The created watermark file will be a PNG image with transparent background containing the supplied text
|
68
|
-
# slanted by 30 degrees counter-clockwise.
|
69
|
-
#
|
70
|
-
# @param [Hash] options Hash of options for watermark creation.
|
71
|
-
def watermark(options = {})
|
72
|
-
options.key_strings_to_symbols!
|
73
|
-
if options[:file] || options[:image]
|
74
|
-
watermark_image(options)
|
75
|
-
elsif options[:text]
|
76
|
-
watermark_text(options)
|
77
|
-
elsif options[:banner]
|
78
|
-
watermark_banner(options)
|
79
|
-
end
|
30
|
+
def author(v)
|
31
|
+
@options[:author] = v.blank? ? nil : v
|
80
32
|
end
|
81
33
|
|
82
|
-
def
|
83
|
-
options.
|
84
|
-
@options[:watermark] = {command: 'image'}
|
85
|
-
@options[:watermark][:data] = options[:file] || options[:image]
|
86
|
-
@options[:watermark][:opacity] = options[:opacity] || '0.3'
|
87
|
-
|
34
|
+
def creator(v)
|
35
|
+
@options[:creator] = v.blank? ? nil : v
|
88
36
|
end
|
89
37
|
|
90
|
-
def
|
91
|
-
options.
|
92
|
-
@options[:watermark] = {command: 'text'}
|
93
|
-
@options[:watermark][:data] = (options[:text] || '© LIBIS').split('\n')
|
94
|
-
@options[:watermark][:rotation] = options[:rotation] if options[:rotation]
|
95
|
-
@options[:watermark][:size] = options[:size] if options[:size]
|
96
|
-
@options[:watermark][:gap] = options[:gap] if options[:gap].to_s =~ /^\s*\d+\s*$/
|
97
|
-
@options[:watermark][:padding] = options[:gap] if options[:gap].to_s =~ /^\s*(0+\.\d+|1\.0+)\s*$/
|
98
|
-
@options[:watermark][:padding] = options[:padding] if options[:padding]
|
99
|
-
@options[:watermark][:opacity] = options[:opacity] || '0.3'
|
38
|
+
def keywords(v)
|
39
|
+
@options[:keywords] = v.blank? ? nil : v
|
100
40
|
end
|
101
41
|
|
102
|
-
|
103
|
-
|
104
|
-
# The banner options are:
|
105
|
-
# - banner: text to put in the banner
|
106
|
-
# - add_filename: append filename to the text (use any value to enable)
|
107
|
-
# - fontsize: size of the font (in points)
|
108
|
-
# - width: width of the banner
|
109
|
-
# - (background|text)_color_(red|green|blue): color components of background and text
|
110
|
-
def watermark_banner(options = {})
|
111
|
-
options.key_strings_to_symbols!
|
112
|
-
@options[:watermark] = {command: 'banner'}
|
113
|
-
@options[:watermark][:data] = (options[:banner] || '© LIBIS')
|
114
|
-
@options[:watermark][:add_filename] = !!options[:add_filename]
|
115
|
-
@options[:watermark][:size] = options[:fontsize] if options[:fontsize]
|
116
|
-
@options[:watermark][:width] = options[:width] if options[:width]
|
117
|
-
@options[:watermark][:background_red] = options[:background_color_red] if options[:background_color_red]
|
118
|
-
@options[:watermark][:background_green] = options[:background_color_green] if options[:background_color_green]
|
119
|
-
@options[:watermark][:background_blue] = options[:background_color_blue] if options[:background_color_blue]
|
120
|
-
@options[:watermark][:text_red] = options[:text_color_red] if options[:text_color_red]
|
121
|
-
@options[:watermark][:text_green] = options[:text_color_green] if options[:text_color_green]
|
122
|
-
@options[:watermark][:text_blue] = options[:text_color_blue] if options[:text_color_blue]
|
42
|
+
def subject(v)
|
43
|
+
@options[:subject] = v.blank? ? nil : v
|
123
44
|
end
|
124
45
|
|
125
|
-
#
|
126
|
-
#
|
127
|
-
|
128
|
-
|
129
|
-
#
|
130
|
-
# - 0 : lowest quality (Acrobat Distiller 'Screen Optimized' equivalent)
|
131
|
-
# - 1 : medium quality (Acrobat Distiller 'eBook' equivalent)
|
132
|
-
# - 2 : good quality (Acrobat Distiller 'Default' equivalent)
|
133
|
-
# - 3 : high quality (Acrobat Distiller 'Print Optimized' equivalent)
|
134
|
-
# - 4 : highest quality (Acrobat Distiller 'Prepress Optimized' equivalent)
|
135
|
-
#
|
136
|
-
# Note that the optimization is intended to be used with PDF's containing high-resolution images.
|
137
|
-
#
|
138
|
-
# @param [Integer] setting quality setting. [0-4]
|
139
|
-
def optimize(setting = 1)
|
140
|
-
@options[:optimize] = %w[screen ebook default printer prepress][setting] if (0..4).include?(setting)
|
46
|
+
# Select a partial list of pages
|
47
|
+
# @param [String] selection as described in com.itextpdf.text.pdf.SequenceList: [!][o][odd][e][even]start-end
|
48
|
+
def range(selection)
|
49
|
+
@options[:ranges] = selection.blank? ? nil : selection
|
141
50
|
end
|
142
51
|
|
143
52
|
def convert(source, target, format, opts = {})
|
@@ -147,122 +56,65 @@ module Libis
|
|
147
56
|
|
148
57
|
unless @options.empty?
|
149
58
|
result = convert_pdf(source, target)
|
59
|
+
return nil unless result
|
150
60
|
source = result
|
151
61
|
end
|
152
62
|
|
153
|
-
if
|
154
|
-
result = optimize_pdf(source, target, quality)
|
155
|
-
source = result
|
156
|
-
end
|
157
|
-
|
158
|
-
if source && (format == :PDFA)
|
63
|
+
if format == :PDFA and source
|
159
64
|
result = pdf_to_pdfa(source, target)
|
160
65
|
end
|
161
66
|
|
162
|
-
|
163
|
-
files: [result],
|
164
|
-
converter: self.class.name
|
165
|
-
}
|
166
|
-
end
|
167
|
-
|
168
|
-
protected
|
67
|
+
result
|
169
68
|
|
170
|
-
def optimize_pdf(source, target, quality)
|
171
|
-
using_temp(target) do |tmpname|
|
172
|
-
result = Libis::Format::Tool::PdfOptimizer.run(source, tmpname, quality)
|
173
|
-
unless result[:err].empty?
|
174
|
-
error("Pdf optimization encountered errors:\n%s", (result[:err] + result[:out]).join("\n"))
|
175
|
-
return nil
|
176
|
-
end
|
177
|
-
tmpname
|
178
|
-
end
|
179
69
|
end
|
180
70
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
def options_to_args(options)
|
190
|
-
options.map do |k, v|
|
191
|
-
key = "--#{k.to_s.tr_s('_', '-')}"
|
192
|
-
value = v
|
193
|
-
case value
|
194
|
-
when TrueClass
|
195
|
-
value = nil
|
196
|
-
when FalseClass
|
197
|
-
value = key = nil
|
198
|
-
when Array
|
199
|
-
value = value.map(&:to_s)
|
200
|
-
else
|
201
|
-
value = v.to_s
|
202
|
-
end
|
203
|
-
[key, value]
|
204
|
-
end.flatten.compact
|
205
|
-
end
|
206
|
-
|
207
|
-
def add_watermark(source, target, options)
|
208
|
-
command = options.delete(:command)
|
209
|
-
data = [options.delete(:data)].flatten.compact
|
210
|
-
args = data + options_to_args(options)
|
211
|
-
|
212
|
-
using_temp(target) do |tmpname|
|
213
|
-
result = Libis::Format::Tool::PdfTool.run(['watermark', command], source, tmpname, *args)
|
214
|
-
unless result[:err].empty?
|
215
|
-
error("Pdf watermarking encountered errors:\n%s", result[:err].join(join("\n")))
|
216
|
-
return nil
|
217
|
-
end
|
218
|
-
tmpname
|
219
|
-
end
|
220
|
-
end
|
71
|
+
OPTIONS_TABLE = {
|
72
|
+
title: 'md_title',
|
73
|
+
author: 'md_author',
|
74
|
+
creator: 'md_creator',
|
75
|
+
keywords: 'md_keywords',
|
76
|
+
subject: 'md_subject'
|
77
|
+
}
|
221
78
|
|
222
|
-
def
|
223
|
-
args = options_to_args(options)
|
79
|
+
def convert_pdf(source, target)
|
224
80
|
|
225
81
|
using_temp(target) do |tmpname|
|
226
|
-
result = Libis::Format::Tool::
|
82
|
+
result = Libis::Format::Tool::PdfCopy.run(
|
83
|
+
source, tmpname,
|
84
|
+
@options.map {|k, v|
|
85
|
+
if v.nil?
|
86
|
+
nil
|
87
|
+
else
|
88
|
+
k = OPTIONS_TABLE[k] || k
|
89
|
+
["--#{k}", (v.is_a?(Array) ? v : v.to_s)]
|
90
|
+
end}.compact.flatten
|
91
|
+
)
|
227
92
|
unless result[:err].empty?
|
228
|
-
error("Pdf
|
229
|
-
|
93
|
+
error("Pdf conversion encountered errors:\n%s", result[:err].join(join("\n")))
|
94
|
+
next nil
|
230
95
|
end
|
231
96
|
tmpname
|
232
97
|
end
|
233
|
-
end
|
234
98
|
|
235
|
-
def select_range(source, target, options)
|
236
|
-
args = options_to_args(options)
|
237
|
-
|
238
|
-
using_temp(target) do |tmpname|
|
239
|
-
result = Libis::Format::Tool::PdfTool.run('select', source, tmpname, *args)
|
240
|
-
unless result[:err].empty?
|
241
|
-
error("Pdf select encountered errors:\n%s", result[:err].join(join("\n")))
|
242
|
-
return nil
|
243
|
-
end
|
244
|
-
tmpname
|
245
|
-
end
|
246
99
|
end
|
247
100
|
|
248
101
|
def pdf_to_pdfa(source, target)
|
102
|
+
|
249
103
|
using_temp(target) do |tmpname|
|
250
104
|
result = Libis::Format::Tool::PdfToPdfa.run source, tmpname
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
return nil
|
105
|
+
if result[:status] != 0
|
106
|
+
error("Pdf/A conversion encountered errors:\n%s", result[:err].join("\n"))
|
107
|
+
next nil
|
255
108
|
else
|
256
|
-
|
257
|
-
if r[:status] != 0
|
258
|
-
error "Pdf/A file failed to validate with following errors:\n%s", (r[:err] || r[:out] || []).join("\n")
|
259
|
-
return nil
|
260
|
-
end
|
109
|
+
warn("Pdf/A conversion warnings:\n%s", result[:err].join("\n")) unless result[:err].empty?
|
261
110
|
end
|
262
111
|
tmpname
|
263
112
|
end
|
113
|
+
|
264
114
|
end
|
115
|
+
|
265
116
|
end
|
117
|
+
|
266
118
|
end
|
267
119
|
end
|
268
120
|
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'base'
|
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'
|
9
|
+
|
10
|
+
module Libis
|
11
|
+
module Format
|
12
|
+
module Converter
|
13
|
+
|
14
|
+
class PdfOptimizer < 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]
|
23
|
+
end
|
24
|
+
|
25
|
+
def pdf_optimize(_)
|
26
|
+
#force usage of this converter
|
27
|
+
end
|
28
|
+
|
29
|
+
# Optimize the PDF
|
30
|
+
#
|
31
|
+
# This reduces the graphics quality to a level in order to limit file size. This option relies on the
|
32
|
+
# presence of ghostscript and takes one argument: the quality level. It should be one of:
|
33
|
+
#
|
34
|
+
# - 0 : lowest quality (Acrobat Distiller 'Screen Optimized' equivalent)
|
35
|
+
# - 1 : medium quality (Acrobat Distiller 'eBook' equivalent)
|
36
|
+
# - 2 : good quality
|
37
|
+
# - 3 : high quality (Acrobat Distiller 'Print Optimized' equivalent)
|
38
|
+
# - 4 : highest quality (Acrobat Distiller 'Prepress Optimized' equivalent)
|
39
|
+
#
|
40
|
+
# Note that the optimization is intended to be used with PDF's containing high-resolution images.
|
41
|
+
#
|
42
|
+
# @param [Integer] setting quality setting. [0-4]
|
43
|
+
def quality(setting = 1)
|
44
|
+
@quality = %w(screen ebook default printer prepress)[setting] if (0..4) === setting
|
45
|
+
end
|
46
|
+
|
47
|
+
def convert(source, target, format, opts = {})
|
48
|
+
super
|
49
|
+
|
50
|
+
optimize_pdf(source, target, @quality || 'ebook')
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
def optimize_pdf(source, target, quality)
|
55
|
+
|
56
|
+
using_temp(target) do |tmpname|
|
57
|
+
result = Libis::Format::Tool::PdfOptimizer.run(source, tmpname, quality)
|
58
|
+
unless result[:status] == 0
|
59
|
+
error("Pdf optimization encountered errors:\n%s", (result[:err] + result[:out]).join("\n"))
|
60
|
+
next nil
|
61
|
+
end
|
62
|
+
tmpname
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|