Class: Bookie::Emitters::PDF

Inherits:
Object
  • Object
show all
Includes:
Prawn::Measurements
Defined in:
lib/bookie/emitters/pdf.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePDF

Returns a new instance of PDF.



12
13
14
15
16
17
# File 'lib/bookie/emitters/pdf.rb', line 12

def initialize
  @document    = new_prawn_document
  @document.extend(Prawn::Measurements)

  register_fonts
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



19
20
21
# File 'lib/bookie/emitters/pdf.rb', line 19

def document
  @document
end

Class Method Details

.extensionObject



8
9
10
# File 'lib/bookie/emitters/pdf.rb', line 8

def self.extension
  ".pdf"
end

Instance Method Details

#build_list(list) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bookie/emitters/pdf.rb', line 28

def build_list(list)
  items = list.contents

  draw do
    font("serif", :size => 9) do
      items.each do |li|
        li_text = li.gsub(/\s+/," ")

        group do
          float { text "" }
          indent(in2pt(0.15)) do
            text li_text,
              inline_format: true,
              leading: 2
          end
        end

        move_down in2pt(0.05)
      end
    end

    move_down in2pt(0.05)
  end
end

#build_paragraph(paragraph) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/bookie/emitters/pdf.rb', line 69

def build_paragraph(paragraph)
  para_text = convert_inlines(paragraph.contents)

  draw do
    font("serif", size: 9) do
      text(para_text, align: :justify, leading: 2, inline_format: true)
    end
    move_down in2pt(0.1)
  end
end

#build_raw_text(raw_text) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/bookie/emitters/pdf.rb', line 91

def build_raw_text(raw_text)
  sanitized_text = raw_text.contents.gsub(" ", Prawn::Text::NBSP).strip

  draw do
    font("mono", size: 8) do
      text sanitized_text            
      move_down in2pt(0.1)
    end
  end
end

#build_section_heading(section_text) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bookie/emitters/pdf.rb', line 53

def build_section_heading(section_text)
  draw do
    start_new_page unless cursor > in2pt(0.4)

    move_down in2pt(0.1)

    float do
      font("sans", :style => :bold, :size => 14) do
        text(section_text.contents.strip)
      end
    end
  
    move_down in2pt(0.3)
  end
end

#convert_inlines(contents) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/bookie/emitters/pdf.rb', line 80

def convert_inlines(contents)
  contents.map do |c|
    case c
    when Bookie::NormalText
      c.contents
    when Bookie::CodeText
      "<color rgb='660000'><font name='mono' size='9'>#{c.contents}</font></color>"
    end
  end.join.strip
end

#draw(&block) ⇒ Object



150
151
152
# File 'lib/bookie/emitters/pdf.rb', line 150

def draw(&block)
  @document.instance_eval(&block)
end

#register_fontsObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bookie/emitters/pdf.rb', line 123

def register_fonts
  dejavu_path = File.dirname(__FILE__) + "/../../../data/fonts/dejavu"

  draw do
    font_families["sans"] = {
      :normal => "#{dejavu_path}/DejaVuSansCondensed.ttf",
      :italic => "#{dejavu_path}/DejaVuSansCondensed-Oblique.ttf",
      :bold => "#{dejavu_path}/DejaVuSansCondensed-Bold.ttf",
      :bold_italic => "#{dejavu_path}/DejaVuSansCondensed-BoldOblique.ttf"
    }

    font_families["mono"] = {
      :normal => "#{dejavu_path}/DejaVuSansMono.ttf",
      :italic => "#{dejavu_path}/DejaVuSansMono-Oblique.ttf",
      :bold => "#{dejavu_path}/DejaVuSansMono-Bold.ttf",
      :bold_italic => "#{dejavu_path}/DejaVuSansMono-BoldOblique.ttf"
    }

    font_families["serif"] = {
      :normal => "#{dejavu_path}/DejaVuSerif.ttf",
      :italic => "#{dejavu_path}/DejaVuSerif-Italic.ttf",
      :bold => "#{dejavu_path}/DejaVuSerif-Bold.ttf",
      :bold_italic => "#{dejavu_path}/DejaVuSerif-BoldItalic.ttf"
    }
  end
end

#render(params) ⇒ Object



102
103
104
# File 'lib/bookie/emitters/pdf.rb', line 102

def render(params)
  @document.render_file(params[:file])
end

#render_header(params) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bookie/emitters/pdf.rb', line 106

def render_header(params)
  draw do
    font("sans") do
      text "<b>#{params[:header]}</b>", 
        size: 12, align: :right, inline_format: true
      stroke_horizontal_rule

      move_down in2pt(0.1)

      text "<b>#{params[:title]}</b>",
        size: 18, align: :right, inline_format: true
        
      move_down in2pt(1.25)
    end
  end
end

#start_new_chapter(params) ⇒ Object



21
22
23
24
25
26
# File 'lib/bookie/emitters/pdf.rb', line 21

def start_new_chapter(params)
  @document.save_graphics_state # HACK for what might be a Prawn bug
  @document.start_new_page
  @document.outline.section(params[:title], :destination => @document.page_number)
  render_header(params)
end