Module: Waxx::Pdf

Defined in:
waxx/pdf.rb

Overview

require 'prawn' to use PDFs

Instance Method Summary collapse

Instance Method Details

#doc_info(x, title: "Untitled", author: nil, subject: "", keywords: "", creator: "WAXX waxx.io", producer: "Prawn", creation_date: Time.new) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'waxx/pdf.rb', line 12

def doc_info(
  x,
  title: "Untitled",
  author: nil,
  subject: "",
  keywords: "",
  creator: "WAXX waxx.io",
  producer: "Prawn",
  creation_date: Time.new
)
  {
  :Title => title,
  :Author => author || "#{x.usr['un']}",
  :Subject => subject,
  :Keywords => keywords,
  :Creator => creator,
  :Producer => producer,
  :CreationDate => creation_date
  }
end

#file_path(x) ⇒ Object



33
34
35
# File 'waxx/pdf.rb', line 33

def file_path(x)
  "#{Waxx::Root}/tmp/#{Time.new.strftime('%Y%m%dT%H%M%S')}-u#{x.usr['id']}.pdf"
end

#getObject



37
38
39
40
41
42
# File 'waxx/pdf.rb', line 37

def get
  pdf = new_doc
  pdf.text "Hello from WAXX. You need to implement the get method in your layout. Then call render_file & return_file"
  render_file(x, pdf)
  return_file(x)
end

#new_doc(margin: 50, orientation: "portrait", info: {}) ⇒ Object



8
9
10
# File 'waxx/pdf.rb', line 8

def new_doc(margin:50, orientation: "portrait", info: {})
  Prawn::Document.new(:margin=>margin, :orientation=>orientation, :info=>info)
end

#render_file(x, pdf, path) ⇒ Object



44
45
46
# File 'waxx/pdf.rb', line 44

def render_file(x, pdf, path)
  pdf.render_file path
end

#return_file(x, path) ⇒ Object



48
49
50
# File 'waxx/pdf.rb', line 48

def return_file(x, path)
  File.open(path, "rb"){|f| x << f.read}
end

#show_grid(pdf) ⇒ Object



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

def show_grid(pdf)
  existing_color = pdf.stroke_color?
  pdf.stroke_color Color::RGB.new(230, 230, 255)
  (0..800).step(10){|a|
    pdf.stroke_color Color::RGB.new(130, 130, 255) if (a % 100).zero?
    pdf.line(a,0,a,620).stroke
    pdf.stroke_color Color::RGB.new(230, 230, 255) if (a % 100).zero?
  }

  (0..620).step(10){|b|
    pdf.stroke_color Color::RGB.new(130, 130, 255) if (b % 100).zero?
    pdf.line(0,b,800,b).stroke
    pdf.stroke_color Color::RGB.new(230, 230, 255) if (b % 100).zero?
  }
  pdf.stroke_color existing_color
end