Class: PDF::Storycards::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/storycards/writer.rb

Constant Summary collapse

CARD_WIDTH =
PDF::Writer.in2pts(5)
CARD_HEIGHT =
PDF::Writer.in2pts(3)

Class Method Summary collapse

Class Method Details

.make_pdf(story_text, opts) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pdf/storycards/writer.rb', line 11

def self.make_pdf( story_text, opts )
  opts[:style] = :card_1up unless opts[:style]
  opts[:borders] = false unless opts[:borders]
  
  stories = StoryParser.new.parse_stories(story_text)
  
  if opts[:style] == :card_1up
    pdf = PDF::Writer.new(:paper => [7.62, 12.7], :orientation => :landscape) #3x5 card
    pdf.margins_mm 8
    pdf.move_pointer(-60)

    stories.each_with_index do |story, index|
      pdf.start_new_page if index > 0
      print_card_corners(pdf, story, opts[:lower_left], opts[:lower_right], 0, 0)
      print_card_text(pdf, story, 0, opts[:make_sentences])
    end

    return pdf.render

  elsif opts[:style] == :letter_4up

    pdf = PDF::Writer.new(:paper => "LETTER", :orientation => :landscape)
    margin = PDF::Writer.in2pts(0.33)
    gutter = margin
    padding = 10

    pdf.start_columns(2, gutter)
    stories.each_with_index do |story, index|

      is_laying_out_left_hand_column = (index % 4 < 2)
      if is_laying_out_left_hand_column
        rect_x = margin
      else
        rect_x = pdf.page_width - margin - CARD_WIDTH
      end

      is_laying_out_top_row = (index % 2 == 0)
      if is_laying_out_top_row
        pdf.start_new_page unless index == 0
        pdf.y = pdf.page_height - margin - padding
        rect_y = pdf.page_height - margin - CARD_HEIGHT
      else
        rect_y = margin
        pdf.y = margin + CARD_HEIGHT - padding
      end
  
      print_border(pdf, rect_x, rect_y) if opts[:borders]
      print_card_corners(pdf, story, opts[:lower_left], opts[:lower_right], rect_x, rect_y)
      print_card_text(pdf, story, padding, opts[:make_sentences])
    end

    return pdf.render

  else
    raise "Unknown style: #{opts[:style]}"
  end
end