Class: Pdf::Utility::PageSplitter

Inherits:
Object
  • Object
show all
Defined in:
app/services/pdf/utility/page_splitter.rb

Overview

Splits a multi-page PDF (fetched from a URL) into an array of single-page
label hashes, following the convention used by shipment label arrays.

Used by Shipping::ShipengineBase to handle carriers (e.g. Canpar) that
return all package labels as a single multi-page PDF asset.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.split_from_url(url) ⇒ Object

Fetches a PDF from +url+, splits it page-by-page, and returns an Array of
hashes with +:image+ (Tempfile) entries — one per page.



11
12
13
# File 'app/services/pdf/utility/page_splitter.rb', line 11

def self.split_from_url(url)
  new.split_from_url(url)
end

Instance Method Details

#split_from_url(url) ⇒ Object



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
# File 'app/services/pdf/utility/page_splitter.rb', line 15

def split_from_url(url)
  Pdf::Loader.load!

  label_arr = []
  binary_string = HTTP.get(url).body.to_s
  file = StringIO.new(binary_string)
  pdf  = HexaPDF::Document.new(io: file)

  pdf.pages.each_with_index do |page, ind|
    Rails.logger.debug { "PageSplitter splitting page #{ind} from #{url}" }

    new_pdf   = HexaPDF::Document.new
    new_pdf.pages << new_pdf.import(page)

    file_path = Rails.application.config.x.temp_storage_path.join("label_pdf_#{ind}.pdf")
    out = File.new(file_path, 'wb')
    new_pdf.write(out, optimize: true)
    out.flush
    out.close

    label         = {}
    label[:image] = Tempfile.new('shipping_label.pdf')
    label[:image].binmode
    label[:image] << File.read(file_path)
    label[:image].rewind
    label[:image].flush
    label[:image].fsync

    label_arr << label
  end

  label_arr
end