File: example014_controller.rb

package info (click to toggle)
ruby-rbpdf 1.21.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,488 kB
  • sloc: ruby: 138,259; makefile: 12
file content (170 lines) | stat: -rw-r--r-- 5,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# coding: UTF-8
#============================================================+
# Begin       : 2008-03-04
# Last Update : 2010-06-02
#
# Description : Example 014 for RBPDF class
#               Javascript Form and user rights (only works on Adobe Acrobat)
#
# Author: Jun NAITOH
# License: LGPL 2.1 or later
#============================================================+

require("example_common.rb")

class Example014Controller < ApplicationController
  def index
    # create new PDF document
    pdf = RBPDF.new(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false)

    # set document information
    pdf.set_creator(PDF_CREATOR)
    pdf.set_author(PDF_AUTHOR)
    pdf.set_title('RBPDF Example 014')
    pdf.set_subject('RBPDF Tutorial')
    pdf.set_keywords('RBPDF, PDF, example, test, guide')

    # set default header data
    pdf.set_header_data(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE + ' 014', PDF_HEADER_STRING)

    # set header and footer fonts
    pdf.set_header_font([PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN])
    pdf.set_footer_font([PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA])

    # set default monospaced font
    pdf.set_default_monospaced_font(PDF_FONT_MONOSPACED)

    # set margins
    pdf.set_margins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT)
    pdf.set_header_margin(PDF_MARGIN_HEADER)
    pdf.set_footer_margin(PDF_MARGIN_FOOTER)

    # set auto page breaks
    pdf.set_auto_page_break(true, PDF_MARGIN_BOTTOM)

    # set image scale factor
    pdf.set_image_scale(PDF_IMAGE_SCALE_RATIO)

    # set some language-dependent strings
    pdf.set_language_array($l)

    # ---------------------------------------------------------

    # set font
    # IMPORTANT: disable font subsetting to allow users editing the document
    pdf.set_font('helvetica', '', 10)

    # add a page
    pdf.add_page()

    # It is possible to create text fields, combo boxes, check boxes and buttons.
    # Fields are created at the current position and are given a name.
    # This name allows to manipulate them via JavaScript in order to perform some validation for instance.

    # set default form properties
    pdf.setFormDefaultProp({'lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>[255, 255, 200], 'strokeColor'=>[255, 128, 128]})

    pdf.set_font('helvetica', 'BI', 18)
    pdf.cell(0, 5, 'Example of Form', 0, 1, 'C')
    pdf.ln(10)

    pdf.set_font('helvetica', '', 12)

    # First name
    pdf.cell(35, 5, 'First name:')
    pdf.text_field('firstname', 50, 5)
    pdf.ln(6)

    # Last name
    pdf.cell(35, 5, 'Last name:')
    pdf.text_field('lastname', 50, 5)
    pdf.ln(6)

    # Gender
    pdf.cell(35, 5, 'Gender:')
    pdf.combo_box('gender', 30, 5, [['', '-'], ['M', 'Male'],['F', 'Female']])
    pdf.ln(6)

    # Drink
    pdf.cell(35, 5, 'Drink:')
    pdf.radio_button('drink', 5, {}, {}, 'Water')
    pdf.cell(35, 5, 'Water')
    pdf.ln(6)
    pdf.cell(35, 5, '')
    pdf.radio_button('drink', 5, {}, {}, 'Beer', true)
    pdf.cell(35, 5, 'Beer')
    pdf.ln(6)
    pdf.cell(35, 5, '')
    pdf.radio_button('drink', 5, {}, {}, 'Wine')
    pdf.cell(35, 5, 'Wine')
    pdf.ln(10)

    # Listbox
    pdf.cell(35, 5, 'List:')
    pdf.list_box('listbox', 60, 15, ['', 'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7'], {'multipleSelection'=>'true'})
    pdf.ln(20)

    # Adress
    pdf.cell(35, 5, 'Address:')
    pdf.text_field('address', 60, 18, {'multiline'=>true})
    pdf.ln(19)

    # E-mail
    pdf.cell(35, 5, 'E-mail:')
    pdf.text_field('email', 50, 5)
    pdf.ln(6)

    # Newsletter
    pdf.cell(35, 5, 'Newsletter:')
    pdf.check_box('newsletter', 5, true, {}, {}, 'OK')
    pdf.ln(10)

    # Date of the day
    pdf.cell(35, 5, 'Date:')
    pdf.text_field('date', 30, 5, {}, {'v' => Date.today.to_s, 'dv' => Date.today.to_s})
    pdf.ln(10)

    pdf.SetX(50)

    # button to validate and print
    pdf.button('print', 30, 10, 'Print', 'Print()', {'lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>[128, 196, 255], 'strokeColor'=>[64, 64, 64]})

    # Reset button
    pdf.button('reset', 30, 10, 'Reset', {'S'=>'ResetForm'}, {'lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>[128, 196, 255], 'strokeColor'=>[64, 64, 64]})

    # Submit button
    pdf.button('submit', 30, 10, 'Submit', {'S'=>'SubmitForm', 'F'=>'http://localhost/printvars.php', 'Flags'=>['ExportFormat']}, {'lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>[128, 196, 255], 'strokeColor'=>[64, 64, 64]})

    # Form validation functions
    js = <<~EOD
    function CheckField(name,message) {
      var f = getField(name)
      if(f.value == '') {
          app.alert(message)
          f.setFocus()
          return false
      }
      return true
    }
    function Print() {
      if(!CheckField('firstname','First name is mandatory')) {return;}
      if(!CheckField('lastname','Last name is mandatory')) {return;}
      if(!CheckField('gender','Gender is mandatory')) {return;}
      if(!CheckField('address','Address is mandatory')) {return;}
      print()
    }
    EOD

    # Add Javascript code
    pdf.IncludeJS(js)

    # ---------------------------------------------------------

    # Close and output PDF document
    send_data pdf.output(), :type => "application/pdf", :disposition => "inline"
  end
end

#============================================================+
# END OF FILE
#============================================================+