Class: PdfCombinator
- Inherits:
-
Object
- Object
- PdfCombinator
- Defined in:
- lib/pdf_combinator.rb
Overview
Thin wrapper around HexaPDF for merging multiple PDFs into one
document. Used by the quote / invoice / RMA PDF generators that
stitch together a cover sheet, body, and per-attachment sections
before sending.
Defined Under Namespace
Classes: PdfPage
Instance Attribute Summary collapse
-
#pdf_doc ⇒ Object
readonly
Returns the value of attribute pdf_doc.
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(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.
- #fix_orientation(force_orientation = nil) ⇒ Object
-
#initialize(io: nil) ⇒ PdfCombinator
constructor
A new instance of PdfCombinator.
- #load(pdf_file_path) ⇒ Object
-
#number_pages(location: [:bottom_right], margin_from_height: -4,, font_size: 9) ⇒ Object
See https://github.com/gettalong/hexapdf/issues/197 Kwargs are part of the public API and Quote::CombinedPdfGenerator passes them by name; do not let UnusedMethodArgument prefix them with
_again. -
#pages ⇒ Object
Returns an array of pages in our decorator that is CombinedPdf alike.
- #save(path = nil) ⇒ Object
-
#to_pdf ⇒ Object
(also: #to_s)
Convert to a binary IO blob.
Constructor Details
#initialize(io: nil) ⇒ PdfCombinator
Returns a new instance of PdfCombinator.
11 12 13 |
# File 'lib/pdf_combinator.rb', line 11 def initialize(io: nil) @pdf_doc = HexaPDF::Document.new(io:) end |
Instance Attribute Details
#pdf_doc ⇒ Object (readonly)
Returns the value of attribute pdf_doc.
9 10 11 |
# File 'lib/pdf_combinator.rb', line 9 def pdf_doc @pdf_doc end |
Class Method Details
.load(pdf_file_path) ⇒ Object
15 16 17 18 |
# File 'lib/pdf_combinator.rb', line 15 def self.load(pdf_file_path) io = IO.new(IO.sysopen(pdf_file_path)) new(io:) end |
.parse(pdf_blob) ⇒ Object
20 21 22 |
# File 'lib/pdf_combinator.rb', line 20 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
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pdf_combinator.rb', line 29 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
73 74 75 76 77 78 79 |
# File 'lib/pdf_combinator.rb', line 73 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
41 42 43 44 |
# File 'lib/pdf_combinator.rb', line 41 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
See https://github.com/gettalong/hexapdf/issues/197
Kwargs are part of the public API and Quote::CombinedPdfGenerator passes
them by name; do not let UnusedMethodArgument prefix them with _ again.
84 85 86 87 88 89 90 91 92 |
# File 'lib/pdf_combinator.rb', line 84 def number_pages(location: [:bottom_right], margin_from_height: -4, font_size: 9) # rubocop:disable Lint/UnusedMethodArgument 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 |
#pages ⇒ Object
Returns an array of pages in our decorator that is CombinedPdf alike
69 70 71 |
# File 'lib/pdf_combinator.rb', line 69 def pages pdf_doc.pages.map { |p| PdfPage.new(p) } end |
#save(path = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pdf_combinator.rb', line 54 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_pdf ⇒ Object Also known as: to_s
Convert to a binary IO blob
47 48 49 50 51 |
# File 'lib/pdf_combinator.rb', line 47 def to_pdf output = StringIO.new(''.b) pdf_doc.write(output, optimize: true) output.string end |