-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf-lambda-function.js
255 lines (207 loc) · 5.79 KB
/
pdf-lambda-function.js
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
const PDFDocument = require("pdfkit");
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const download = require("image-downloader");
exports.handler = async (event) => {
const data = event;
const pdfDoc = new PDFDocument({ size: "A4", margin: 50 });
await generateHeader(pdfDoc);
generateCustomerInformation(pdfDoc, data);
generateInvoiceTable(pdfDoc, data);
generateFooter(pdfDoc);
const pdfBuffer = await new Promise((resolve, reject) => {
const buffers = [];
pdfDoc.on('data', buffers.push.bind(buffers));
pdfDoc.on('end', () => {
resolve(Buffer.concat(buffers));
});
pdfDoc.end();
});
const timestamp = new Date().toISOString().replace(/:/g, '-');
const fileName = `boleta_${timestamp}.pdf`; // Para que todos los pdf se llamen distinto
try {
const params = {
Bucket: 'boletasvuelos',
Key: fileName,
Body: pdfBuffer,
ContentType: 'application/pdf'
};
await s3.upload(params).promise();
const url = `https://boletasvuelos.s3.amazonaws.com/${fileName}`;
return `Boleta generada exitosamente. Puedes verla aquí: ${url}`;
} catch (error) {
console.error('Error al subir el PDF a S3:', error);
throw error;
}
};
async function generateHeader(doc) {
const logoUrl = "https://boletasvuelos.s3.us-east-2.amazonaws.com/logo.jpg";
const options = {
url: logoUrl,
dest: '/tmp/logo.jpg' // Save to /tmp directory in Lambda environment
};
try {
const { filename } = await download.image(options);
console.log("Imagen descargada en: ", filename);
doc
.image(filename, 40, 0, { width: 160})
.fillColor("#444444")
.fontSize(10)
.text("VuelosNicolás", 200, 50, { align: "right" })
.text("Av. Vicuña Mackenna 4686", 200, 65, { align: "right" })
.text("Macul, Santiago de Chile", 200, 80, { align: "right" })
.moveDown();
} catch (error) {
console.error("Error downloading the image: ", error);
}
}
function generateCustomerInformation(doc, invoice) {
doc
.fillColor("#444444")
.fontSize(20)
.text("Boleta electrónica", 50, 160);
generateHr(doc, 185);
const customerInformationTop = 200;
doc
.fontSize(10)
.font("Helvetica-Bold")
.text("Datos del pasajero:", 50, customerInformationTop)
.font("Helvetica")
.text(invoice.name, 50, customerInformationTop + 15)
.text(invoice.email, 50, customerInformationTop + 30)
.font("Helvetica-Bold")
.text("Fecha actual:", 300, customerInformationTop)
.font("Helvetica")
.text(formatDate(new Date()), 400, customerInformationTop)
.font("Helvetica-Bold")
.text("Fecha de compra:", 300, customerInformationTop + 15)
.font("Helvetica")
.text(invoice.fecha_compra, 400, customerInformationTop + 15)
.moveDown();
generateHr(doc, 267);
}
function generateInvoiceTable(doc, invoice) {
doc
.fontSize(15)
.font("Helvetica-Bold")
.text("Detalles del vuelo", 50, 300, { align: "left" });
const invoiceTableTop = 330;
doc.font("Helvetica-Bold");
generateTableRow5(
doc,
invoiceTableTop,
"Origen",
"Destino",
"Duración",
"Fecha",
"Hora"
);
generateHr(doc, invoiceTableTop + 20);
doc.font("Helvetica");
generateTableRow5(
doc,
invoiceTableTop + 30,
invoice.origen,
invoice.destino,
invoice.duracion,
invoice.fecha,
invoice.hora,
);
generateHr(doc, invoiceTableTop + 50);
doc
.fontSize(15)
.font("Helvetica-Bold")
.text("Datos del billete", 50, 410, { align: "left" });
// Segunda fila de información
const invoiceTableMiddle = 440;
doc.font("Helvetica-Bold");
generateTableRow4(
doc,
invoiceTableMiddle,
"Id viaje",
"Aerolínea",
"Costo",
"Cantidad",
);
generateHr(doc, invoiceTableMiddle + 20);
doc.font("Helvetica");
generateTableRow4(
doc,
invoiceTableMiddle + 30,
invoice.id_viaje,
invoice.aerolinea,
formatCurrency(invoice.precio),
invoice.cantidad
);
generateHr(doc, invoiceTableMiddle + 50);
const duePosition = invoiceTableMiddle + 80;
doc.font("Helvetica-Bold");
generateTableRow4(
doc,
duePosition,
"",
"",
"Total a pagar",
formatCurrency(invoice.precio * invoice.cantidad)
);
doc.font("Helvetica");
}
function generateFooter(doc) {
doc
.fontSize(10)
.text(
"Muchas gracias por confiar en nosotros.",
50,
780,
{ align: "center", width: 500 }
);
}
function generateTableRow4(
doc,
y,
id_viaje,
aerolinea,
precio,
cantidad
) {
doc
.fontSize(10)
.text(id_viaje, 50, y)
.text(aerolinea, 150, y)
.text(precio, 280, y, { width: 90, align: "right" })
.text(cantidad, 0, y, {align: "right" })
}
function generateTableRow5(
doc,
y,
origen,
destino,
duracion,
fecha,
hora,
) {
doc
.fontSize(10)
.text(origen, 50, y)
.text(destino, 180, y)
.text(duracion, 300, y)
.text(fecha, 370, y)
.text(hora, 0, y, { align: "right" })
}
function generateHr(doc, y) {
doc
.strokeColor("#aaaaaa")
.lineWidth(1)
.moveTo(50, y)
.lineTo(550, y)
.stroke();
}
function formatCurrency(cents) {
return "$" + cents ;
}
function formatDate(date) {
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
return day + "/" + + month + "/" + year;
}