Class: PrmGenerator::PdfGenerator

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/udb/prm_generator.rb

Overview

Handles PDF generation from AsciiDoc

Instance Method Summary collapse

Constructor Details

#initialize(template_dir, root_dir) ⇒ PdfGenerator

Returns a new instance of PdfGenerator.



652
653
654
655
# File 'lib/udb/prm_generator.rb', line 652

def initialize(template_dir, root_dir)
  @template_dir = Pathname.new(template_dir)
  @root_dir = Pathname.new(root_dir)
end

Instance Method Details

#generate(adoc_path, pdf_path) ⇒ Object



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/udb/prm_generator.rb', line 657

def generate(adoc_path, pdf_path)
  puts "[INFO] Generating PDF: #{pdf_path}"

  # Ensure output directory exists
  FileUtils.mkdir_p(File.dirname(pdf_path))

  cmd = build_asciidoctor_command(adoc_path, pdf_path)

  puts "[INFO] Running command: #{cmd.join(' ')}"

  success = T.let(nil, T.nilable(T::Boolean))
  output = ""
  error_output = ""

  Dir.chdir(@root_dir) do
    # Capture both stdout and stderr for better error reporting
    Open3.popen3(cmd.join(" ")) do |stdin, stdout, stderr, wait_thr|
      stdin.close
      output = stdout.read
      error_output = stderr.read
      success = T.cast(wait_thr.value, Process::Status).success?
    end
  end

  # Log output for debugging
  unless output.empty?
    puts "[INFO] AsciiDoctor output:"
    puts output
  end

  unless error_output.empty?
    if success
      puts "[WARN] AsciiDoctor warnings:"
    else
      puts "[ERROR] AsciiDoctor errors:"
    end
    puts error_output
  end

  if success
    puts "[INFO] Successfully generated PDF: #{pdf_path}"
  else
    raise GenerationError, "Failed to generate PDF. Command: #{cmd.join(' ')}\nError output: #{error_output}"
  end
end