feat : Home page password Expiry message
feat : Statement Download
This commit is contained in:
95
src/app/_components/statement_download/PdfGenerator.tsx
Normal file
95
src/app/_components/statement_download/PdfGenerator.tsx
Normal file
@@ -0,0 +1,95 @@
|
||||
"use client";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
export const generatePDF = (
|
||||
accountNo: string,
|
||||
balance: string,
|
||||
txns: any[],
|
||||
) => {
|
||||
const html2pdf = require("html2pdf.js");
|
||||
|
||||
// Header with logo + bank name + generated date
|
||||
const headerHTML = `
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:10px;">
|
||||
<div style="display:flex;align-items:center;gap:10px;">
|
||||
<img src="/logo.jpg" alt="Bank Logo" style="height:50px;" />
|
||||
<h2 style="margin:0;">The Kangra Central Co Operative Bank</h2>
|
||||
</div>
|
||||
<div style="font-size:12px;color:#555;">
|
||||
Report generated: ${dayjs().format("DD/MM/YYYY HH:mm")}
|
||||
</div>
|
||||
</div>
|
||||
<hr/>
|
||||
`;
|
||||
|
||||
// Table rows
|
||||
const rows = txns.map(
|
||||
(t: any) => `
|
||||
<tr style="page-break-inside: avoid;">
|
||||
<td style="border:1px solid #ccc;padding:6px;text-align:left;">${t.name}</td>
|
||||
<td style="border:1px solid #ccc;padding:6px;text-align:left;">${t.date}</td>
|
||||
<td style="border:1px solid #ccc;padding:6px;text-align:right;color:${t.type === "DR" ? "red" : "green"
|
||||
}">
|
||||
${parseFloat(t.amount).toLocaleString("en-IN", {
|
||||
minimumFractionDigits: 2,
|
||||
})} ${t.type === "DR" ? "dr." : "cr."}
|
||||
</td>
|
||||
</tr>
|
||||
`
|
||||
);
|
||||
|
||||
const content = `
|
||||
<div style="font-family:Arial, sans-serif;">
|
||||
${headerHTML}
|
||||
<h3>Account Statement</h3>
|
||||
<p><strong>Account No:</strong> ${accountNo}</p>
|
||||
<p><strong>Available Balance:</strong> ₹ ${parseFloat(
|
||||
balance
|
||||
).toLocaleString("en-IN", { minimumFractionDigits: 2 })}</p>
|
||||
|
||||
<table style="width:100%;border-collapse:collapse;font-size:12px;margin-top:10px;page-break-inside:auto;">
|
||||
<thead style="background:#f0f0f0;">
|
||||
<tr>
|
||||
<th style="border:1px solid #ccc;padding:6px;text-align:left;">Name</th>
|
||||
<th style="border:1px solid #ccc;padding:6px;text-align:left;">Date</th>
|
||||
<th style="border:1px solid #ccc;padding:6px;text-align:right;">Amount (₹)</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${rows.join("")}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div style="height:40px;"></div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// PDF options
|
||||
const opt = {
|
||||
margin: [10, 10, 20, 10], // bottom margin for page count
|
||||
filename: `AccountStatement_${accountNo}.pdf`,
|
||||
image: { type: "jpeg", quality: 0.98 },
|
||||
html2canvas: { scale: 2 },
|
||||
jsPDF: { unit: "mm", format: "a4", orientation: "portrait" },
|
||||
pagebreak: { mode: ["avoid-all", "css", "legacy"] },
|
||||
};
|
||||
|
||||
html2pdf()
|
||||
.set(opt)
|
||||
.from(content)
|
||||
.toPdf()
|
||||
.get("pdf")
|
||||
.then((pdf: any) => {
|
||||
const totalPages = pdf.internal.getNumberOfPages();
|
||||
for (let i = 1; i <= totalPages; i++) {
|
||||
pdf.setPage(i);
|
||||
pdf.setFontSize(10);
|
||||
pdf.text(
|
||||
`Page ${i} of ${totalPages}`,
|
||||
pdf.internal.pageSize.getWidth() - 40,
|
||||
pdf.internal.pageSize.getHeight() - 10
|
||||
);
|
||||
}
|
||||
})
|
||||
.save();
|
||||
};
|
||||
Reference in New Issue
Block a user