Class: RailsCodeAuditor::PdfGenerator

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

Constant Summary collapse

OUTPUT_PATH =
"code_audit_report.pdf"
TEMP_PRWAN_PDF =
"tmp/main_audit.pdf"

Class Method Summary collapse

Class Method Details

.generate(results, scores, graphs, html_pdf_paths = []) ⇒ Object



12
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rails_code_auditor/pdf_generator.rb', line 12

def self.generate(results, scores, graphs, html_pdf_paths = [])
  FileUtils.mkdir_p("tmp")
  FileUtils.mkdir_p("report/pdf")

  # Generate main Prawn PDF
  Prawn::Document.generate(TEMP_PRWAN_PDF) do |pdf|
    pdf.text "Rails Code Audit Report", size: 24, style: :bold, align: :center
    pdf.move_down 20

    # Summary Scores Table
    pdf.text "Audit Summary Scores", size: 16, style: :bold
    pdf.move_down 10
    summary_data = [["Metric", "Score (0-100)", "Remarks"]]
    scores.each do |metric, value|
      summary_data << [metric.to_s.humanize, value[:score], value[:remark]]
    end
    pdf.table(summary_data, header: true, width: pdf.bounds.width)
    pdf.move_down 20

    # Detailed Audit Results
    pdf.text "Detailed Audit Results", size: 16, style: :bold
    pdf.move_down 10
    results.each do |check_name, result|
      pdf.text check_name.to_s.humanize, size: 12, style: :bold
      pdf.text "Status: #{result[:status]}"
      pdf.text "Details: #{result[:details]}"
      pdf.move_down 10
    end

    # Graphs (optional)
    if graphs && graphs.any?
      pdf.start_new_page
      pdf.text "Visual Graphs", size: 16, style: :bold
      pdf.move_down 10

      graphs.each do |graph|
        if File.exist?(graph[:path])
          pdf.text graph[:title], size: 12, style: :bold
          pdf.image graph[:path], fit: [500, 300]
          pdf.move_down 20
        else
          puts "[!] Graph file missing: #{graph[:path]}"
        end
      end
    end

    pdf.number_pages "Page <page> of <total>", at: [pdf.bounds.right - 150, 0]
  end

  puts "[✓] Main audit PDF saved to #{TEMP_PRWAN_PDF}"

  # Merge all PDFs (main + html converted)
  combined_pdf = CombinePDF.new
  combined_pdf << CombinePDF.load(TEMP_PRWAN_PDF)

  html_pdf_paths.each do |pdf_path|
    if File.exist?(pdf_path)
      puts "[+] Merging #{pdf_path}"
      combined_pdf << CombinePDF.load(pdf_path)
    else
      puts "[!] Skipped missing HTML-generated PDF: #{pdf_path}"
    end
  end

  combined_pdf.save(OUTPUT_PATH)
  puts "[✓] Final merged PDF saved to #{OUTPUT_PATH}"
end