Module: RFPDF

Defined in:
vendor/plugins/rfpdf/lib/rfpdf/view.rb,
vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb,
vendor/plugins/rfpdf/lib/rfpdf/errors.rb

Overview

Copyright © 2006 4ssoM LLC <www.4ssoM.com>

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Thanks go out to Bruce Williams of codefluency who created RTex. This template handler is modification of his work.

Example Registration

  ActionView::Base::register_template_handler 'rfpdf', RFpdfView

Defined Under Namespace

Classes: GenerationError, View

Constant Summary

COLOR_PALETTE =
{
  :black => [0x00, 0x00, 0x00],
  :white => [0xff, 0xff, 0xff],
}.freeze

Instance Method Summary

Instance Method Details

- (Object) close_tag(tag, options = {})

:nodoc:



271
272
273
274
275
276
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 271

def close_tag(tag, options = {}) #:nodoc:
  #Closing tag
  tag = tag.to_s.upcase
  set_style(tag, false) if tag == 'B' or tag == 'I' or  tag == 'U'
  @href = '' if $tag == 'A'
end

- (Object) draw_box(x, y, w, h, options = {})

Draw a box at (x, y), w wide and h high.

Options are:

  • :border - Draw a border, 0 = no, 1 = yes? Default value is 1.
  • :border_color - Default value is COLOR_PALETTE[:black].
  • :border_width - Default value is 0.5.
  • :fill - Fill the box, 0 = no, 1 = yes? Default value is 1.
  • :fill_color - Default value is nothing or COLOR_PALETTE[:white].

Example:

  draw_box(x, y - 1, 38, 22)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 87

def draw_box(x, y, w, h, options = {})
  options[:border] ||= 1
  options[:border_color] ||= COLOR_PALETTE[:black]
  options[:border_width] ||= 0.5
  options[:fill] ||= 1
  options[:fill_color] ||= COLOR_PALETTE[:white]
  SetLineWidth(options[:border_width])
  set_draw_color(options[:border_color])
  set_fill_color(options[:fill_color])
  fd = ""
  fd = "D" if options[:border] == 1
  fd += "F" if options[:fill] == 1
  Rect(x, y, w, h, fd)
end

- (Object) draw_line(x1, y1, x2, y2, options = {})

Draw a line from (x1, y1) to (x2, y2).

Options are:

  • :line_color - Default value is COLOR_PALETTE[:black].
  • :line_width - Default value is 0.5.

Example:

  draw_line(x1, y1, x1, y1+h, :line_color => ReportHelper::COLOR_PALETTE[:dark_blue], :line_width => 1)


17
18
19
20
21
22
23
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 17

def draw_line(x1, y1, x2, y2, options = {})
  options[:line_color] ||= COLOR_PALETTE[:black]
  options[:line_width] ||= 0.5
  set_draw_color(options[:line_color])
  SetLineWidth(options[:line_width])
  Line(x1, y1, x2, y2)
end

- (Object) draw_text(x, y, text, options = {})

Draw a string of text at (x, y).

Options are:

  • :font_color - Default value is COLOR_PALETTE[:black].
  • :font_size - Default value is 10.
  • :font_style - Default value is nothing or ’’.

Example:

  draw_text(x, y, header_left, :font_size => 10)


36
37
38
39
40
41
42
43
44
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 36

def draw_text(x, y, text, options = {})
  options[:font_color] ||= COLOR_PALETTE[:black]
  options[:font_size] ||= 10
  options[:font_style] ||= ''
  set_text_color(options[:font_color])
  SetFont('Arial', options[:font_style], options[:font_size])
  SetXY(x, y)
  Write(options[:font_size] + 4, text)
end

- (Object) draw_text_block(x, y, text, left_margin, right_margin, options = {})

Draw a block of text at (x, y) bounded by left_margin and right_margin. Both margins are measured from their corresponding edge.

Options are:

  • :font_color - Default value is COLOR_PALETTE[:black].
  • :font_size - Default value is 10.
  • :font_style - Default value is nothing or ’’.

Example:

  draw_text_block(left_margin, 85, "question", left_margin, 280,
      :font_color => ReportHelper::COLOR_PALETTE[:dark_blue],
      :font_size => 12,
      :font_style => 'I')


61
62
63
64
65
66
67
68
69
70
71
72
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 61

def draw_text_block(x, y, text, left_margin, right_margin, options = {})
  options[:font_color] ||= COLOR_PALETTE[:black]
  options[:font_size] ||= 10
  options[:font_style] ||= ''
  set_text_color(options[:font_color])
  SetFont('Arial', options[:font_style], options[:font_size])
  SetXY(x, y)
  SetLeftMargin(left_margin)
  SetRightMargin(right_margin)
  Write(options[:font_size] + 4, text)
  SetMargins(0,0,0)
end

- (Object) draw_text_box(x, y, w, h, text, options = {})

Draw a string of text at (x, y) in a box w wide and h high.

Options are:

  • :align - Vertical alignment ‘C’ = center, ‘L’ = left, ‘R’ = right. Default value is ‘C’.
  • :border - Draw a border, 0 = no, 1 = yes? Default value is 0.
  • :border_color - Default value is COLOR_PALETTE[:black].
  • :border_width - Default value is 0.5.
  • :fill - Fill the box, 0 = no, 1 = yes? Default value is 1.
  • :fill_color - Default value is nothing or COLOR_PALETTE[:white].
  • :font_color - Default value is COLOR_PALETTE[:black].
  • :font_size - Default value is nothing or 8.
  • :font_style - ‘B’ = bold, ‘I’ = italic, ‘U’ = underline. Default value is nothing ’’.
  • :padding - Default value is nothing or 2.
  • :valign - ‘M’ = middle, ‘T’ = top, ‘B’ = bottom. Default value is nothing or ‘M’.

Example:

  draw_text_box(x, y - 1, 38, 22,
                "your_score_title",
                :fill => 0,
                :font_color => ReportHelper::COLOR_PALETTE[:blue],
                :font_line_spacing => 0,
                :font_style => "B",
                :valign => "M")


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 127

def draw_text_box(x, y, w, h, text, options = {})
  options[:align] ||= 'C'
  options[:border] ||= 0
  options[:border_color] ||= COLOR_PALETTE[:black]
  options[:border_width] ||= 0.5
  options[:fill] ||= 1
  options[:fill_color] ||= COLOR_PALETTE[:white]
  options[:font_color] ||= COLOR_PALETTE[:black]
  options[:font_size] ||= 8
  options[:font_line_spacing] ||= options[:font_size] * 0.3
  options[:font_style] ||= ''
  options[:padding] ||= 2
  options[:valign] ||= "M"
  if options[:fill] == 1 or options[:border] == 1
    draw_box(x, y, w, h, options)
  end    
  SetMargins(0,0,0)
  set_text_color(options[:font_color])
  font_size = options[:font_size]
  SetFont('Arial', options[:font_style], font_size)
  font_size += options[:font_line_spacing]
  case options[:valign]
    when "B"
      y -= options[:padding]
      text = "\n" + text if text["\n"].nil?
    when "T"
      y += options[:padding]
  end
  SetXY(x, y)
  if GetStringWidth(text) > w or not text["\n"].nil? or options[:valign] == "T"
    font_size += options[:font_size] * 0.1
    #TODO 2006-07-21 Level=1 - this is assuming a 2 line text
    SetXY(x, y + ((h - (font_size * 2)) / 2)) if options[:valign] == "M"
    MultiCell(w, font_size, text, 0, options[:align])
  else
    Cell(w, h, text, 0, 0, options[:align])
  end
end

- (Object) draw_title(x, y, title, options = {})

Draw a string of text at (x, y) as a title.

Options are:

  • :font_color - Default value is COLOR_PALETTE[:black].
  • :font_size - Default value is 18.
  • :font_style - Default value is nothing or ’’.

Example:

  draw_title(left_margin, 60,
      "title:",
      :font_color => ReportHelper::COLOR_PALETTE[:dark_blue])


179
180
181
182
183
184
185
186
187
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 179

def draw_title(x, y, title, options = {})
  options[:font_color] ||= COLOR_PALETTE[:black]
  options[:font_size] ||= 18
  options[:font_style] ||= ''
  set_text_color(options[:font_color])
  SetFont('Arial', options[:font_style], options[:font_size])
  SetXY(x, y)
  Write(options[:font_size] + 2, title)
end

- (Object) open_tag(tag, options = {})

:nodoc:



263
264
265
266
267
268
269
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 263

def open_tag(tag, options = {}) #:nodoc:
  #Opening tag
  tag = tag.to_s.upcase
  set_style(tag, true) if tag == 'B' or tag == 'I' or tag == 'U'
  @href = options['HREF'] if tag == 'A'
  Ln(options[:height]) if tag == 'BR'
end

:nodoc:



288
289
290
291
292
293
294
295
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 288

def put_link(url, txt) #:nodoc:
  #Put a hyperlink
  SetTextColor(0,0,255)
  set_style('U',true)
  Write(5, txt, url)
  set_style('U',false)
  SetTextColor(0)
end

- (Object) set_draw_color(color = COLOR_PALETTE[:black])

Set the draw color. Default value is COLOR_PALETTE[:black].

Example:

  set_draw_color(ReportHelper::COLOR_PALETTE[:dark_blue])


195
196
197
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 195

def set_draw_color(color = COLOR_PALETTE[:black])
  SetDrawColor(color[0], color[1], color[2])
end

- (Object) set_fill_color(color = COLOR_PALETTE[:white])

Set the fill color. Default value is COLOR_PALETTE[:white].

Example:

  set_fill_color(ReportHelper::COLOR_PALETTE[:dark_blue])


205
206
207
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 205

def set_fill_color(color = COLOR_PALETTE[:white])
  SetFillColor(color[0], color[1], color[2])
end

- (Object) set_style(tag, enable = true)

:nodoc:



278
279
280
281
282
283
284
285
286
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 278

def set_style(tag, enable = true) #:nodoc:
  #Modify style and select corresponding font
  style = ""
  @style[tag] = enable
  ['B','I','U'].each do |s|
    style += s if not @style[s].nil? and @style[s]
  end
  SetFont('', style)
end

- (Object) set_text_color(color = COLOR_PALETTE[:black])

Set the text color. Default value is COLOR_PALETTE[:white].

Example:

  set_text_color(ReportHelper::COLOR_PALETTE[:dark_blue])


215
216
217
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 215

def set_text_color(color = COLOR_PALETTE[:black])
  SetTextColor(color[0], color[1], color[2])
end

- (Object) write_html(html, options = {})

Write a string containing html characters. Default value is COLOR_PALETTE[:white].

Options are:

  • :height - Line height. Default value is 20.

Example:

  write_html(html, :height => 12)


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 228

def write_html(html, options = {})
  options[:height] ||= 20
  #HTML parser
  @href = nil
  @style = {}
  html.gsub!("\n",' ')
  re = %r{ ( <!--.*?--> |
             <  (?:
                [^<>"] +
                |
                "  (?: \\.  |  [^\\"]+  ) *  "
                ) *
             >
           )  }xm

  html.split(re).each do |value|
    if "<" == value[0,1]
      #Tag
      if (value[1, 1] == '/')
        close_tag(value[2..-2], options)
      else
        tag = value[1..-2]
        open_tag(tag, options)
      end
    else
      #Text
      if @href
        put_link(@href,value)
      else
        Write(options[:height], value)
      end
    end
  end
end