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



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
# File 'lib/dragonfly_pdf/processors/convert.rb', line 9

def call(content, page, geometry=nil, options = {})
  options = options.deep_symbolize_keys
  format = options.fetch(:format, :png)

  case format.to_sym
  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, input_options)

    dimensions = case geometry
    when DragonflyLibvips::Processors::Thumb::RESIZE_GEOMETRY then 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.to_s
  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



58
59
60
61
62
63
# File 'lib/dragonfly_pdf/processors/convert.rb', line 58

def update_url(attrs, page, geometry=nil, options = {})
  options = options.deep_symbolize_keys
  format = options.fetch(:format, :png)

  attrs.ext = format.to_s
end