Class: Shipping::PreProcessCanparLabel

Inherits:
Object
  • Object
show all
Defined in:
app/services/shipping/pre_process_canpar_label.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.testObject



35
36
37
38
# File 'app/services/shipping/pre_process_canpar_label.rb', line 35

def self.test
  path = Rails.root.join('extras', 'samples', 'shipping_labels', 'canpar_test_label.pdf')
  new.process(path)
end

Instance Method Details

#process(original_file_path, out_file_path: nil) ⇒ Object

This achieves the same as:
convert -density 300 canpar_test_label.pdf -crop "1900x2850+0+0" canpar_test_label.png
Note that the +0+0 prevents the multi page output



8
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
# File 'app/services/shipping/pre_process_canpar_label.rb', line 8

def process(original_file_path, out_file_path: nil)
  require 'vips'

  directory_path = File.dirname(original_file_path)
  base_name = File.basename(original_file_path, '.pdf')
  out_file_path ||= "#{directory_path}/#{base_name}_out.pdf"

  # Load PDF at 150 DPI (matching the original density)
  image = Vips::Image.new_from_file(original_file_path, dpi: 150, page: 0)

  # Crop to 950x1425 from top-left corner (equivalent to crop('950x1425+0+0'))
  image = image.crop(0, 0, [950, image.width].min, [1425, image.height].min)

  # Flatten alpha channel with white background
  if image.has_alpha?
    image = image.flatten(background: [255, 255, 255])
  end

  # Resize to 612x918 (letter size at 72 DPI)
  image = image.thumbnail_image(612, height: 918, size: :force)

  # Write as PDF
  image.write_to_file(out_file_path)

  out_file_path
end