-
Notifications
You must be signed in to change notification settings - Fork 9
feat: added pdf download #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sstefdev
merged 6 commits into
main
from
73-export-invoice-as-pdf-from-the-invoice-dashboard-component
Jul 24, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f763858
feat: added pdf download
sstefdev 6bf9ec4
feat: added tooltip
sstefdev 37f01ac
chore: added try catch blocks, removed console log
sstefdev f5b1d56
chore: remove html2pdf.js package
sstefdev 19afc1b
feat: added shadcnui and toaster, removed low level try-catch blocks
sstefdev f29ff16
chore: move shadcn/ui to shared folder
sstefdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
packages/invoice-dashboard/src/utils/generateInvoice.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { formatUnits } from "viem"; | ||
| import { loadScript } from "./loadScript"; | ||
| import { calculateItemTotal } from "@requestnetwork/shared-utils/invoiceTotals"; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| html2pdf: any; | ||
| } | ||
| } | ||
|
|
||
| async function ensureHtml2PdfLoaded() { | ||
| if (typeof window.html2pdf === "undefined") { | ||
| await loadScript( | ||
| "https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export const exportToPDF = async ( | ||
| invoice: any, | ||
| currency: any, | ||
| logo: string | ||
| ) => { | ||
| await ensureHtml2PdfLoaded(); | ||
|
|
||
| const content = ` | ||
| <html> | ||
| <head> | ||
| <link rel="preconnect" href="https://fonts.googleapis.com" /> | ||
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> | ||
| <link href="https://fonts.googleapis.com/css2?family=Urbanist:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet" /> | ||
| </head> | ||
| <body> | ||
| <div id="invoice" style="font-family: Urbanist, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px;"> | ||
| <div style="display: flex; justify-content: space-between; align-items: start;"> | ||
| <img src="${logo}" alt="Logo" style="width: 50px; height: 50px;"> | ||
| <div style="text-align: right;"> | ||
| <p>Issued on ${new Date( | ||
| invoice.contentData.creationDate | ||
| ).toLocaleDateString()}</p> | ||
| <p>Payment due by ${new Date( | ||
| invoice.contentData.paymentTerms.dueDate | ||
| ).toLocaleDateString()}</p> | ||
| </div> | ||
| </div> | ||
|
|
||
| <h1 style="text-align: center; color: #333; font-size: 28px; font-style: bold; margin-bottom: 14px;">INVOICE #${ | ||
| invoice.contentData.invoiceNumber | ||
| }</h1> | ||
|
|
||
| <div style="display: flex; justify-content: space-between; margin-bottom: 20px; background-color: #FBFBFB; padding: 10px;"> | ||
| <div> | ||
| <strong>From:</strong><br> | ||
| <p style="font-size: 14px">${invoice.payee.value}</p> | ||
| ${invoice.contentData.sellerInfo.firstName ?? ""} ${ | ||
| invoice.contentData.sellerInfo.lastName ?? "" | ||
| }<br> | ||
| ${invoice.contentData.sellerInfo.address["street-address"] ?? ""}<br> | ||
| ${invoice.contentData.sellerInfo.address.locality ?? ""}${ | ||
| invoice.contentData.sellerInfo.address.locality ? "," : "" | ||
| } ${invoice.contentData.sellerInfo.address["postal-code"] ?? ""}<br> | ||
| ${invoice.contentData.sellerInfo.address["country-name"] ?? ""}<br> | ||
| ${ | ||
| invoice.contentData.sellerInfo.taxRegistration | ||
| ? `VAT: ${invoice.contentData.sellerInfo.taxRegistration}` | ||
| : "" | ||
| }<br> | ||
| </div> | ||
|
|
||
| <div> | ||
| <strong>To:</strong><br> | ||
| <p style="font-size: 14px">${invoice.payer.value}</p> | ||
| ${invoice.contentData.buyerInfo.firstName ?? ""} ${ | ||
| invoice.contentData.buyerInfo.lastName ?? "" | ||
| }<br> | ||
| ${invoice.contentData.buyerInfo.address["street-address"] ?? ""}<br> | ||
| ${invoice.contentData.buyerInfo.address.locality ?? ""}${ | ||
| invoice.contentData.sellerInfo.address.locality ? "," : "" | ||
| } ${invoice.contentData.buyerInfo.address["postal-code"] ?? ""}<br> | ||
| ${invoice.contentData.buyerInfo.address["country-name"] ?? ""}<br> | ||
| ${ | ||
| invoice.contentData.buyerInfo.taxRegistration | ||
| ? `VAT: ${invoice.contentData.buyerInfo.taxRegistration}` | ||
| : "" | ||
| }<br> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div style="margin-bottom: 20px;"> | ||
| <strong>Payment Chain:</strong> ${invoice.currencyInfo.network}<br> | ||
| <strong>Invoice Currency:</strong> ${invoice.currency}<br> | ||
| <strong>Invoice Type:</strong> Regular Invoice | ||
| </div> | ||
|
|
||
| <table style="width: 100%; border-collapse: collapse;"> | ||
| <thead> | ||
| <tr style="background-color: #f2f2f2;"> | ||
| <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Description</th> | ||
| <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Quantity</th> | ||
| <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Unit Price</th> | ||
| <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Discount</th> | ||
| <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Tax</th> | ||
| <th style="border: 1px solid #ddd; padding: 8px; text-align: right;">Amount</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| ${invoice.contentData.invoiceItems | ||
| .map( | ||
| (item: any) => ` | ||
| <tr> | ||
| <td style="border: 1px solid #ddd; padding: 8px;">${ | ||
| item.name | ||
| }</td> | ||
| <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${ | ||
| item.quantity | ||
| }</td> | ||
| <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits( | ||
| item.unitPrice, | ||
| currency.decimals | ||
| )}</td> | ||
| <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits( | ||
| item.discount, | ||
| currency.decimals | ||
| )}</td> | ||
| <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${ | ||
| item.tax.amount | ||
| }%</td> | ||
| <td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits( | ||
| calculateItemTotal(item), | ||
| currency?.decimals | ||
| )}</td> | ||
| </tr> | ||
| ` | ||
| ) | ||
| .join("")} | ||
| </tbody> | ||
| <tfoot> | ||
| <tr> | ||
| <td colspan="5" style="border: 1px solid #ddd; padding: 8px; text-align: right;"><strong>Due:</strong></td> | ||
| <td style="border: 1px solid #ddd; padding: 8px; text-align: right;"><strong>${formatUnits( | ||
| invoice.expectedAmount, | ||
| currency.decimals | ||
| )} ${invoice.currency}</strong></td> | ||
| </tr> | ||
| </tfoot> | ||
| </table> | ||
|
|
||
| ${ | ||
| invoice.contentData.note | ||
| ? `<div style="margin-top: 20px;"> | ||
| <h3>Memo:</h3> | ||
| <p>${invoice.contentData.note}</p> | ||
| </div>` | ||
| : "" | ||
| } | ||
| </div> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| const opt = { | ||
| margin: 10, | ||
| filename: `invoice-${invoice.contentData.invoiceNumber}.PDF`, | ||
| image: { type: "jpeg", quality: 0.98 }, | ||
| html2canvas: { scale: 2 }, | ||
| jsPDF: { unit: "mm", format: "a4", orientation: "portrait" }, | ||
| }; | ||
|
|
||
| window.html2pdf().from(content).set(opt).save(); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { debounce } from "./debounce"; | ||
| export { formatAddress } from "./formatAddress"; | ||
| export { exportToPDF } from "./generateInvoice"; | ||
| export { publicClientToProvider, walletClientToSigner } from "./wallet-utils"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export const loadScript = (src: string): Promise<void> => { | ||
| return new Promise((resolve, reject) => { | ||
| const script = document.createElement("script"); | ||
| script.src = src; | ||
| script.onload = () => resolve(); | ||
| script.onerror = () => reject(new Error(`Failed to load script: ${src}`)); | ||
| document.head.appendChild(script); | ||
| }); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.