-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratepdf.js
94 lines (80 loc) · 3.22 KB
/
generatepdf.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
const PDFDocument = require('pdfkit');
const fs = require('fs');
const path = require('path');
// Assuming you have all your models already imported
const Customers = require('./models/customer');
const Expenses = require('./models/expense');
const Suppliers = require('./models/supplier');
const Staff = require('./models/staff');
const Sales = require('./models/sales');
const Products = require('./models/product');
// PDF generation middleware
async function generatePDF(req, res) {
try {
// Fetch data using models
const customers = await Customers.getAllCustomers();
const expenses = await Expenses.getAllExpenses();
const suppliers = await Suppliers.getAllSuppliers();
const staffMembers = await Staff.getAllStaffMembers();
const salesData = await Sales.getAllSales();
const products = await Products.getAllProducts();
// Create a PDF document
const doc = new PDFDocument();
// Set the output file path (you can adjust this or send it directly to the browser)
const outputPath = path.join(__dirname, 'output', 'report.pdf');
doc.pipe(fs.createWriteStream(outputPath));
// Add title to the PDF
doc.fontSize(18).text('Business Report', { align: 'center' });
doc.moveDown();
// Add customer data to the PDF
doc.fontSize(12).text('Customers:', { underline: true });
customers.forEach((customer) => {
doc.text(`Name: ${customer.customer_name}, Email: ${customer.customer_email}`);
});
doc.moveDown();
// Add expense data to the PDF
doc.text('Expenses:', { underline: true });
expenses.forEach((expense) => {
doc.text(`Description: ${expense.description}, Amount: $${expense.amount}`);
});
doc.moveDown();
// Add supplier data to the PDF
doc.text('Suppliers:', { underline: true });
suppliers.forEach((supplier) => {
doc.text(`Supplier Name: ${supplier.supplier_name}, Product: ${supplier.product_name}, Supply Quantity: ${supplier.supply_qty}`);
});
doc.moveDown();
// Add staff data to the PDF
doc.text('Staff Members:', { underline: true });
staffMembers.forEach((staff) => {
doc.text(`Name: ${staff.staff_name}, Email: ${staff.staff_email}`);
});
doc.moveDown();
// Add sales data to the PDF
doc.text('Sales Data:', { underline: true });
salesData.forEach((sale) => {
doc.text(`Sale ID: ${sale.id}, Customer: ${sale.customer_name}, Total Amount: $${sale.total_amount}`);
});
doc.moveDown();
// Add product data to the PDF
doc.text('Products:', { underline: true });
products.forEach((product) => {
doc.text(`Product Name: ${product.product_name}, Category: ${product.category_name}`);
});
// Finalize the document and end the stream
doc.end();
// Respond with the generated PDF file path or send the PDF directly
res.download(outputPath, 'business_report.pdf', (err) => {
if (err) {
console.error('Error downloading the PDF:', err);
res.status(500).send('Error generating PDF');
} else {
console.log('PDF generated and sent successfully');
}
});
} catch (error) {
console.error('Error generating PDF:', error);
res.status(500).send('Error generating PDF');
}
}
module.exports = generatePDF;