Sha256: 4513dd0b468fdcca8c43c1f0cc24257e808660708537edcffba45a760fe583f2

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'open3'

module NewspaperWorks
  class PDFDerivativeService < NewspaperPageDerivativeService
    TARGET_EXT = '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

    # graphicsmagick prefix, may be needed for jp2 source on Ubuntu
    GM_PREFX = 'gm '.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
      cmd = format(template, source_file: @source_path, out_file: @dest_path)
      @source_path.ends_with?('jp2') ? GM_PREFIX + cmd : cmd
    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 imagemagick or graphicsmagick command
      `#{convert_cmd}`
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
newspaper_works-0.1.0 app/services/newspaper_works/pdf_derivative_service.rb