-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_hexa.rb
160 lines (126 loc) · 5.24 KB
/
main_hexa.rb
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
# frozen_string_literal: true
require 'hexapdf'
require_relative 'lib/lib'
BASE_FONT_SIZE = 12
class FactureComposer < HexaPDF::Composer
def initialize(skip_page_creation: false, page_size: :A4, page_orientation: :portrait,
margin: 36)
super(skip_page_creation:, page_size:, page_orientation:, margin:)
# https://hexapdf.gettalong.org/documentation/pdfa/index.html
document.task(:pdfa)
document.config['font.map'] = {
'DejaVu Sans' => {
none: 'assets/DejaVuSans.ttf',
bold: 'assets/DejaVuSans-Bold.ttf',
}
}
style(:base, font: 'DejaVu Sans', font_size: BASE_FONT_SIZE, line_spacing: 1.2)
style(:bold, font: ['DejaVu Sans', { variant: :bold }])
style(:header, font: ['DejaVu Sans', { variant: :bold }], font_size: BASE_FONT_SIZE * 7 / 6, text_align: :center) # TODO: Use 14 as hardcoded?
style(:footer, font_size: 7, text_align: :center) # TODO: Use `BASE_FONT_SIZE / 2`?
page_style(:default, page_size: page_size) do |canvas, style|
# canvas.font('DejaVu Sans', size: 12)
# canvas.text("Coucou", at: [50, 50], position: :flow)
margins = HexaPDF::Layout::Style::Quad.new(0)
# margins.top =
# style.frame = style.create_frame(canvas.context, margins)
end
end
def new_page(style = @next_page_style)
super(style)
image('assets/logo_rezo.png', width: 75, position: :float, mask_mode: :none)
text('Facture Rézoléo', style: :header, margin: margin_bottom(2))
text(DocumentGenerator::Facture::FOOTER, style: :footer, position: [0, 0], text_align: :center)
end
end
def margin_bottom(lines)
[0, 0, lines * BASE_FONT_SIZE]
end
input = DocumentGenerator::Facture::INPUT
doc_metadata = DocumentGenerator::Facture::FactureMetadata.new(facture_id: input[:id_facture])
composer = FactureComposer.new(skip_page_creation: true)
# composer.document.config['debug'] = true
composer.document.metadata.title(doc_metadata.title)
composer.document.metadata.author(doc_metadata.author)
composer.document.metadata.subject(doc_metadata.subject)
composer.document.metadata.creation_date(doc_metadata.creation_date)
composer.new_page
facture_header = <<~EOS
Facture n°001
Date de vente : 2024-04-27
Date d'émission : 2024-04-27
EOS
composer.text(facture_header, text_align: :center, margin: margin_bottom(2))
composer.text('Association Rézoléo (Trésorerie)', style: :bold, margin: margin_bottom(1))
composer.text(DocumentGenerator::Facture::INFO_REZOLEO, margin: margin_bottom(2))
composer.text('Client', style: :bold, margin: margin_bottom(1))
composer.text(DocumentGenerator::Facture::CLIENT_INFO, margin: margin_bottom(3))
items = [
{ nom_item: 'Article 1', prix: 1000, quantite: 2 },
{ nom_item: 'Article 2', prix: 2000, quantite: 1 },
]
header = lambda do |tb|
[
{ background_color: 'C0C0C0' },
[
# TODO: Fix this once HexaPDF header supports passing only strings, like for data
composer.document.layout.text('ID'),
composer.document.layout.text('Désignation article'),
composer.document.layout.text('Prix unit. HT', text_align: :right),
composer.document.layout.text('Quantité', text_align: :right),
composer.document.layout.text('TVA (1)', text_align: :right),
composer.document.layout.text('Total TTC', text_align: :right),
]
]
end
data = []
items.each_with_index do |item, i|
item_id = i + 1
item_name = item[:nom_item]
price_in_euros = item[:prix] / 100
quantity = item[:quantite]
data << [
item_id.to_s,
item_name,
composer.document.layout.text("#{'%.2f' % price_in_euros}€", text_align: :right),
composer.document.layout.text(quantity.to_s, text_align: :right),
composer.document.layout.text('0%', text_align: :right),
composer.document.layout.text("#{'%.2f' % (price_in_euros * quantity)}€", text_align: :right),
]
end
total_price = items.map { |it| it[:prix] * it[:quantite] }.sum / 100
data << [
{ content: 'Total', col_span: 5 },
composer.document.layout.text("#{'%.2f' % total_price}€", text_align: :right),
]
composer.table(data, column_widths: [-2, -8, -5, -4, -4, -4], header: header, margin: margin_bottom(2))
composer.text("Somme totale hors taxes (en euros, HT) : #{'%.2f' % total_price}€")
composer.text("Somme totale à payer toutes taxes comprises (en euros, TTC) : #{'%.2f' % total_price}€", margin: margin_bottom(1))
composer.text DocumentGenerator::Facture::CONDITIONS, font_size: 10, fill_color: '3C3C3C', margin: margin_bottom(3)
header = lambda do |tb|
[
{ background_color: 'C0C0C0', font_size: 10 },
[
# TODO: Fix this once HexaPDF header supports passing only strings, like for data
composer.document.layout.text('Date'),
composer.document.layout.text('Règlement'),
composer.document.layout.text('Montant', text_align: :right),
composer.document.layout.text('À payer', text_align: :right),
]
]
end
data = [
{ font_size: 10 },
[
'2024-04-27',
'CB',
composer.document.layout.text('40.00€', text_align: :right),
composer.document.layout.text('0.00€', text_align: :right)
]
]
composer.table(data, header: header, width: 400)
# composer.document.pages.each do |page|
# box = page.canvas.context.box(:media)
# page.canvas.font('DejaVu Sans', size: 12)
# end
composer.write('demo_hexa.pdf')