|
| 1 | +import { formatUnits } from "viem"; |
| 2 | +import { loadScript } from "./loadScript"; |
| 3 | +import { calculateItemTotal } from "@requestnetwork/shared-utils/invoiceTotals"; |
| 4 | + |
| 5 | +declare global { |
| 6 | + interface Window { |
| 7 | + html2pdf: any; |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +async function ensureHtml2PdfLoaded() { |
| 12 | + if (typeof window.html2pdf === "undefined") { |
| 13 | + await loadScript( |
| 14 | + "https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js" |
| 15 | + ); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +export const exportToPDF = async ( |
| 20 | + invoice: any, |
| 21 | + currency: any, |
| 22 | + logo: string |
| 23 | +) => { |
| 24 | + await ensureHtml2PdfLoaded(); |
| 25 | + |
| 26 | + const content = ` |
| 27 | + <html> |
| 28 | + <head> |
| 29 | + <link rel="preconnect" href="https://fonts.googleapis.com" /> |
| 30 | + <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> |
| 31 | + <link href="https://fonts.googleapis.com/css2?family=Urbanist:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet" /> |
| 32 | + </head> |
| 33 | + <body> |
| 34 | + <div id="invoice" style="font-family: Urbanist, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px;"> |
| 35 | + <div style="display: flex; justify-content: space-between; align-items: start;"> |
| 36 | + <img src="${logo}" alt="Logo" style="width: 50px; height: 50px;"> |
| 37 | + <div style="text-align: right;"> |
| 38 | + <p>Issued on ${new Date( |
| 39 | + invoice.contentData.creationDate |
| 40 | + ).toLocaleDateString()}</p> |
| 41 | + <p>Payment due by ${new Date( |
| 42 | + invoice.contentData.paymentTerms.dueDate |
| 43 | + ).toLocaleDateString()}</p> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | +
|
| 47 | + <h1 style="text-align: center; color: #333; font-size: 28px; font-style: bold; margin-bottom: 14px;">INVOICE #${ |
| 48 | + invoice.contentData.invoiceNumber |
| 49 | + }</h1> |
| 50 | +
|
| 51 | + <div style="display: flex; justify-content: space-between; margin-bottom: 20px; background-color: #FBFBFB; padding: 10px;"> |
| 52 | + <div> |
| 53 | + <strong>From:</strong><br> |
| 54 | + <p style="font-size: 14px">${invoice.payee.value}</p> |
| 55 | + ${invoice.contentData.sellerInfo.firstName ?? ""} ${ |
| 56 | + invoice.contentData.sellerInfo.lastName ?? "" |
| 57 | + }<br> |
| 58 | + ${invoice.contentData.sellerInfo.address["street-address"] ?? ""}<br> |
| 59 | + ${invoice.contentData.sellerInfo.address.locality ?? ""}${ |
| 60 | + invoice.contentData.sellerInfo.address.locality ? "," : "" |
| 61 | + } ${invoice.contentData.sellerInfo.address["postal-code"] ?? ""}<br> |
| 62 | + ${invoice.contentData.sellerInfo.address["country-name"] ?? ""}<br> |
| 63 | + ${ |
| 64 | + invoice.contentData.sellerInfo.taxRegistration |
| 65 | + ? `VAT: ${invoice.contentData.sellerInfo.taxRegistration}` |
| 66 | + : "" |
| 67 | + }<br> |
| 68 | + </div> |
| 69 | +
|
| 70 | + <div> |
| 71 | + <strong>To:</strong><br> |
| 72 | + <p style="font-size: 14px">${invoice.payer.value}</p> |
| 73 | + ${invoice.contentData.buyerInfo.firstName ?? ""} ${ |
| 74 | + invoice.contentData.buyerInfo.lastName ?? "" |
| 75 | + }<br> |
| 76 | + ${invoice.contentData.buyerInfo.address["street-address"] ?? ""}<br> |
| 77 | + ${invoice.contentData.buyerInfo.address.locality ?? ""}${ |
| 78 | + invoice.contentData.sellerInfo.address.locality ? "," : "" |
| 79 | + } ${invoice.contentData.buyerInfo.address["postal-code"] ?? ""}<br> |
| 80 | + ${invoice.contentData.buyerInfo.address["country-name"] ?? ""}<br> |
| 81 | + ${ |
| 82 | + invoice.contentData.buyerInfo.taxRegistration |
| 83 | + ? `VAT: ${invoice.contentData.buyerInfo.taxRegistration}` |
| 84 | + : "" |
| 85 | + }<br> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + |
| 89 | + <div style="margin-bottom: 20px;"> |
| 90 | + <strong>Payment Chain:</strong> ${invoice.currencyInfo.network}<br> |
| 91 | + <strong>Invoice Currency:</strong> ${invoice.currency}<br> |
| 92 | + <strong>Invoice Type:</strong> Regular Invoice |
| 93 | + </div> |
| 94 | + |
| 95 | + <table style="width: 100%; border-collapse: collapse;"> |
| 96 | + <thead> |
| 97 | + <tr style="background-color: #f2f2f2;"> |
| 98 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Description</th> |
| 99 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Quantity</th> |
| 100 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Unit Price</th> |
| 101 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Discount</th> |
| 102 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Tax</th> |
| 103 | + <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Amount</th> |
| 104 | + </tr> |
| 105 | + </thead> |
| 106 | + <tbody> |
| 107 | + ${invoice.contentData.invoiceItems |
| 108 | + .map( |
| 109 | + (item: any) => ` |
| 110 | + <tr> |
| 111 | + <td style="border: 1px solid #ddd; padding: 8px;">${ |
| 112 | + item.name |
| 113 | + }</td> |
| 114 | + <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${ |
| 115 | + item.quantity |
| 116 | + }</td> |
| 117 | + <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits( |
| 118 | + item.unitPrice, |
| 119 | + currency.decimals |
| 120 | + )}</td> |
| 121 | + <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits( |
| 122 | + item.discount, |
| 123 | + currency.decimals |
| 124 | + )}</td> |
| 125 | + <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${ |
| 126 | + item.tax.amount |
| 127 | + }%</td> |
| 128 | + <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits( |
| 129 | + calculateItemTotal(item), |
| 130 | + currency?.decimals |
| 131 | + )}</td> |
| 132 | + </tr> |
| 133 | + ` |
| 134 | + ) |
| 135 | + .join("")} |
| 136 | + </tbody> |
| 137 | + <tfoot> |
| 138 | + <tr> |
| 139 | + <td colspan="5" style="border: 1px solid #ddd; padding: 8px; text-align: right;"><strong>Due:</strong></td> |
| 140 | + <td style="border: 1px solid #ddd; padding: 8px; text-align: right;"><strong>${formatUnits( |
| 141 | + invoice.expectedAmount, |
| 142 | + currency.decimals |
| 143 | + )} ${invoice.currency}</strong></td> |
| 144 | + </tr> |
| 145 | + </tfoot> |
| 146 | + </table> |
| 147 | + |
| 148 | + ${ |
| 149 | + invoice.contentData.note |
| 150 | + ? `<div style="margin-top: 20px;"> |
| 151 | + <h3>Memo:</h3> |
| 152 | + <p>${invoice.contentData.note}</p> |
| 153 | + </div>` |
| 154 | + : "" |
| 155 | + } |
| 156 | + </div> |
| 157 | + </body> |
| 158 | + </html> |
| 159 | + `; |
| 160 | + |
| 161 | + const opt = { |
| 162 | + margin: 10, |
| 163 | + filename: `invoice-${invoice.contentData.invoiceNumber}.PDF`, |
| 164 | + image: { type: "jpeg", quality: 0.98 }, |
| 165 | + html2canvas: { scale: 2 }, |
| 166 | + jsPDF: { unit: "mm", format: "a4", orientation: "portrait" }, |
| 167 | + }; |
| 168 | + |
| 169 | + window.html2pdf().from(content).set(opt).save(); |
| 170 | +}; |
0 commit comments