Class: DragonflyPdf::Processors::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly_pdf/processors/convert.rb

Constant Summary collapse

DPI =
600

Instance Method Summary collapse

Instance Method Details

#call(content, page, geometry = nil, options = {}) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dragonfly_pdf/processors/convert.rb', line 9

def call(content, page, geometry = nil, options = {})
  raise UnsupportedFormat unless content.ext
  raise UnsupportedFormat unless SUPPORTED_FORMATS.include?(content.ext.downcase)

  options = DragonflyPdf.stringify_keys(options)
  format = options.fetch('format', 'png').to_s

  raise UnsupportedOutputFormat unless SUPPORTED_OUTPUT_FORMATS.include?(format.downcase)

  case format
  when 'pdf'
    pdf_options = options.fetch('pdf_options', 'compress,compress-fonts,compress-images,linearize,sanitize,garbage=deduplicate')

    content.shell_update(ext: format) do |old_path, new_path|
      "#{convert_command} -o \"#{new_path}\" -F #{format} -O #{pdf_options} \"#{old_path}\" #{page}"
    end

  when 'svg'
    text = options.fetch('text', 'path')

    content.shell_update(ext: format) do |old_path, new_path|
      "#{convert_command} -o \"#{new_path}\" -F #{format} -O text=#{text} \"#{old_path}\" #{page}"
    end

    # MuPDF appends 1 at the end of the filename, we need to rename it back
    `mv #{content.path.sub(".#{format}", "1.#{format}")} #{content.path}`

  else
    input_options = options.fetch('input_options', {})
    input_options['access'] = input_options.fetch('access', 'sequential')
    input_options['dpi'] = input_options.fetch('dpi', DPI)

    img = ::Vips::Image.new_from_file(content.path, DragonflyPdf.symbolize_keys(input_options))

    dimensions = case geometry
                 when DragonflyLibvips::Processors::Thumb::RESIZE_GEOMETRY
                   DragonflyLibvips::Dimensions.call(geometry, img.width, img.height)
                 else
                   raise ArgumentError, "Didn't recognise the geometry string: #{geometry}"
                 end

    width = dimensions.width.ceil

    content.shell_update(ext: format) do |old_path, new_path|
      "#{convert_command} -o \"#{new_path}\" -F #{format} -O width=#{width},colorspace=rgb \"#{old_path}\" #{page}"
    end

    # MuPDF appends 1 at the end of the filename, we need to rename it back
    `mv \"#{content.path.sub(".#{format}", "1.#{format}")}\" \"#{content.path}\"`
  end

  content.meta['format'] = format
  content.ext = format
  content.meta['mime_type'] = nil # don't need it as we have ext now
end

#update_url(attrs, _page, _geometry = nil, options = {}) ⇒ Object



65
66
67
68
69
# File 'lib/dragonfly_pdf/processors/convert.rb', line 65

def update_url(attrs, _page, _geometry = nil, options = {})
  options = options.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v } # stringify keys
  format = options.fetch('format', 'png').to_s
  attrs.ext = format
end