Module: Yaml::Converter::Renderer::PdfHexapdf

Defined in:
lib/yaml/converter/renderer/pdf_hexapdf.rb

Overview

Native PDF rendering using HexaPDF.
Basic layout: title lines, YAML block in monospace, notes as italic paragraphs.

For PDF rendering via pandoc, see {Yaml::Converter::Renderer::PandocShell}.

Class Method Summary collapse

Class Method Details

.draw_columns(composer, yaml_section, notes, size:, margin:, options: {}) ⇒ Object

Compose each column independently and overlay its pages on the title document.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/yaml/converter/renderer/pdf_hexapdf.rb', line 58

def draw_columns(composer, yaml_section, notes, size:, margin:, options: {})
  page = composer.page
  width = page.box.width
  column_width = (width - margin[1] - margin[3] - 16) / 2.0
  start_page = composer.document.pages.count - 1
  first_top = page.box.height - composer.y
  columns = [
    [yaml_section, [margin[0], width - margin[3] - column_width, margin[2], margin[3]],
      {font: "Courier", font_size: options[:pdf_yaml_font_size] || 9}],
    [notes.map { |note| "NOTE: #{note}" }, [margin[0], margin[1], margin[2], margin[3] + column_width + 16],
      {font: "Helvetica", font_size: options[:pdf_body_font_size] || 11, font_variant: :italic}]
  ]
  columns.each do |lines, column_margin, style|
    column = HexaPDF::Composer.new(skip_page_creation: true)
    column.page_style(:continuation, page_size: size) do |canvas, page_style|
      page_style.frame = page_style.create_frame(canvas.context, column_margin)
    end
    column.page_style(:first, page_size: size, next_style: :continuation) do |canvas, page_style|
      page_style.frame = page_style.create_frame(canvas.context, [first_top, *column_margin[1..]])
    end
    column.new_page(:first)
    draw_lines(column, lines, **style)
    column.document.dispatch_message(:complete_objects)
    column.document.validate
    column.document.pages.each_with_index do |column_page, index|
      composer.new_page while composer.document.pages.count <= start_page + index
      target = composer.document.pages[start_page + index]
      form = column.document.add(column_page.to_form_xobject)
      target.canvas.xobject(composer.document.import(form), at: [0, 0])
    end
  end
end

.draw_lines(composer, lines, **style) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/yaml/converter/renderer/pdf_hexapdf.rb', line 91

def draw_lines(composer, lines, **style)
  variant = style.delete(:font_variant)
  style[:font] = composer.document.fonts.add(style[:font], variant: variant) if variant
  lines.each do |line|
    indent = (style[:font] == "Courier") ? line[/\A */].length * style[:font_size] * 0.6 : 0
    composer.text(line.empty? ? " " : line, text_indent: indent, **style)
  end
end

.extract_notes(markdown) ⇒ Array<String>

Extract note strings from Markdown blockquote lines.

Parameters:

  • markdown (String)

Returns:

  • (Array<String>)


148
149
150
# File 'lib/yaml/converter/renderer/pdf_hexapdf.rb', line 148

def extract_notes(markdown)
  markdown.lines.grep(/^> NOTE:/).map { |l| l.sub(/^> NOTE:\s*/, "").strip }
end

.fenced_yaml(markdown) ⇒ Array<String>

Return the lines inside the first fenced ```yaml block.

Parameters:

  • markdown (String)

Returns:

  • (Array<String>)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/yaml/converter/renderer/pdf_hexapdf.rb', line 128

def fenced_yaml(markdown)
  inside = false
  lines = []
  markdown.each_line do |l|
    if l.start_with?("```yaml")
      inside = true
      next
    elsif inside && l.strip == "```"
      inside = false
      break
    elsif inside
      lines << l.rstrip
    end
  end
  lines
end

.header_lines(markdown) ⇒ Array<String>

Extract leading # Title lines.

Parameters:

  • markdown (String)

Returns:

  • (Array<String>)


121
122
123
# File 'lib/yaml/converter/renderer/pdf_hexapdf.rb', line 121

def header_lines(markdown)
  markdown.lines.take_while { |l| l.start_with?("# ") }.map { |l| l.sub(/^# /, "").strip }
end

.normalize_margin(margin) ⇒ Object



111
112
113
114
115
116
# File 'lib/yaml/converter/renderer/pdf_hexapdf.rb', line 111

def normalize_margin(margin)
  values = Array(margin).map(&:to_i)
  return [36, 36, 36, 36] unless values.size == 4

  values
end

.page_size(name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/yaml/converter/renderer/pdf_hexapdf.rb', line 100

def page_size(name)
  case name.to_s.upcase
  when "A4"
    :A4
  when "LEGAL"
    [0, 0, 612, 1008]
  else
    :Letter
  end
end

.render(markdown:, out_path:, options: {}) ⇒ Boolean

Render a PDF document from the given markdown string.

Parameters:

  • markdown (String) —

    Markdown that includes fenced YAML and blockquote notes

  • out_path (String) —

    Destination PDF path

  • options (Hash) (defaults to: {}) —

    PDF options (see Config defaults: page size, margins, font sizes, two-column notes)

Options Hash (options:):

  • :pdf_page_size (String) — default: "LETTER"
  • :pdf_margin (Array<Integer>) — default: [36, 36, 36, 36]
  • :pdf_title_font_size (Integer) — default: 14
  • :pdf_body_font_size (Integer) — default: 11
  • :pdf_yaml_font_size (Integer) — default: 9
  • :pdf_two_column_notes (Boolean) — default: false

Returns:

  • (Boolean) —

    true if rendering succeeded



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
# File 'lib/yaml/converter/renderer/pdf_hexapdf.rb', line 25

def render(markdown:, out_path:, options: {})
  require "hexapdf"

  notes = extract_notes(markdown)
  yaml_section = fenced_yaml(markdown)
  title_lines = header_lines(markdown)

  margin = normalize_margin(options[:pdf_margin] || [36, 36, 36, 36])
  # Reserve room for the footer even when the requested bottom margin is smaller.
  margin[2] = [margin[2], 50].max
  size = page_size(options[:pdf_page_size] || "LETTER")
  composer = HexaPDF::Composer.new(page_size: size, margin: margin)
  draw_lines(composer, title_lines, font: "Helvetica", font_size: options[:pdf_title_font_size] || 14, font_variant: :bold)

  if options[:pdf_two_column_notes] && notes.any?
    draw_columns(composer, yaml_section, notes, size: size, margin: margin, options: options)
  else
    draw_lines(composer, yaml_section, font: "Courier", font_size: options[:pdf_yaml_font_size] || 9)
    draw_lines(composer, notes.map { |note| "NOTE: #{note}" }, font: "Helvetica", font_size: options[:pdf_body_font_size] || 11, font_variant: :italic)
  end

  pages = composer.document.pages.to_a
  pages.each_with_index do |page, index|
    page.canvas.font("Helvetica", size: 9).text("Page #{index + 1} of #{pages.length}", at: [page.box.width - 136, 24])
  end
  composer.write(out_path, optimize: true)
  true
rescue => e
  warn("hexapdf pdf failed: #{e.class}: #{e.message}")
  false
end