Class: Libis::Format::Converter::PdfConverter
- Defined in:
- lib/libis/format/converter/pdf_converter.rb
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #convert(source, target, format, opts = {}) ⇒ Object
- #convert_pdf(source, target) ⇒ Object
-
#metadata(values = {}) ⇒ Object
Set metadata for Pdf file.
- #pdf_convert(_) ⇒ Object
- #pdf_to_pdfa(source, target) ⇒ Object
-
#range(selection) ⇒ Object
Select a partial list of pages.
-
#watermark(options = {}) ⇒ Object
Create or use a watermark image.
Methods inherited from Base
inherited, #initialize, #using_temp
Constructor Details
This class inherits a constructor from Libis::Format::Converter::Base
Class Method Details
.input_types(_ = nil) ⇒ Object
15 16 17 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 15 def self.input_types(_ = nil) [:PDF] end |
.output_types(_ = nil) ⇒ Object
19 20 21 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 19 def self.output_types(_ = nil) [:PDF, :PDFA] end |
Instance Method Details
#convert(source, target, format, opts = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 81 def convert(source, target, format, opts = {}) super result = nil 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 result end |
#convert_pdf(source, target) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 101 def convert_pdf(source, target) using_temp(target) do |tmpname| result = Libis::Format::PdfCopy.run( source, tmpname, @options.map { |k, v| if v.nil? nil else ["--#{k}", (v.is_a?(Array) ? v : v.to_s)] end }.flatten ) result[:err].empty? ? target : begin error("Pdf conversion encountered errors:\n%s", result[:err].join('\n')) nil end tmpname end end |
#metadata(values = {}) ⇒ Object
Set metadata for Pdf file
valid metadata keys are):
- title
-
- creator
- keywords
- subject
37 38 39 40 41 42 43 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 37 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 |
#pdf_convert(_) ⇒ Object
23 24 25 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 23 def pdf_convert(_) #force usage of this converter end |
#pdf_to_pdfa(source, target) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 122 def pdf_to_pdfa(source, target) using_temp(target) do |tmpname| result = Libis::Format::PdfToPdfa.run source, tmpname result[:status] == 0 ? target : begin error("Pdf/A conversion encountered errors:\n%s", result[:err].join('\n')) nil end tmpname end end |
#range(selection) ⇒ Object
Select a partial list of pages
47 48 49 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 47 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.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/libis/format/converter/pdf_converter.rb', line 67 def watermark( = {}) .key_strings_to_symbols! if [:file] && File.exist?([:file]) @options['wm_image'] = [:file] else @options['wm_text'] = ([:text] || '© LIBIS').split('\n') @options['wm_text_rotation'] = [:rotation] if [:rotation] @options['wm_font_size'] = [:size] if [:size] end @options['wm_opacity'] = [:opacity] || '0.3' @options['wm_gap_ratio'] = [:gap] if [:gap].to_s =~ /^\s*(0+\.\d+|1\.0+)\s*$/ @options['wm_gap_size'] = [:gap] if [:gap].to_s =~ /^\s*\d+\s*$/ end |