-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathform.exs
More file actions
260 lines (231 loc) · 8.45 KB
/
Copy pathform.exs
File metadata and controls
260 lines (231 loc) · 8.45 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
Code.require_file("support/fonts.exs", __DIR__)
# A realistic interactive form: supplier account application.
#
# Exercises every field type the library can produce — text (single and
# multiline), choice, checkbox, radio group, signature and push buttons — laid
# out as an actual document rather than a grid of test widgets.
#
# Static text is set in embedded fonts. Field *values* are not: a form field's
# appearance is generated by the viewer from the /DA string, which resolves
# against the AcroForm resource dictionary, and that only carries the standard
# 14 fonts. Mixing the two is deliberate and is what the library supports today.
page_w = 595
page_h = 842
margin = 50
content_w = page_w - margin * 2
ink = {0.13, 0.14, 0.16}
muted = {0.42, 0.45, 0.50}
accent = {0.06, 0.35, 0.55}
rule = {0.85, 0.86, 0.88}
{pdf, embedded?} =
Tincture.new()
|> Tincture.page_size(:a4)
|> Tincture.set_metadata(
title: "Supplier account application",
author: "Northgate Instruments Ltd",
subject: "Interactive application form"
)
|> Examples.Fonts.register("Body", "Sans")
# Resolve through the helper rather than naming "Body" directly: on a machine
# with none of the candidate fonts installed nothing was registered, and the
# document does not know that name.
body = Examples.Fonts.resolve("Body", embedded?)
sans = Examples.Fonts.resolve("Sans", embedded?)
# --- header ---------------------------------------------------------------
pdf =
pdf
|> Tincture.set_fill_color(accent)
|> Tincture.rectangle(0, page_h - 96, page_w, 96, :fill)
|> Tincture.set_fill_color({1.0, 1.0, 1.0})
|> Tincture.set_font(body, 22)
|> Tincture.text_at(margin, page_h - 52, "Supplier account application")
|> Tincture.set_font(sans, 8.5)
|> Tincture.text_at(
margin,
page_h - 70,
"Complete every field marked required, then sign and submit."
)
# Section heading, and a small-caps-ish label above a field.
section = fn pdf, y, title ->
pdf
|> Tincture.set_fill_color(accent)
|> Tincture.set_font(sans, 8)
|> Tincture.text_at(margin, y, String.upcase(title))
|> Tincture.set_stroke_color(rule)
|> Tincture.set_line_width(0.75)
|> Tincture.line(margin, y - 6, page_w - margin, y - 6)
|> Tincture.stroke()
end
label = fn pdf, x, y, text ->
pdf
|> Tincture.set_fill_color(muted)
|> Tincture.set_font(sans, 7.5)
|> Tincture.text_at(x, y, String.upcase(text))
end
# A field's box is drawn as a rule under it rather than a full border, which is
# why every field is created with border: :none.
underline = fn pdf, x, y, width ->
pdf
|> Tincture.set_stroke_color(rule)
|> Tincture.set_line_width(0.75)
|> Tincture.line(x, y - 3, x + width, y - 3)
|> Tincture.stroke()
end
# --- your details ---------------------------------------------------------
col_w = (content_w - 20) / 2
right_x = margin + col_w + 20
pdf = section.(pdf, page_h - 136, "Your details")
pdf =
pdf
|> label.(margin, page_h - 160, "Registered company name")
|> underline.(margin, page_h - 178, content_w)
|> Tincture.text_field(margin, page_h - 178, content_w, 18, "company_name",
required: true,
border: :none,
tooltip: "As it appears on your certificate of incorporation"
)
|> label.(margin, page_h - 200, "Contact name")
|> underline.(margin, page_h - 218, col_w)
|> Tincture.text_field(margin, page_h - 218, col_w, 18, "contact_name",
required: true,
border: :none
)
|> label.(right_x, page_h - 200, "Position")
|> underline.(right_x, page_h - 218, col_w)
|> Tincture.text_field(right_x, page_h - 218, col_w, 18, "position", border: :none)
|> label.(margin, page_h - 240, "Email")
|> underline.(margin, page_h - 258, col_w)
|> Tincture.text_field(margin, page_h - 258, col_w, 18, "email",
required: true,
border: :none
)
|> label.(right_x, page_h - 240, "Telephone")
|> underline.(right_x, page_h - 258, col_w)
|> Tincture.text_field(right_x, page_h - 258, col_w, 18, "telephone", border: :none)
|> label.(margin, page_h - 280, "Registered address")
|> underline.(margin, page_h - 330, content_w)
|> Tincture.text_field(margin, page_h - 330, content_w, 50, "address",
multiline: true,
border: :none
)
# --- account ---------------------------------------------------------------
pdf = section.(pdf, page_h - 366, "Account")
radio_y = page_h - 394
pdf =
pdf
|> label.(margin, radio_y + 16, "Account type")
|> Tincture.radio_group(
"account_type",
[
[value: "standard", x: margin, y: radio_y - 12, size: 12],
[value: "priority", x: margin, y: radio_y - 32, size: 12],
[value: "trial", x: margin, y: radio_y - 52, size: 12]
],
selected: "standard",
border: :none,
tooltip: "Priority accounts are invoiced monthly in arrears"
)
|> Tincture.set_fill_color(ink)
|> Tincture.set_font(body, 10)
|> Tincture.text_at(margin + 20, radio_y - 9, "Standard — invoiced per order")
|> Tincture.text_at(margin + 20, radio_y - 29, "Priority — invoiced monthly in arrears")
|> Tincture.text_at(margin + 20, radio_y - 49, "Trial — three months, then review")
pdf =
pdf
|> label.(right_x, radio_y + 16, "Payment terms")
|> underline.(right_x, radio_y - 6, col_w)
|> Tincture.choice_field(right_x, radio_y - 6, col_w, 18, "payment_terms",
options: ["14 days", "30 days", "60 days"],
value: "30 days",
dropdown: true,
border: :none
)
|> label.(right_x, radio_y - 30, "Currency")
|> underline.(right_x, radio_y - 52, col_w)
|> Tincture.choice_field(right_x, radio_y - 52, col_w, 18, "currency",
options: ["GBP", "EUR", "USD"],
value: "GBP",
dropdown: true,
border: :none
)
# --- declarations ----------------------------------------------------------
pdf = section.(pdf, page_h - 500, "Declarations")
check_y = page_h - 528
pdf =
pdf
|> Tincture.checkbox(margin, check_y, 12, "accept_terms",
required: true,
border: :none
)
|> Tincture.checkbox(margin, check_y - 22, 12, "newsletter", border: :none)
|> Tincture.set_fill_color(ink)
|> Tincture.set_font(body, 10)
|> Tincture.text_at(margin + 20, check_y + 3, "I accept the standard terms of supply (required)")
|> Tincture.text_at(margin + 20, check_y - 19, "Send me occasional product updates")
pdf =
pdf
|> label.(margin, check_y - 52, "Anything we should know")
|> underline.(margin, check_y - 100, content_w)
|> Tincture.text_field(margin, check_y - 100, content_w, 48, "notes",
multiline: true,
border: :none,
tooltip: "Optional"
)
# --- signature -------------------------------------------------------------
sign_y = page_h - 690
pdf =
pdf
|> section.(sign_y + 44, "Signature")
|> label.(margin, sign_y + 20, "Signed")
|> underline.(margin, sign_y - 20, 260)
|> Tincture.signature_field(margin, sign_y - 20, 260, 40, "signature",
required: true,
border: :none,
tooltip: "Sign here"
)
|> label.(right_x, sign_y + 20, "Date")
|> underline.(right_x, sign_y - 2, col_w)
|> Tincture.text_field(right_x, sign_y - 2, col_w, 18, "signed_on", border: :none)
# --- actions ---------------------------------------------------------------
button_y = 96
pdf =
pdf
|> Tincture.push_button(margin, button_y, 96, 26, "submit",
label: "Submit",
action: {:submit, "https://northgate-instruments.example/apply"},
tooltip: "Send this application"
)
|> Tincture.push_button(margin + 106, button_y, 96, 26, "clear",
label: "Clear",
action: :reset,
tooltip: "Reset every field"
)
|> Tincture.push_button(margin + 212, button_y, 96, 26, "help",
label: "Help",
action: {:url, "https://northgate-instruments.example/help"}
)
# --- footer ----------------------------------------------------------------
pdf =
pdf
|> Tincture.set_stroke_color(rule)
|> Tincture.set_line_width(0.75)
|> Tincture.line(margin, 66, page_w - margin, 66)
|> Tincture.stroke()
|> Tincture.set_fill_color(muted)
|> Tincture.set_font(sans, 7.5)
|> Tincture.text_at(
margin,
52,
"Northgate Instruments Ltd · Registered in England 04471902 · VAT GB 812 4471 02"
)
binary = Tincture.export(pdf)
path = Examples.Fonts.output_path("form.pdf")
File.write!(path, binary)
field_count = length(pdf.form_fields)
radio_buttons = pdf.form_fields |> Enum.flat_map(&Map.get(&1, :widgets, [])) |> length()
IO.puts("wrote #{Path.relative_to_cwd(path)} — #{byte_size(binary)} bytes")
IO.puts("fields: #{field_count} (#{radio_buttons} radio buttons across groups)")
IO.puts(
"types: " <>
(pdf.form_fields |> Enum.map(& &1.type) |> Enum.frequencies() |> inspect())
)