lib/libis/format/converter/pdf_converter.rb in libis-format-1.2.9 vs lib/libis/format/converter/pdf_converter.rb in libis-format-1.3.0

- old
+ new

@@ -1,6 +1,6 @@ -# encoding: utf-8 +# frozen_string_literal: true require_relative 'base' require 'libis/tools/extend/hash' require 'libis/format/tool/pdf_copy' @@ -9,24 +9,23 @@ require 'libis/format/tool/pdf_optimizer' module Libis module Format module Converter - class PdfConverter < Libis::Format::Converter::Base - def self.input_types [:PDF] end def self.output_types(format = nil) return [] unless input_types.include?(format) - [:PDF, :PDFA] + + %i[PDF PDFA] end def pdf_convert(_) - #force usage of this converter + # force usage of this converter end # Set metadata for Pdf file # # valid metadata keys are): @@ -38,11 +37,12 @@ # # @param [Hash] values list of metadata values to set def metadata(values = {}) values.key_strings_to_symbols! values.each do |k, v| - next unless [:title, :author, :creator, :keywords, :subject].include?(k) + next unless %i[title author creator keywords subject].include?(k) + @options["md_#{k}"] = v end end # Select a partial list of pages @@ -57,11 +57,12 @@ # - 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. + # - 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. @@ -94,76 +95,72 @@ # # Note that the optimization is intended to be used with PDF's containing high-resolution images. # # @param [Integer] setting quality setting. [0-4] def optimize(setting = 1) - @options['optimize'] = %w(screen ebook default printer prepress)[setting] if (0..4) === setting + @options['optimize'] = %w[screen ebook default printer prepress][setting] if (0..4).include?(setting) end 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 + result = pdf_to_pdfa(source, target) if (format == :PDFA) && source - { + { files: [result], converter: self.class.name } - end def optimize_pdf(source, target, quality) - using_temp(target) do |tmpname| result = Libis::Format::Tool::PdfOptimizer.run(source, tmpname, quality) - unless result[:status] == 0 + unless (result[:status]).zero? error("Pdf optimization encountered errors:\n%s", (result[:err] + result[:out]).join("\n")) next nil end tmpname end end 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 + source, tmpname, + @options.map do |k, v| + if v.nil? + nil + else + ["--#{k}", (v.is_a?(Array) ? v : v.to_s)] + end + end.flatten ) unless result[:err].empty? error("Pdf conversion encountered errors:\n%s", result[:err].join(join("\n"))) next nil end tmpname end - end 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")) @@ -175,13 +172,10 @@ next nil end end tmpname end - end - end - end end end