Class: Shipping::PreProcessLabel
- Inherits:
-
BaseService
- Object
- BaseService
- Shipping::PreProcessLabel
- Defined in:
- app/services/shipping/pre_process_label.rb
Instance Method Summary collapse
- #process(pdf_file_path, out_file_name: nil) ⇒ Object
-
#process_image(img) ⇒ Object
Resize to fill 1224x1584, then crop to 672x1008 from center https://www.imagemagick.org/script/command-line-processing.php#geometry.
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Method Details
#process(pdf_file_path, out_file_name: nil) ⇒ Object
4 5 6 7 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 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 |
# File 'app/services/shipping/pre_process_label.rb', line 4 def process(pdf_file_path, out_file_name: nil) require 'vips' = pdf_file_path.split('/').last.split('.pdf').first pdf_directory = pdf_file_path.split().first # Get number of pages in PDF # Load first page to check if multi-page first_page = Vips::Image.new_from_file(pdf_file_path, dpi: 144, page: 0, access: :sequential) # Try to get page count from PDF loader page_count = begin Vips::Image.new_from_file(pdf_file_path, dpi: 144, n: -1).get('n-pages') rescue StandardError 1 end combined_pdf = PdfCombinator.new files_to_rm = [] page_count.times do |i| img_base_name = "#{pdf_directory}#{}_tmp_#{i}" tmp_png_path = "#{img_base_name}.png" tmp_pdf_path = "#{img_base_name}.pdf" # Load specific page at 144 DPI page_image = Vips::Image.new_from_file(pdf_file_path, dpi: 144, page: i) # Flatten alpha with white background if page_image.has_alpha? page_image = page_image.flatten(background: [255, 255, 255]) end # Process image: resize to 1224x1584, then crop to 672x1008 page_image = process_image(page_image) # Write as 8-bit PNG page_image.write_to_file(tmp_png_path, palette: true) files_to_rm << tmp_png_path # Convert PNG to PDF at 4x6 inches # Load the PNG and write as PDF png_image = Vips::Image.new_from_file(tmp_png_path) png_image.write_to_file(tmp_pdf_path) files_to_rm << tmp_pdf_path combined_pdf << PdfCombinator.load(tmp_pdf_path) end out_pdf_file_path = "#{pdf_directory}#{}_4x6.pdf" combined_pdf.save out_pdf_file_path # Cleanup temporary files (commented out in original) # files_to_rm.each { |tmp_path| FileUtils.rm(tmp_path) } out_pdf_file_path end |
#process_image(img) ⇒ Object
Resize to fill 1224x1584, then crop to 672x1008 from center
https://www.imagemagick.org/script/command-line-processing.php#geometry
64 65 66 67 68 69 70 71 72 73 |
# File 'app/services/shipping/pre_process_label.rb', line 64 def process_image(img) # Resize to cover 1224x1584 (^ modifier in ImageMagick means "fill") scale = [1224.0 / img.width, 1584.0 / img.height].max img = img.resize(scale) # Crop to 672x1008 from center left = [(img.width - 672) / 2, 0].max top = [(img.height - 1008) / 2, 0].max img.crop(left.to_i, top.to_i, [672, img.width].min, [1008, img.height].min) end |