|
| 1 | +import React from "react"; |
| 2 | +import { |
| 3 | + Document, |
| 4 | + Page, |
| 5 | + Text, |
| 6 | + View, |
| 7 | + Image, |
| 8 | + StyleSheet, |
| 9 | +} from "@react-pdf/renderer"; |
| 10 | +import { generatePDF } from "../utils/pdfGenerator"; |
| 11 | +import { cartItem } from "../types/cart"; |
| 12 | + |
| 13 | +interface InvoiceData { |
| 14 | + products: cartItem[]; |
| 15 | + clientAddress: string; |
| 16 | + companyAddress: string; |
| 17 | + logoUrl?: string; |
| 18 | +} |
| 19 | + |
| 20 | +const styles = StyleSheet.create({ |
| 21 | + page: { padding: 30 }, |
| 22 | + header: { flexDirection: "row", marginBottom: 20, alignItems: "center" }, |
| 23 | + logo: { width: 50, height: 50 }, |
| 24 | + title: { fontSize: 24, marginLeft: 20 }, |
| 25 | + addressSection: { |
| 26 | + flexDirection: "row", |
| 27 | + justifyContent: "space-between", |
| 28 | + marginBottom: 20, |
| 29 | + }, |
| 30 | + addressTitle: { fontWeight: "bold", marginBottom: 5 }, |
| 31 | + address: { fontSize: 10 }, |
| 32 | + table: { flexGrow: 1 }, |
| 33 | + tableRow: { |
| 34 | + flexDirection: "row", |
| 35 | + borderBottomWidth: 1, |
| 36 | + borderBottomColor: "#000", |
| 37 | + alignItems: "center", |
| 38 | + }, |
| 39 | + tableHeader: { width: "20%", padding: 5, fontWeight: "bold" }, |
| 40 | + tableCell: { width: "20%", padding: 5 }, |
| 41 | + productImage: { width: 30, height: 30 }, |
| 42 | + total: { marginTop: 20, alignItems: "flex-end" }, |
| 43 | + totalText: { fontWeight: "bold" }, |
| 44 | +}); |
| 45 | + |
| 46 | +const InvoiceDocument: React.FC<{ data: InvoiceData }> = ({ data }) => ( |
| 47 | + <Document> |
| 48 | + <Page size="A4" style={styles.page}> |
| 49 | + {/* Header */} |
| 50 | + <View style={styles.header}> |
| 51 | + <Image style={styles.logo} src={data.logoUrl} /> |
| 52 | + <Text style={styles.title}>Invoice</Text> |
| 53 | + </View> |
| 54 | + |
| 55 | + {/* Addresses */} |
| 56 | + <View style={styles.addressSection}> |
| 57 | + <View> |
| 58 | + <Text style={styles.addressTitle}>Bill To:</Text> |
| 59 | + <Text style={styles.address}>{data.clientAddress}</Text> |
| 60 | + </View> |
| 61 | + <View> |
| 62 | + <Text style={styles.addressTitle}>From:</Text> |
| 63 | + <Text style={styles.address}>{data.companyAddress}</Text> |
| 64 | + </View> |
| 65 | + </View> |
| 66 | + |
| 67 | + {/* Product Table */} |
| 68 | + <View style={styles.table}> |
| 69 | + <View style={styles.tableRow}> |
| 70 | + <Text style={styles.tableHeader}>Image</Text> |
| 71 | + <Text style={styles.tableHeader}>Name</Text> |
| 72 | + <Text style={styles.tableHeader}>Price</Text> |
| 73 | + <Text style={styles.tableHeader}>Quantity</Text> |
| 74 | + <Text style={styles.tableHeader}>Total</Text> |
| 75 | + </View> |
| 76 | + {data.products.map((product, index) => ( |
| 77 | + <View style={styles.tableRow} key={index}> |
| 78 | + <Image style={styles.productImage} src={product.image} /> |
| 79 | + <Text style={styles.tableCell}>{product.name}</Text> |
| 80 | + <Text style={styles.tableCell}>${product.price.toFixed(2)}</Text> |
| 81 | + <Text style={styles.tableCell}>{product.quantity}</Text> |
| 82 | + <Text style={styles.tableCell}> |
| 83 | + ${(product.price * product.quantity).toFixed(2)} |
| 84 | + </Text> |
| 85 | + </View> |
| 86 | + ))} |
| 87 | + </View> |
| 88 | + |
| 89 | + {/* Total */} |
| 90 | + <View style={styles.total}> |
| 91 | + <Text style={styles.totalText}> |
| 92 | + Total: $ |
| 93 | + {data.products |
| 94 | + .reduce((sum, product) => sum + product.price * product.quantity, 0) |
| 95 | + .toFixed(2)} |
| 96 | + </Text> |
| 97 | + </View> |
| 98 | + </Page> |
| 99 | + </Document> |
| 100 | +); |
| 101 | + |
| 102 | +export async function generateInvoice(data: InvoiceData): Promise<Buffer> { |
| 103 | + return await generatePDF(<InvoiceDocument data={data} />); |
| 104 | +} |
0 commit comments