Module: Pdfzus::Zusammenfuegen

Defined in:
lib/pdfzus/zusammenfuegen.rb,
lib/pdfzus/zusammenfuegen/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.merge_pdfs(inputs:, output:) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/pdfzus/zusammenfuegen.rb', line 13

def merge_pdfs(inputs:, output:)
  raise ArgumentError, "Mindestens 2 Eingabe-PDFs sind erforderlich." if inputs.length < 2
  raise ArgumentError, "Output muss auf .pdf enden." unless output.downcase.end_with?(".pdf")

  normalized_inputs = inputs.map do |raw|
    path = File.expand_path(raw)
    raise ArgumentError, "Datei nicht gefunden: #{raw}" unless File.file?(path)
    raise ArgumentError, "Keine PDF-Datei: #{raw}" unless path.downcase.end_with?(".pdf")

    path
  end

  output_path = File.expand_path(output)
  FileUtils.mkdir_p(File.dirname(output_path))

  merged = CombinePDF.new
  normalized_inputs.each do |pdf_path|
    merged << CombinePDF.load(pdf_path)
  rescue StandardError => e
    raise Error, "PDF konnte nicht gelesen werden (#{pdf_path}): #{e.message}"
  end

  merged.save(output_path)
  output_path
end