Class: Sakusei::MdToPdfConverter

Inherits:
ConverterBase 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.



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

def initialize(content, output_path, style_pack, options = {})
  super(content, style_pack, options)
  @output_path = output_path
end

Instance Method Details

#convertObject



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

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).join(' ')
    result = system(cmd)
    raise Error, 'PDF conversion failed' unless result

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

  @output_path
end