Class: Sakusei::MdToPdfConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/sakusei/md_to_pdf_converter.rb

Overview

Converts markdown content to PDF using md-to-pdf

Instance Method Summary collapse

Constructor Details

#initialize(content, output_path, style_pack, options = {}) ⇒ MdToPdfConverter

Returns a new instance of MdToPdfConverter.



9
10
11
12
13
14
15
# File 'lib/sakusei/md_to_pdf_converter.rb', line 9

def initialize(content, output_path, style_pack, options = {})
  @content    = content
  @output_path = output_path
  @style_pack = style_pack
  @options    = options
  @source_dir = options[:source_dir]
end

Instance Method Details

#convertObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sakusei/md_to_pdf_converter.rb', line 17

def convert
  Dir.mktmpdir('sakusei') do |temp_dir|
    # Write processed content to temp markdown file
    temp_md = File.join(temp_dir, 'input.md')
    File.write(temp_md, page_chrome_prefix + @content)

    # Copy any local images into the temp dir, mirroring the relative structure,
    # so md-to-pdf's HTTP server can serve them by their relative paths.
    copy_images(temp_dir)

    cmd = build_command(temp_md, temp_dir)
    result = system(cmd)
    raise Error, 'PDF conversion failed' unless result

    FileUtils.mv(File.join(temp_dir, 'input.pdf'), @output_path)
  end

  @output_path
end