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
-
.load(pdf_file_path) ⇒ PdfCombinator
A combinator loaded from the file.
-
.parse(pdf_blob) ⇒ PdfCombinator
A combinator wrapping the blob.
Instance Method Summary collapse
-
#<<(pdf_blob) ⇒ void
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) ⇒ Symbol?
The orientation applied to the pages.
-
#initialize(io: nil) ⇒ PdfCombinator
constructor
A new instance of PdfCombinator.
-
#load(pdf_file_path) ⇒ void
Appends the file's pages to the document.
-
#number_pages(location: [:bottom_right], margin_from_height: -4,, font_size: 9) ⇒ void
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 ⇒ Array<PdfPage>
Returns an array of pages in our decorator that is CombinedPdf alike.
-
#save(path = nil) ⇒ String
The path the PDF was written to.
-
#to_pdf ⇒ String
(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) ⇒ PdfCombinator
Returns a combinator loaded from the file.
16 17 18 19 |
# File 'lib/pdf_combinator.rb', line 16 def self.load(pdf_file_path) io = IO.new(IO.sysopen(pdf_file_path)) new(io:) end |
.parse(pdf_blob) ⇒ PdfCombinator
Returns a combinator wrapping the blob.
22 23 24 |
# File 'lib/pdf_combinator.rb', line 22 def self.parse(pdf_blob) new(io: StringIO.new(pdf_blob)) end |
Instance Method Details
#<<(pdf_blob) ⇒ void
This method returns an undefined value.
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
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pdf_combinator.rb', line 32 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) ⇒ Symbol?
Returns the orientation applied to the pages.
81 82 83 84 85 86 87 |
# File 'lib/pdf_combinator.rb', line 81 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) ⇒ void
This method returns an undefined value.
Returns appends the file's pages to the document.
45 46 47 48 |
# File 'lib/pdf_combinator.rb', line 45 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) ⇒ void
This method returns an undefined value.
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.
93 94 95 96 97 98 99 100 101 |
# File 'lib/pdf_combinator.rb', line 93 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 ⇒ Array<PdfPage>
Returns an array of pages in our decorator that is CombinedPdf alike
76 77 78 |
# File 'lib/pdf_combinator.rb', line 76 def pages pdf_doc.pages.map { |p| PdfPage.new(p) } end |
#save(path = nil) ⇒ String
Returns the path the PDF was written to.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pdf_combinator.rb', line 60 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 ⇒ String Also known as: to_s
Convert to a binary IO blob
52 53 54 55 56 |
# File 'lib/pdf_combinator.rb', line 52 def to_pdf output = StringIO.new(''.b) pdf_doc.write(output, optimize: true) output.string end |