+
Subtotal${subtotal.toFixed(2)}
{discountTotal > 0 && (
-
- Discount
- -${discountTotal.toFixed(2)}
-
+
Discount-${discountTotal.toFixed(2)}
)}
-
- Tax
- ${taxTotal.toFixed(2)}
-
+
Tax${taxTotal.toFixed(2)}
{rounding !== 0 && (
-
- Rounding
- {rounding > 0 ? '+' : ''}{rounding.toFixed(2)}
-
+
Rounding{rounding > 0 ? '+' : ''}{rounding.toFixed(2)}
)}
-
-
TOTAL
-
${total.toFixed(2)}
+
+ TOTAL${total.toFixed(2)}
{/* Payment */}
-
-
- Payment
- {txn.paymentMethod?.replace('_', ' ') ?? 'N/A'}
-
+
+
Payment{txn.paymentMethod?.replace('_', ' ') ?? 'N/A'}
{tendered !== null && (
-
- Tendered
- ${tendered.toFixed(2)}
-
+
Tendered${tendered.toFixed(2)}
)}
{change !== null && change > 0 && (
-
- Change
- ${change.toFixed(2)}
-
+
Change${change.toFixed(2)}
)}
{/* Barcode */}
-
+
{/* Footer */}
{(config?.footer || footerText) && (
-
{config?.footer || footerText}
+
{config?.footer || footerText}
)}
{config?.returnPolicy && (
-
{config.returnPolicy}
+
{config.returnPolicy}
)}
{config?.social && (
-
{config.social}
+
{config.social}
)}
)
}
-export async function saveReceiptPDF(txnNumber?: string): Promise
{
+export function printReceipt() {
const el = document.getElementById('pos-receipt-print')
- if (!el) return null
+ if (!el) return
- const html2pdf = (await import('html2pdf.js')).default
- const blob: Blob = await html2pdf()
- .set({
- margin: [4, 4, 4, 4],
- filename: `receipt-${txnNumber ?? 'unknown'}.pdf`,
- image: { type: 'jpeg', quality: 0.95 },
- html2canvas: { scale: 2, useCORS: true },
- jsPDF: { unit: 'mm', format: [80, 200], orientation: 'portrait' },
- })
- .from(el)
- .outputPdf('blob')
+ // Clone the receipt into an iframe for clean printing
+ const iframe = document.createElement('iframe')
+ iframe.style.cssText = 'position:fixed;left:-9999px;width:400px;height:800px;border:none;'
+ document.body.appendChild(iframe)
- return blob
+ const doc = iframe.contentDocument
+ if (!doc) { document.body.removeChild(iframe); return }
+
+ doc.open()
+ doc.write(`${el.innerHTML}`)
+ doc.close()
+
+ // Wait for content to render then print
+ setTimeout(() => {
+ try {
+ iframe.contentWindow?.focus()
+ iframe.contentWindow?.print()
+ } catch {
+ // Fallback: just use window.print
+ window.print()
+ }
+ setTimeout(() => document.body.removeChild(iframe), 2000)
+ }, 300)
}
export async function downloadReceiptPDF(txnNumber?: string) {
- const blob = await saveReceiptPDF(txnNumber)
- if (!blob) return
+ const el = document.getElementById('pos-receipt-print')
+ if (!el) return
- const url = URL.createObjectURL(blob)
- const a = document.createElement('a')
- a.href = url
- a.download = `receipt-${txnNumber ?? 'unknown'}.pdf`
- a.click()
- URL.revokeObjectURL(url)
-}
-
-export async function printReceiptPDF(txnNumber?: string) {
- const blob = await saveReceiptPDF(txnNumber)
- if (!blob) return
-
- const url = URL.createObjectURL(blob)
- const printWindow = window.open(url)
- if (printWindow) {
- printWindow.onload = () => {
- printWindow.print()
- }
- }
- // Clean up after a delay
- setTimeout(() => URL.revokeObjectURL(url), 30000)
+ const html2pdf = (await import('html2pdf.js')).default
+ html2pdf()
+ .set({
+ margin: [2, 2, 2, 2],
+ filename: `receipt-${txnNumber ?? 'unknown'}.pdf`,
+ image: { type: 'jpeg', quality: 0.95 },
+ html2canvas: { scale: 2, useCORS: true, width: 280 },
+ jsPDF: { unit: 'mm', format: [72, 250], orientation: 'portrait' },
+ })
+ .from(el)
+ .save()
}