Class: RailsCodeAuditor::HtmlToPdfConverter

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

Constant Summary collapse

INPUT_PATHS =
["report", "report/rubycritic"]
OUTPUT_PATH =
"report/pdf"

Class Method Summary collapse

Class Method Details

.convert_allObject



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

def self.convert_all
  unless puppeteer_installed?
    puts "[!] Puppeteer is not installed. Please run: yarn add puppeteer"
    return []
  end

  FileUtils.mkdir_p(OUTPUT_PATH)
  pdf_paths = []

  html_files = INPUT_PATHS.flat_map { |path| Dir["#{path}/*.html"] }

  if html_files.empty?
    puts "[!] No HTML files found in #{INPUT_PATHS.join(', ')}"
    return []
  end

  html_files.each do |html_path|
    begin
      relative_name = html_path.sub(%r{^.*?report/}, "").gsub("/", "_")
      pdf_filename = relative_name.sub(/\.html$/, ".pdf")
      pdf_output_path = File.join(OUTPUT_PATH, pdf_filename)

      html_content = File.read(html_path)

      grover = Grover.new(
        html_content,
        print_background: true,
        prefer_css_page_size: true,
        wait_until: 'networkidle0',
        format: 'A4',
        margin: { top: '1cm', bottom: '1cm' }
      )

      File.write(pdf_output_path, grover.to_pdf)
      pdf_paths << pdf_output_path
      puts "[✓] PDF generated: #{pdf_output_path}"

    rescue Grover::DependencyError => e
      puts "[!] Puppeteer is required but not available. Skipping: #{html_path}"
      next
    rescue => e
      puts "[!] Failed to convert #{html_path}: #{e.class} - #{e.message}"
      next
    end
  end

  pdf_paths
end

.puppeteer_installed?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/rails_code_auditor/html_to_pdf_converter.rb', line 9

def self.puppeteer_installed?
  system("npx puppeteer --version > /dev/null 2>&1")
end