ifixkart-admin/utils/pdfGenerator.ts

142 lines
4.9 KiB
TypeScript

/**
* pdfGenerator.ts
* Generates standard PDF-1.4 documents as Base64 strings without external dependencies.
*/
export interface QuotationPdfData {
quoteNumber: string;
customerName: string;
customerPhone: string;
customerEmail?: string;
date: string;
items: Array<{ description: string; qty?: number; unit_price?: number }>;
subtotal: number;
taxAmount: number;
grandTotal: number;
terms?: string;
}
export function generateQuotationPdfBase64(data: QuotationPdfData): string {
const clean = (str: string) => (str || '').replace(/[\(\)\\]/g, '\\$&');
const formattedDate = data.date || new Date().toLocaleDateString('en-IN');
const items = data.items && data.items.length > 0 ? data.items : [
{ description: 'Device Repair & Hardware Service', qty: 1, unit_price: data.subtotal }
];
// Construct PDF graphics & text stream commands
const textLines: string[] = [
`BT`,
`/F1 20 Tf`,
`50 790 Td`,
`(iFixKart Official Sales & Repair Quotation) Tj`,
`ET`,
`BT`,
`/F2 9 Tf`,
`50 772 Td`,
`(Unified Commerce & Service Platform | GSTIN: 33ABCDE1234F1Z5) Tj`,
`0 -12 Td`,
`(No. 142, Tech Park Avenue, OMR, Chennai - 600096 | Ph: +91 9080323589) Tj`,
`ET`,
// Divider Line
`0.8 0.8 0.8 RG`,
`1.5 w`,
`50 742 m 545 742 l S`,
// Meta Details
`BT`,
`/F1 11 Tf`,
`50 722 Td`,
`(QUOTE NUMBER: ${clean(data.quoteNumber)}) Tj`,
`300 0 Td`,
`(DATE: ${clean(formattedDate)}) Tj`,
`-300 -18 Td`,
`/F2 10 Tf`,
`(CUSTOMER NAME: ${clean(data.customerName)}) Tj`,
`0 -14 Td`,
`(MOBILE CONTACT: ${clean(data.customerPhone)}) Tj`,
];
if (data.customerEmail) {
textLines.push(`0 -14 Td\n(EMAIL ADDRESS: ${clean(data.customerEmail)}) Tj`);
}
// Table Headers
textLines.push(
`0 -24 Td`,
`/F1 10 Tf`,
`(ITEM / SERVICE DESCRIPTION QTY UNIT PRICE TOTAL) Tj`,
`ET`,
`0.7 0.7 0.7 RG`,
`1 w`,
`50 635 m 545 635 l S`,
`BT`,
`/F2 9 Tf`,
`50 620 Td`
);
let currentY = 620;
items.forEach((item) => {
const qty = item.qty || 1;
const price = item.unit_price || 0;
const total = qty * price;
const rawDesc = item.description || 'Service Item';
const desc = rawDesc.length > 45 ? rawDesc.substring(0, 42) + '...' : rawDesc;
const qtyStr = String(qty);
const priceStr = `Rs. ${price.toLocaleString('en-IN')}`;
const totalStr = `Rs. ${total.toLocaleString('en-IN')}`;
textLines.push(`(${clean(desc)}) Tj`);
textLines.push(`320 0 Td (${qtyStr}) Tj`);
textLines.push(`45 0 Td (${clean(priceStr)}) Tj`);
textLines.push(`65 0 Td (${clean(totalStr)}) Tj`);
textLines.push(`-430 -16 Td`);
currentY -= 16;
});
// Financial Breakdown
textLines.push(
`ET`,
`0.8 0.8 0.8 RG`,
`1 w`,
`50 ${currentY - 5} m 545 ${currentY - 5} l S`,
`BT`,
`/F2 10 Tf`,
`300 ${currentY - 22} Td (Subtotal (Excl. Tax):) Tj 125 0 Td (Rs. ${data.subtotal.toLocaleString('en-IN')}) Tj`,
`-125 -14 Td (GST Tax (18%):) Tj 125 0 Td (Rs. ${data.taxAmount.toLocaleString('en-IN')}) Tj`,
`-125 -18 Td`,
`/F1 12 Tf`,
`(GRAND TOTAL:) Tj 125 0 Td (Rs. ${data.grandTotal.toLocaleString('en-IN')}) Tj`,
`ET`,
// Terms & Warranty Box
`BT`,
`/F1 10 Tf`,
`50 ${currentY - 95} Td (WARRANTY & SERVICE TERMS:) Tj`,
`0 -14 Td`,
`/F2 9 Tf`,
`(${clean(data.terms || '6-Month IMEI Warranty included. Free express doorstep pickup & return.')}) Tj`,
`0 -14 Td`,
`(Quotation valid for 15 days from date of issue.) Tj`,
`0 -35 Td`,
`/F1 10 Tf`,
`(Authorized Signatory) Tj`,
`0 -12 Td`,
`/F2 9 Tf`,
`(iFixKart Billing & Finance Department) Tj`,
`ET`
);
const streamContent = textLines.join('\n');
const streamLength = Buffer.byteLength(streamContent, 'utf-8');
const pdfHead = `%PDF-1.4\n1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 4 0 R /F2 5 0 R >> >> /Contents 6 0 R >>\nendobj\n4 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold >>\nendobj\n5 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n6 0 obj\n<< /Length ${streamLength} >>\nstream\n`;
const pdfTail = `\nendstream\nendobj\nxref\n0 7\n0000000000 65535 f \n0000000009 00000 n \n0000000058 00000 n \n0000000115 00000 n \n0000000242 00000 n \n0000000313 00000 n \n0000000379 00000 n \ntrailer\n<< /Size 7 /Root 1 0 R >>\nstartxref\n${400 + streamLength}\n%%EOF`;
const pdfFull = pdfHead + streamContent + pdfTail;
if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
return window.btoa(unescape(encodeURIComponent(pdfFull)));
}
return Buffer.from(pdfFull, 'utf-8').toString('base64');
}