Skip to content

Commit ba8d90f

Browse files
committed
pdf of sales report download done with proper alignment
1 parent 2c52bfd commit ba8d90f

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

controllers/adminController.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ const generatePdf = async (req, res) => {
306306
const doc = new PDFDocument();
307307
const filename = 'sales-report.pdf';
308308
const orders = req.body;
309+
// console.log(orders);
309310
res.setHeader('Content-Type', 'application/pdf');
310311
res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
311312
doc.pipe(res);
@@ -331,27 +332,32 @@ const generatePdf = async (req, res) => {
331332

332333
const headers = ['Order ID', 'Name', 'Date', 'Total'];
333334

334-
let headerX = 20;
335-
const headerY = doc.y + 10;
335+
let headerX = 20;
336+
const headerY = doc.y + 10;
336337

337-
doc.text(headers[0], headerX, headerY);
338-
headerX += 200;
338+
doc.text(headers[0], headerX, headerY);
339+
headerX += 200;
339340

340-
headers.slice(1).forEach(header => {
341-
doc.text(header, headerX, headerY);
342-
headerX += 130;
343-
});
341+
headers.slice(1).forEach(header => {
342+
doc.text(header, headerX, headerY);
343+
headerX += 130;
344+
});
344345

345-
let dataY = headerY + 25;
346+
let dataY = headerY + 25;
346347

347-
orders.forEach(order => {
348-
doc.text(order.dataId, 20, dataY);
349-
doc.text(order.name, 210, dataY);
350-
doc.text(order.date, 350, dataY);
351-
doc.text(order.totalAmount, 480, dataY);
352-
dataY += 30;
348+
orders.forEach(order => {
349+
const cleanedDataId = order.dataId.trim();
350+
const cleanedName = order.name.trim();
353351

354-
});
352+
doc.text(cleanedDataId, 20, dataY, { width: 200 });
353+
doc.text(cleanedName, 230, dataY);
354+
doc.text(order.date, 350, dataY, { width: 120 });
355+
doc.text(order.totalAmount, 490, dataY);
356+
357+
dataY += 30;
358+
});
359+
360+
355361

356362
doc.end();
357363
} catch (error) {
@@ -360,9 +366,9 @@ const generatePdf = async (req, res) => {
360366
}
361367

362368

363-
const downloadExcel = async (req, res)=>{
369+
const downloadExcel = async (req, res) => {
364370
try {
365-
371+
366372
const workbook = new ExcelJS.Workbook();
367373
const worksheet = workbook.addWorksheet('Sales Report');
368374

views/admin/salesReport.ejs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,17 @@
164164
// Iterate through rows in the table
165165
for (const row of tableBody.rows) {
166166
// Access individual cells in the row
167-
const dataId = row.cells[1].textContent.trim();
167+
const dataId = row.cells[1].textContent
168168
const name = row.cells[2].textContent;
169-
const date = row.cells[4].textContent;
169+
const dateFull = row.cells[4].textContent;
170+
const dateOnly = dateFull.split(',')[0].trim();
170171
let totalAmount = row.cells[7].textContent.trim();
171172
totalAmount = parseFloat(totalAmount.replace(/[^0-9]/g, ''), 10);
172173
// Do something with these values, for example, log them
173174
const dataData = {
174175
dataId: dataId,
175176
name: name,
176-
date: date,
177+
date: dateOnly,
177178
totalAmount: totalAmount
178179
}
179180

0 commit comments

Comments
 (0)