-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfGenerator1.js
More file actions
180 lines (150 loc) · 5.13 KB
/
pdfGenerator1.js
File metadata and controls
180 lines (150 loc) · 5.13 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
const express = require('express');
const mysql = require('mysql2/promise');
const path = require('path');
const app = express();
const port = process.env.PORT || 25537;
const fs = require('fs');
const PDFDocument = require('pdfkit');
// Configuração do banco de dados MySQL
const dbConfig = {
connectionLimit: 10,
connectTimeout: 60000,
host: process.env.MYSQL_HOST || 'mysql-2221c92b-danielcalebe719-2b82.l.aivencloud.com',
port: 25538,
user: process.env.MYSQL_USER || 'avnadmin',
password: process.env.MYSQL_PASSWORD || 'AVNS_MRgcH_4ZC0MTrUnxZpv',
database: process.env.MYSQL_DATABASE || 'defaultdb',
};
// Middleware para definir o Content-Type como application/json
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Pool de conexões MySQL
const pool = mysql.createPool(dbConfig);
// Teste de conexão com o MySQL
async function testMySQLConnection() {
try {
const connection = await pool.getConnection();
console.log('Conexão com o banco de dados MySQL estabelecida com sucesso!');
connection.release();
} catch (error) {
console.error('Erro ao conectar ao banco de dados MySQL:', error);
}
}
// Chama a função de teste de conexão antes de iniciar o servidor
testMySQLConnection();
// Função para gerar o PDF
async function generatePDF(res) {
const doc = new PDFDocument({
margin: 50,
bufferPages: true,
font: 'Helvetica',
});
const colors = {
primary: '#A7C6ED',
secondary: '#FFB3B3',
text: '#333333',
};
const fonts = {
header: 'Helvetica-Bold',
body: 'Helvetica',
};
const query = `
SELECT
'Pedidos' AS tipo,
p.id AS id,
c.nome AS cliente_nome,
c.email AS cliente_email,
c.telefone AS cliente_telefone,
p.observacao AS observacao,
p.status AS status,
COALESCE(p.totalPedido, 0) AS total,
p.dataPedido AS data
FROM pedidos p
LEFT JOIN clientes c ON p.idCliente = c.id
WHERE p.dataPedido >= DATE_SUB(NOW(), INTERVAL 30 DAY)
UNION ALL
SELECT
'Gastos' AS tipo,
g.id AS id,
NULL AS cliente_nome,
NULL AS cliente_email,
NULL AS cliente_telefone,
g.motivo AS observacao,
g.status AS status, -- Certifique-se de que agora existe na tabela
COALESCE(g.valor, 0) AS total,
g.dataCadastro AS data
FROM gastos g
WHERE g.dataCadastro >= DATE_SUB(NOW(), INTERVAL 30 DAY)
ORDER BY tipo, id
`;
try {
const connection = await mysql.createConnection(dbConfig);
const [results] = await connection.execute(query);
connection.end();
let yPos = 150; // Posição inicial do conteúdo
function drawNewPage() {
if (yPos > doc.page.height - doc.page.margins.bottom) {
doc.addPage();
drawHeader(doc, colors, fonts);
yPos = 150; // Resetar yPos
}
}
drawHeader(doc, colors, fonts);
results.forEach((row, index) => {
drawNewPage(); // Verifica se precisa adicionar uma nova página
doc.font(fonts.body).fontSize(8);
doc.text(row.tipo, doc.page.margins.left, yPos);
doc.text(row.id.toString(), doc.page.margins.left + 50, yPos);
doc.text(row.cliente_nome || '', doc.page.margins.left + 100, yPos);
doc.text(row.status || '', doc.page.margins.left + 200, yPos);
doc.text(row.total.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }), doc.page.margins.left + 250, yPos);
doc.text(formatDate(row.data), doc.page.margins.left + 300, yPos);
if (index < results.length - 1) {
doc.moveTo(doc.page.margins.left, yPos + 10).lineTo(doc.page.width - doc.page.margins.right, yPos + 10).stroke();
}
yPos += 30; // Aumentar a posição Y para a próxima linha
});
drawFooter(doc, colors, fonts);
const outputFileName = 'relatorio_financeiro.pdf';
const outputStream = fs.createWriteStream(outputFileName);
doc.pipe(outputStream);
doc.end();
res.download(outputFileName, (err) => {
if (err) {
console.error('Erro ao realizar o download:', err);
} else {
fs.unlinkSync(outputFileName);
}
});
} catch (error) {
console.error('Erro ao executar consulta:', error);
res.status(500).send('Erro ao executar consulta: ' + error.message);
}
}
function drawHeader(doc, colors, fonts) {
doc.fillColor(colors.primary)
.rect(doc.page.margins.left, 50, doc.page.width - 2 * doc.page.margins.left, 50)
.fill();
doc.fillColor(colors.text)
.font(fonts.header)
.fontSize(16)
.text('Relatório Financeiro', doc.page.margins.left + 20, 60);
doc.moveTo(doc.page.margins.left, 100).lineTo(doc.page.width - doc.page.margins.right, 100).stroke();
}
function drawFooter(doc, colors, fonts) {
const footerText = `Contato: buffetdivinosabor@orgs.com | Data de Geração: ${new Date().toLocaleDateString()} `;
doc.fontSize(8)
.text(footerText, doc.page.margins.left, doc.page.height - 50, {
width: doc.page.width - 2 * doc.page.margins.left,
align: 'center'
});
}
function formatDate(date) {
return new Date(date).toLocaleDateString('pt-BR');
}
app.get('/gerar-relatorio-financeiro', (req, res) => {
generatePDF(res);
});
app.listen(port, () => {
console.log(`Servidor Express iniciado na porta ${port}`);
});