Class: Libis::Format::Converter::PdfConverter

Inherits:
Base
  • Object
show all
Defined in:
lib/libis/format/converter/pdf_converter.rb

Instance Attribute Summary

Attributes inherited from Base

#flags, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #using_temp

Constructor Details

This class inherits a constructor from Libis::Format::Converter::Base

Class Method Details

.input_types ⇒ Object



17
18
19
# File 'lib/libis/format/converter/pdf_converter.rb', line 17

def self.input_types
  [:PDF]
end

.output_types(format = nil) ⇒ Object



21
22
23
24
# File 'lib/libis/format/converter/pdf_converter.rb', line 21

def self.output_types(format = nil)
  return [] unless input_types.include?(format)
  [:PDF, :PDFA]
end

Instance Method Details

#convert(source, target, format, opts = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/libis/format/converter/pdf_converter.rb', line 102

def convert(source, target, format, opts = {})
  super

  result = nil

  if (quality = @options.delete('optimize'))
    result = optimize_pdf(source, target, quality)
    return nil unless result
    source = result
  end

  unless @options.empty?
    result = convert_pdf(source, target)
    return nil unless result
    source = result
  end

  if format == :PDFA and source
    result = pdf_to_pdfa(source, target)
  end

  { 
    files: [result],
    converter: self.class.name
  }

end

#convert_pdf(source, target) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/libis/format/converter/pdf_converter.rb', line 142

def convert_pdf(source, target)

  using_temp(target) do |tmpname|
    result = Libis::Format::Tool::PdfCopy.run(
        source, tmpname,
        @options.map {|k, v|
          if v.nil?
            nil
          else
            ["--#{k}", (v.is_a?(Array) ? v : v.to_s)]
          end}.flatten
    )
    unless result[:err].empty?
      error("Pdf conversion encountered errors:\n%s", result[:err].join(join("\n")))
      next nil
    end
    tmpname
  end

end

#metadata(values = {}) ⇒ Object

Set metadata for Pdf file

valid metadata keys are): - title - author - creator - keywords - subject

Parameters:

  • values (Hash) (defaults to: {}) —

    list of metadata values to set



40
41
42
43
44
45
46
# File 'lib/libis/format/converter/pdf_converter.rb', line 40

def (values = {})
  values.key_strings_to_symbols!
  values.each do |k, v|
    next unless [:title, :author, :creator, :keywords, :subject].include?(k)
    @options["md_#{k}"] = v
  end
end

#optimize(setting = 1) ⇒ Object

Optimize the PDF

This reduces the graphics quality to a level in order to limit file size. This option relies on the presence of ghostscript and takes one argument: the quality level. It should be one of:

  • 0 : lowest quality (Acrobat Distiller 'Screen Optimized' equivalent)
  • 1 : medium quality (Acrobat Distiller 'eBook' equivalent)
  • 2 : good quality
  • 3 : high quality (Acrobat Distiller 'Print Optimized' equivalent)
  • 4 : highest quality (Acrobat Distiller 'Prepress Optimized' equivalent)

Note that the optimization is intended to be used with PDF's containing high-resolution images.

Parameters:

  • setting (Integer) (defaults to: 1) —

    quality setting. [0-4]



98
99
100
# File 'lib/libis/format/converter/pdf_converter.rb', line 98

def optimize(setting = 1)
  @options['optimize'] = %w(screen ebook default printer prepress)[setting] if (0..4) === setting
end

#optimize_pdf(source, target, quality) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/libis/format/converter/pdf_converter.rb', line 130

def optimize_pdf(source, target, quality)

  using_temp(target) do |tmpname|
    result = Libis::Format::Tool::PdfOptimizer.run(source, tmpname, quality)
    unless result[:status] == 0
      error("Pdf optimization encountered errors:\n%s", (result[:err] + result[:out]).join("\n"))
      next nil
    end
    tmpname
  end
end

#pdf_convert(_) ⇒ Object



26
27
28
# File 'lib/libis/format/converter/pdf_converter.rb', line 26

def pdf_convert(_)
  #force usage of this converter
end

#pdf_to_pdfa(source, target) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/libis/format/converter/pdf_converter.rb', line 163

def pdf_to_pdfa(source, target)

  using_temp(target) do |tmpname|
    result = Libis::Format::Tool::PdfToPdfa.run source, tmpname

    if result[:command][:status] != 0
      error("Pdf/A conversion encountered errors:\n%s", (result[:command][:out] + result[:command][:err]).join("\n"))
      next nil
    else
      r = Libis::Format::Tool::PdfaValidator.run tmpname
      if r[:status] != 0
        error "Pdf/A file failed to validate with following errors:\n%s", (r[:err] || r[:out] || []).join("\n")
        next nil
      end
    end
    tmpname
  end

end

#range(selection) ⇒ Object

Select a partial list of pages

Parameters:

  • selection (String) —

    as described in com.itextpdf.text.pdf.SequenceList: [!][o][odd][e][even]start-end



50
51
52
# File 'lib/libis/format/converter/pdf_converter.rb', line 50

def range(selection)
  @options[:ranges] = selection
end

#watermark(options = {}) ⇒ Object

Create or use a watermark image.

The watermark options are (use symbols): - text: text to create a watermark from - file: watermark image to use - rotation: rotation of the watermark text (in degrees; integer number) - size: font size of the watermark text - opacity: opacity of the watermark (fraction 0.0 - 1.0) - gap: size of the gap between watermark instances. Integer value is absolute size in points (1/72 inch). Fractions are percentage of widht/height. If both options are given, the file will be used as-is if it exists and is a valid image file. Otherwise the file will be created or overwritten with a newly created watermark image.

The created watermark file will be a PNG image with transparent background containing the supplied text slanted by 30 degrees counter-clockwise.

Parameters:

  • options (Hash) (defaults to: {}) —

    Hash of options for watermark creation.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/libis/format/converter/pdf_converter.rb', line 70

def watermark(options = {})
  options.key_strings_to_symbols!
  if options[:file] && File.exist?(options[:file])
    @options['wm_image'] = options[:file]
  else
    @options['wm_text'] = (options[:text] || '© LIBIS').split('\n')
    @options['wm_text_rotation'] = options[:rotation] if options[:rotation]
    @options['wm_font_size'] = options[:size] if options[:size]
  end
  @options['wm_opacity'] = options[:opacity] || '0.3'
  @options['wm_gap_ratio'] = options[:gap] if options[:gap].to_s =~ /^\s*(0+\.\d+|1\.0+)\s*$/
  @options['wm_gap_size'] = options[:gap] if options[:gap].to_s =~ /^\s*\d+\s*$/
end