Class: PdfCombinator

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf_combinator.rb

Defined Under Namespace

Classes: PdfPage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io: nil) ⇒ PdfCombinator

Returns a new instance of PdfCombinator.



6
7
8
# File 'lib/pdf_combinator.rb', line 6

def initialize(io: nil)
  @pdf_doc = HexaPDF::Document.new(io:)
end

Instance Attribute Details

#pdf_docObject (readonly)

Returns the value of attribute pdf_doc.



4
5
6
# File 'lib/pdf_combinator.rb', line 4

def pdf_doc
  @pdf_doc
end

Class Method Details

.load(pdf_file_path) ⇒ Object



10
11
12
13
# File 'lib/pdf_combinator.rb', line 10

def self.load(pdf_file_path)
  io = IO.new(IO.sysopen(pdf_file_path))
  new(io:)
end

.parse(pdf_blob) ⇒ Object



15
16
17
# File 'lib/pdf_combinator.rb', line 15

def self.parse(pdf_blob)
  new(io: StringIO.new(pdf_blob))
end

Instance Method Details

#<<(pdf_blob) ⇒ Object

Appends to the current Pdf doc, can be either:

  • a pdf data blob
  • a PdfPage from another PdfCombinator
  • another PdfCombinator object
    If you need to load up a file first. Use PdfCombinator.load(path) so you get a PdfCombinator object


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdf_combinator.rb', line 24

def <<(pdf_blob)
  if pdf_blob.is_a?(PdfPage)
    pdf_doc.pages << pdf_doc.import(pdf_blob.page)
  elsif pdf_blob.is_a?(PdfCombinator)
    pdf_blob.pages.each { |p| self << p }
  else
    new_pdf_io = StringIO.new(pdf_blob)
    new_pdf_doc = HexaPDF::Document.new(io: new_pdf_io)
    new_pdf_doc.pages.each { |new_page| pdf_doc.pages << pdf_doc.import(new_page) }
  end
end

#fix_orientation(force_orientation = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/pdf_combinator.rb', line 68

def fix_orientation(force_orientation = nil)
  return unless pdf_doc.pages.size > 1

  master_orientation = force_orientation || pages.first.orientation
  pages[1..].each { |p| p.orientation = master_orientation }
  master_orientation
end

#load(pdf_file_path) ⇒ Object



36
37
38
39
# File 'lib/pdf_combinator.rb', line 36

def load(pdf_file_path)
  new_pdf_doc = HexaPDF::Document.open(pdf_file_path)
  new_pdf_doc.pages.each { |new_page| pdf_doc.pages << pdf_doc.import(new_page) }
end

#number_pages(location: [:bottom_right], margin_from_height: -4,, font_size: 9) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/pdf_combinator.rb', line 77

def number_pages(location: [:bottom_right], margin_from_height: -4, font_size: 9)
  pdf_doc.pages.each do |page|
    box = page.box
    page.canvas(type: :overlay)
        .font('Helvetica', size: 9)
        .fill_color('000000')
        .text((page.index + 1).to_s, at: [box.width - 30, 15])
  end
end

#pagesObject

Returns an array of pages in our decorator that is CombinedPdf alike



64
65
66
# File 'lib/pdf_combinator.rb', line 64

def pages
  pdf_doc.pages.map { |p| PdfPage.new(p) }
end

#save(path = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pdf_combinator.rb', line 49

def save(path = nil)
  path = if path.present?
           path.to_s # Pathname becomes regular path otherwise error later on trying to set binmode
         else
           Tempfile.new(['output', '.pdf'], binmode: true).path
         end
  out = File.new(path, 'wb')
  pdf_doc.write(out, optimize: true)
  out.flush
  out.fsync
  out.close
  path
end

#to_pdfObject Also known as: to_s

Convert to a binary IO blob



42
43
44
45
46
# File 'lib/pdf_combinator.rb', line 42

def to_pdf
  output = StringIO.new(''.b)
  pdf_doc.write(output, optimize: true)
  output.string
end