Sha256: a38f8b180b1120af37b08d6196ec17f3b17bc7bc8da6640afd06232d1cd43263

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'open3'

module IiifPrint
  class PDFDerivativeService < BaseDerivativeService
    self.target_extension = 'pdf'.freeze

    # PDF (JPEG, 8 bit grayscale), 150ppi
    GRAY_PDF_CMD = 'convert %<source_file>s ' \
                   '-resize 1800 -density 150 ' \
                   '-depth 8 -colorspace Gray ' \
                   '-compress jpeg %<out_file>s'.freeze

    # sRBG color PDF (JPEG, 8 bits per channel), 150ppi
    COLOR_PDF_CMD = 'convert %<source_file>s ' \
                    '-resize 1800 -density 150 ' \
                    '-depth 8 ' \
                    '-compress jpeg %<out_file>s'.freeze

    def initialize(file_set)
      super(file_set)
    end

    # Get conversion command; command varies on whether or not we have
    #   JP2 source, and whether we have color or grayscale material.
    def convert_cmd
      template = use_color? ? COLOR_PDF_CMD : GRAY_PDF_CMD
      data = format(template, source_file: @source_path, out_file: @dest_path)
      IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: file_set.id.to_s, container: 'service_file', mime_type: mime_type_for(target_extension) })
      data
    end

    def create_derivatives(filename)
      # Base class takes care of loading @source_path, @dest_path
      super(filename)

      # no creation if pdf master
      return if mime_type == 'application/pdf'

      # Get and run conversion command
      return jp2_convert if mime_type == 'image/jp2'
      im_convert
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
iiif_print-3.0.1 lib/iiif_print/pdf_derivative_service.rb
iiif_print-3.0.0 lib/iiif_print/pdf_derivative_service.rb
iiif_print-2.0.1 lib/iiif_print/pdf_derivative_service.rb
iiif_print-2.0.0 lib/iiif_print/pdf_derivative_service.rb