Add Receipt Generator tool
Same pattern as the Payslip Generator: self-contained, entirely client-side (fill in details, live preview, download PDF), no server- side logic, so no logic.py - just a route serving the page. Applied the same fixes proactively, since it's the same design/PDF pattern that had these exact issues: - SRI-pinned standalone html2canvas + jsPDF instead of html2pdf.js, whose bundled html2canvas mis-renders shorter content on this kind of page (confirmed on the Payslip Generator via a controlled A/B test against the current standalone html2canvas). - Page-fit math carried over: hotfixes:['px_scaling'], height clamped to >= width with explicit orientation:'portrait' (avoids jsPDF swapping width/height for landscape-shaped short content), +8px buffer against a sub-pixel-rounding extra-page bug. - Header logo linked back to / (every other tool's header does this). - Box-shadow stripped on capture - screen chrome, not part of the document. - Wired into the platform: Blueprint at /receipt/, registered in main.py, landing-page card added. Verified end-to-end (headless Chromium, real Download button) across defaults, zero items, and a tall/complex receipt with discount/tax/ tip/cash payment - single page, fully rendered, no cropping. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
3336bf98bb
commit
a3196200de
13
app/main.py
13
app/main.py
|
|
@ -5,6 +5,7 @@ from tools.eib import eib_bp
|
|||
from tools.donor_report import donor_report_bp
|
||||
from tools.financial_reports import financial_reports_bp
|
||||
from tools.payslip import payslip_bp
|
||||
from tools.receipt import receipt_bp
|
||||
|
||||
# ── Tool registry ────────────────────────────────────────────────────────────
|
||||
# Each entry drives one card on the landing page. Add a new tool by
|
||||
|
|
@ -54,6 +55,17 @@ TOOLS = [
|
|||
"url": "/payslip/",
|
||||
"status": "online",
|
||||
},
|
||||
{
|
||||
"name": "Receipt Generator",
|
||||
"tagline": "Fill in, preview, download PDF",
|
||||
"description": (
|
||||
"Generates a formatted point-of-sale style receipt from business "
|
||||
"details, line items, discount/tax/tip, entirely in the browser - "
|
||||
"nothing is uploaded to a server."
|
||||
),
|
||||
"url": "/receipt/",
|
||||
"status": "online",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -67,6 +79,7 @@ def create_app():
|
|||
app.register_blueprint(donor_report_bp)
|
||||
app.register_blueprint(financial_reports_bp)
|
||||
app.register_blueprint(payslip_bp)
|
||||
app.register_blueprint(receipt_bp)
|
||||
|
||||
@app.route("/")
|
||||
def home():
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
from .routes import receipt_bp
|
||||
|
||||
__all__ = ["receipt_bp"]
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
from flask import Blueprint, render_template
|
||||
|
||||
# Receipt Generator is entirely client-side (form -> live preview -> PDF,
|
||||
# all in the browser, nothing uploaded) - there's no server-side business
|
||||
# logic, so unlike this repo's other tools there's no logic.py, just the
|
||||
# one page route.
|
||||
receipt_bp = Blueprint("receipt", __name__, url_prefix="/receipt")
|
||||
|
||||
|
||||
@receipt_bp.route("/")
|
||||
def index():
|
||||
return render_template("receipt/index.html")
|
||||
|
|
@ -0,0 +1,946 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Receipt Generator — Undo Designs</title>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600;700&family=Inter:wght@300;400;500;600&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet">
|
||||
<!-- html2canvas + jsPDF loaded separately (current versions), not via
|
||||
html2pdf.js: its bundled html2canvas mis-renders short content on this
|
||||
kind of page (confirmed on the Payslip Generator - same design/PDF
|
||||
pattern - by testing the current standalone html2canvas directly,
|
||||
which renders correctly). See the Download PDF handler below. -->
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"
|
||||
integrity="sha512-BNaRQnYJYiPSqHHDb58B0yaPfCu+Wgds8Gp/gU33kqBtgNS4tSPHuGibyoeqMV/TJlSKda6FXzoEyYGjTe+vXA=="
|
||||
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/4.2.1/jspdf.umd.min.js"
|
||||
integrity="sha512-plOdviVmws4Y3JAvbnpfKb2hVxKM1lCwsi3vmElYRj+tiDLffZ4FVUj5a8vyKJ9pIgl8JCAHEJ4D1iUKBecswg=="
|
||||
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||
<style>
|
||||
:root {
|
||||
--navy: #0C1A3A;
|
||||
--navy-2: #12244C;
|
||||
--navy-3: #1a2f5e;
|
||||
--navy-4: #0a1428;
|
||||
--cyan: #00D4FF;
|
||||
--cyan-dim: rgba(0,212,255,.12);
|
||||
--cyan-glow: rgba(0,212,255,.06);
|
||||
--blue: #1B8EF8;
|
||||
--green: #10D98A;
|
||||
--green-dim: rgba(16,217,138,.12);
|
||||
--red: #F04438;
|
||||
--red-dim: rgba(240,68,56,.12);
|
||||
--w1: rgba(255,255,255,.95);
|
||||
--w2: rgba(255,255,255,.65);
|
||||
--w3: rgba(255,255,255,.35);
|
||||
--w4: rgba(255,255,255,.10);
|
||||
--w5: rgba(255,255,255,.05);
|
||||
--border: rgba(255,255,255,.07);
|
||||
--border-c: rgba(0,212,255,.18);
|
||||
--mono: 'JetBrains Mono', monospace;
|
||||
--disp: 'Space Grotesk', sans-serif;
|
||||
--body: 'Inter', sans-serif;
|
||||
--radius: 10px;
|
||||
}
|
||||
|
||||
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
|
||||
body {
|
||||
font-family: var(--body);
|
||||
background: var(--navy-4);
|
||||
color: var(--w1);
|
||||
min-height: 100vh;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
body::before {
|
||||
content: '';
|
||||
position: fixed; inset: 0; pointer-events: none; z-index: 0;
|
||||
background-image: radial-gradient(circle, rgba(0,212,255,.06) 1px, transparent 1px);
|
||||
background-size: 28px 28px;
|
||||
}
|
||||
|
||||
header {
|
||||
position: sticky; top: 0; z-index: 100;
|
||||
background: rgba(12,26,58,.9);
|
||||
backdrop-filter: blur(12px);
|
||||
border-bottom: 1px solid var(--border);
|
||||
padding: 0 28px;
|
||||
height: 58px;
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
}
|
||||
.logo-mark {
|
||||
display: flex; align-items: center; gap: 10px;
|
||||
font-family: var(--disp); font-size: 15px; font-weight: 600;
|
||||
color: var(--w1); text-decoration: none;
|
||||
}
|
||||
.logo-mark-icon {
|
||||
width: 28px; height: 28px;
|
||||
background: linear-gradient(135deg, var(--navy-3), var(--navy-2));
|
||||
border: 1px solid var(--border-c);
|
||||
border-radius: 7px;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
}
|
||||
.logo-mark-icon svg { width: 15px; height: 15px; }
|
||||
.logo-mark span { color: var(--cyan); }
|
||||
|
||||
.privacy-badge {
|
||||
display: flex; align-items: center; gap: 6px;
|
||||
font-family: var(--mono); font-size: 10.5px; color: var(--green);
|
||||
background: var(--green-dim); border: 1px solid rgba(16,217,138,.25);
|
||||
padding: 5px 12px; border-radius: 20px;
|
||||
}
|
||||
.privacy-badge svg { width: 12px; height: 12px; }
|
||||
|
||||
main {
|
||||
position: relative; z-index: 1;
|
||||
max-width: 1300px; margin: 0 auto;
|
||||
padding: 32px 24px 100px;
|
||||
display: grid;
|
||||
grid-template-columns: 440px 1fr;
|
||||
gap: 24px;
|
||||
align-items: start;
|
||||
}
|
||||
@media (max-width: 980px) {
|
||||
main { grid-template-columns: 1fr; }
|
||||
}
|
||||
|
||||
.page-title { margin-bottom: 20px; grid-column: 1 / -1; }
|
||||
.eyebrow {
|
||||
font-family: var(--mono); font-size: 10px; letter-spacing: .16em;
|
||||
text-transform: uppercase; color: var(--cyan); margin-bottom: 8px;
|
||||
}
|
||||
.page-title h1 {
|
||||
font-family: var(--disp); font-size: 24px; font-weight: 700; margin-bottom: 6px;
|
||||
}
|
||||
.page-title p { font-size: 13px; color: var(--w2); line-height: 1.6; max-width: 560px; }
|
||||
|
||||
/* ── FORM PANEL ── */
|
||||
.panel {
|
||||
background: var(--navy-2);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
padding: 20px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.panel-title {
|
||||
font-family: var(--disp); font-size: 13px; font-weight: 600;
|
||||
display: flex; align-items: center; gap: 8px;
|
||||
margin-bottom: 16px; color: var(--w1);
|
||||
}
|
||||
.panel-num {
|
||||
width: 20px; height: 20px; border-radius: 50%;
|
||||
background: var(--cyan-dim); border: 1px solid var(--border-c);
|
||||
color: var(--cyan); font-family: var(--mono); font-size: 10px;
|
||||
display: flex; align-items: center; justify-content: center; flex-shrink: 0;
|
||||
}
|
||||
|
||||
.field { margin-bottom: 12px; }
|
||||
.field:last-child { margin-bottom: 0; }
|
||||
.field label {
|
||||
display: block; font-family: var(--mono); font-size: 10px;
|
||||
letter-spacing: .06em; text-transform: uppercase; color: var(--w3);
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
.field input[type="text"],
|
||||
.field input[type="date"],
|
||||
.field input[type="time"],
|
||||
.field input[type="number"],
|
||||
.field select,
|
||||
.field textarea {
|
||||
width: 100%; background: var(--navy-4); border: 1px solid var(--border);
|
||||
border-radius: 6px; padding: 9px 11px; color: var(--w1);
|
||||
font-family: var(--body); font-size: 13px; transition: border-color .15s;
|
||||
}
|
||||
.field select { cursor: pointer; }
|
||||
.field textarea { resize: vertical; min-height: 52px; font-family: var(--body); }
|
||||
.field input:focus, .field textarea:focus, .field select:focus {
|
||||
outline: none; border-color: var(--cyan); background: var(--cyan-glow);
|
||||
}
|
||||
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
|
||||
.field-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
|
||||
|
||||
/* ── LOGO UPLOAD ── */
|
||||
.logo-zone {
|
||||
border: 1.5px dashed var(--border); border-radius: 8px;
|
||||
padding: 16px; text-align: center; cursor: pointer;
|
||||
transition: all .2s; position: relative;
|
||||
}
|
||||
.logo-zone:hover, .logo-zone.drag-over { border-color: var(--cyan); background: var(--cyan-glow); }
|
||||
.logo-zone input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
|
||||
.logo-zone-empty { font-size: 12px; color: var(--w2); }
|
||||
.logo-zone-empty strong { color: var(--cyan); }
|
||||
.logo-preview-wrap { display: none; align-items: center; gap: 12px; justify-content: center; }
|
||||
.logo-preview-wrap.visible { display: flex; }
|
||||
.logo-preview-wrap img { max-height: 44px; max-width: 140px; border-radius: 4px; background: #fff; padding: 4px; }
|
||||
.logo-remove {
|
||||
background: var(--red-dim); border: 1px solid rgba(240,68,56,.25); color: var(--red);
|
||||
font-family: var(--mono); font-size: 10px; padding: 4px 10px; border-radius: 6px; cursor: pointer;
|
||||
}
|
||||
|
||||
/* ── ITEM LIST ── */
|
||||
.line-items { margin-bottom: 10px; }
|
||||
.line-item {
|
||||
display: grid; grid-template-columns: 1fr 52px 78px 28px; gap: 6px;
|
||||
margin-bottom: 8px; align-items: center;
|
||||
}
|
||||
.line-item input { width: 100%; padding: 8px 9px; }
|
||||
.line-remove {
|
||||
width: 28px; height: 28px; border-radius: 6px;
|
||||
background: var(--w5); border: 1px solid var(--border); color: var(--w3);
|
||||
cursor: pointer; font-size: 14px; display: flex; align-items: center; justify-content: center;
|
||||
transition: all .15s;
|
||||
}
|
||||
.line-remove:hover { background: var(--red-dim); border-color: rgba(240,68,56,.3); color: var(--red); }
|
||||
.line-item-header {
|
||||
display: grid; grid-template-columns: 1fr 52px 78px 28px; gap: 6px;
|
||||
margin-bottom: 6px; font-family: var(--mono); font-size: 9px; letter-spacing: .06em;
|
||||
text-transform: uppercase; color: var(--w3);
|
||||
}
|
||||
|
||||
.btn-add-line {
|
||||
width: 100%; padding: 8px; border-radius: 6px;
|
||||
background: var(--w5); border: 1px dashed var(--border); color: var(--w2);
|
||||
font-family: var(--mono); font-size: 11px; cursor: pointer; transition: all .15s;
|
||||
}
|
||||
.btn-add-line:hover { border-color: var(--cyan); color: var(--cyan); background: var(--cyan-glow); }
|
||||
|
||||
/* ── TOGGLE PILLS (discount type, tip quick-select) ── */
|
||||
.pill-group { display: flex; gap: 6px; }
|
||||
.pill {
|
||||
flex: 1; text-align: center; padding: 7px 4px; border-radius: 6px;
|
||||
background: var(--w5); border: 1px solid var(--border); color: var(--w3);
|
||||
font-family: var(--mono); font-size: 11px; cursor: pointer; transition: all .15s;
|
||||
}
|
||||
.pill.active { background: var(--cyan-dim); border-color: var(--border-c); color: var(--cyan); }
|
||||
|
||||
/* ── BUTTONS ── */
|
||||
.btn {
|
||||
display: inline-flex; align-items: center; justify-content: center; gap: 7px;
|
||||
padding: 11px 18px; border-radius: 8px; border: none;
|
||||
font-family: var(--disp); font-size: 13px; font-weight: 600;
|
||||
cursor: pointer; transition: all .2s;
|
||||
}
|
||||
.btn-primary { background: linear-gradient(135deg, var(--cyan), var(--blue)); color: var(--navy); }
|
||||
.btn-primary:hover { filter: brightness(1.08); transform: translateY(-1px); }
|
||||
.btn-ghost { background: var(--w5); border: 1px solid var(--border); color: var(--w2); }
|
||||
.btn-ghost:hover { border-color: var(--border-c); color: var(--w1); }
|
||||
.btn-row { display: flex; gap: 10px; margin-bottom: 16px; }
|
||||
.btn-row .btn { flex: 1; }
|
||||
|
||||
/* ── RECEIPT PREVIEW (styled like an actual paper receipt) ── */
|
||||
.preview-wrap { position: sticky; top: 78px; }
|
||||
.preview-toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
|
||||
.preview-label {
|
||||
font-family: var(--mono); font-size: 10px; letter-spacing: .12em;
|
||||
text-transform: uppercase; color: var(--w3);
|
||||
}
|
||||
|
||||
.receipt-stage {
|
||||
display: flex; justify-content: center;
|
||||
padding: 20px 20px 0;
|
||||
}
|
||||
|
||||
#receipt {
|
||||
background: #fdfdfa;
|
||||
color: #1a1a1a;
|
||||
width: 340px;
|
||||
padding: 26px 22px 0;
|
||||
font-family: 'JetBrains Mono', monospace;
|
||||
font-size: 11.5px;
|
||||
line-height: 1.6;
|
||||
box-shadow: 0 20px 50px rgba(0,0,0,.4);
|
||||
position: relative;
|
||||
}
|
||||
.receipt-bottom-edge {
|
||||
width: 340px; height: 14px; margin: 0 auto;
|
||||
background:
|
||||
linear-gradient(-45deg, transparent 7px, #fdfdfa 7px),
|
||||
linear-gradient(45deg, transparent 7px, #fdfdfa 7px);
|
||||
background-size: 14px 14px;
|
||||
background-repeat: repeat-x;
|
||||
background-position: bottom;
|
||||
filter: drop-shadow(0 8px 12px rgba(0,0,0,.25));
|
||||
}
|
||||
|
||||
.rc-logo { text-align: center; margin-bottom: 8px; }
|
||||
.rc-logo img { max-height: 46px; max-width: 160px; object-fit: contain; }
|
||||
.rc-store-name {
|
||||
text-align: center; font-family: 'Space Grotesk', sans-serif;
|
||||
font-size: 16px; font-weight: 700; letter-spacing: .01em; margin-bottom: 4px;
|
||||
}
|
||||
.rc-store-meta { text-align: center; font-size: 10px; color: #555; line-height: 1.5; margin-bottom: 12px; }
|
||||
|
||||
.rc-dashed { border-top: 1.5px dashed #999; margin: 10px 0; }
|
||||
|
||||
.rc-meta-row { display: flex; justify-content: space-between; font-size: 10.5px; margin-bottom: 3px; }
|
||||
.rc-meta-row span:first-child { color: #666; }
|
||||
|
||||
.rc-item-header {
|
||||
display: flex; justify-content: space-between; font-size: 9.5px;
|
||||
letter-spacing: .04em; text-transform: uppercase; color: #666;
|
||||
padding-bottom: 4px; margin-bottom: 4px; border-bottom: 1px solid #ccc;
|
||||
}
|
||||
.rc-item {
|
||||
display: flex; justify-content: space-between; font-size: 11px; margin-bottom: 3px;
|
||||
}
|
||||
.rc-item-name { flex: 1; padding-right: 8px; }
|
||||
.rc-item-qty { width: 50px; color: #666; text-align: center; }
|
||||
.rc-item-amt { width: 60px; text-align: right; }
|
||||
|
||||
.rc-totals-row { display: flex; justify-content: space-between; font-size: 11.5px; padding: 2px 0; }
|
||||
.rc-totals-row.grand {
|
||||
font-size: 14px; font-weight: 700; padding-top: 8px; margin-top: 4px;
|
||||
border-top: 1.5px dashed #333;
|
||||
}
|
||||
.rc-totals-row.change { color: #0a5c3a; font-weight: 600; }
|
||||
|
||||
.rc-payment-block { margin-top: 6px; }
|
||||
|
||||
.rc-footer { text-align: center; margin: 16px 0 10px; font-size: 10.5px; color: #444; }
|
||||
.rc-footer .thanks { font-family: 'Space Grotesk', sans-serif; font-weight: 600; font-size: 13px; margin-bottom: 4px; color: #111; }
|
||||
|
||||
.rc-barcode { display: flex; justify-content: center; gap: 1.5px; margin: 14px 0 4px; height: 34px; align-items: flex-end; }
|
||||
.rc-barcode div { background: #1a1a1a; width: 2px; }
|
||||
.rc-barcode-num { text-align: center; font-size: 9px; letter-spacing: .15em; color: #555; margin-bottom: 14px; }
|
||||
|
||||
footer.app-footer {
|
||||
position: relative; z-index: 1; text-align: center; padding: 24px;
|
||||
font-size: 11px; color: var(--w3); border-top: 1px solid var(--border);
|
||||
margin-top: 20px;
|
||||
}
|
||||
footer.app-footer a { color: var(--cyan); text-decoration: none; }
|
||||
|
||||
/* ── PRINT: isolate only the receipt ── */
|
||||
@media print {
|
||||
body * { visibility: hidden; }
|
||||
#receiptPrintArea, #receiptPrintArea * { visibility: visible; }
|
||||
#receiptPrintArea {
|
||||
position: absolute; left: 0; top: 0; width: 100%;
|
||||
display: flex; justify-content: center;
|
||||
}
|
||||
body::before { display: none; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<header>
|
||||
<a class="logo-mark" href="/">
|
||||
<div class="logo-mark-icon">
|
||||
<svg viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path d="M2 4h12M2 8h8M2 12h5" stroke="#00D4FF" stroke-width="1.5" stroke-linecap="round"/>
|
||||
</svg>
|
||||
</div>
|
||||
Receipt <span>Generator</span>
|
||||
</a>
|
||||
<div class="privacy-badge">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
|
||||
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
|
||||
</svg>
|
||||
Runs entirely in your browser — nothing is uploaded
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<div class="page-title">
|
||||
<div class="eyebrow">Undo Designs</div>
|
||||
<h1>Receipt Generator</h1>
|
||||
<p>Fill in the details on the left — the receipt updates live on the right. Your logo and figures never leave this page: there's no server, no upload, no tracking.</p>
|
||||
</div>
|
||||
|
||||
<!-- ── FORM COLUMN ── -->
|
||||
<div>
|
||||
<div class="panel">
|
||||
<div class="panel-title"><span class="panel-num">1</span>Business</div>
|
||||
<div class="field">
|
||||
<label>Logo (optional)</label>
|
||||
<div class="logo-zone" id="logoZone">
|
||||
<input type="file" id="logoInput" accept="image/*">
|
||||
<div class="logo-zone-empty" id="logoEmpty"><strong>Click to upload</strong> or drag & drop — stays on this page only</div>
|
||||
<div class="logo-preview-wrap" id="logoPreviewWrap">
|
||||
<img id="logoPreviewImg" alt="Logo preview">
|
||||
<button class="logo-remove" id="logoRemoveBtn" type="button">Remove</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Business name</label>
|
||||
<input type="text" id="storeName" value="Undo Designs Coffee Bar">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Address</label>
|
||||
<input type="text" id="storeAddress" value="19 Charlotte Street, Toronto, ON">
|
||||
</div>
|
||||
<div class="field-row">
|
||||
<div class="field">
|
||||
<label>Phone</label>
|
||||
<input type="text" id="storePhone" value="(416) 555-0142">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Currency symbol</label>
|
||||
<input type="text" id="currency" value="$" maxlength="4">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel">
|
||||
<div class="panel-title"><span class="panel-num">2</span>Receipt details</div>
|
||||
<div class="field-row">
|
||||
<div class="field">
|
||||
<label>Receipt #</label>
|
||||
<input type="text" id="receiptNum" value="10432">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Cashier</label>
|
||||
<input type="text" id="cashier" value="Sam">
|
||||
</div>
|
||||
</div>
|
||||
<div class="field-row">
|
||||
<div class="field">
|
||||
<label>Date</label>
|
||||
<input type="date" id="receiptDate">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Time</label>
|
||||
<input type="time" id="receiptTime">
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Customer name (optional)</label>
|
||||
<input type="text" id="customerName" value="">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Payment method</label>
|
||||
<select id="paymentMethod">
|
||||
<option value="Cash">Cash</option>
|
||||
<option value="Credit Card" selected>Credit Card</option>
|
||||
<option value="Debit Card">Debit Card</option>
|
||||
<option value="Mobile Payment">Mobile Payment</option>
|
||||
<option value="Other">Other</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="field-row" id="cardFields" style="display:grid">
|
||||
<div class="field">
|
||||
<label>Card ending in</label>
|
||||
<input type="text" id="cardLast4" value="4417" maxlength="4">
|
||||
</div>
|
||||
<div class="field" id="cashFieldsWrap" style="display:none">
|
||||
<label>Amount tendered</label>
|
||||
<input type="number" step="0.01" id="amountTendered" value="0">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel">
|
||||
<div class="panel-title"><span class="panel-num">3</span>Items</div>
|
||||
<div class="line-item-header">
|
||||
<div>Description</div><div>Qty</div><div>Price</div><div></div>
|
||||
</div>
|
||||
<div class="line-items" id="itemsList"></div>
|
||||
<button class="btn-add-line" id="addItemBtn" type="button">+ Add item</button>
|
||||
</div>
|
||||
|
||||
<div class="panel">
|
||||
<div class="panel-title"><span class="panel-num">4</span>Discount, tax & tip</div>
|
||||
<div class="field">
|
||||
<label>Discount type</label>
|
||||
<div class="pill-group">
|
||||
<div class="pill active" data-discount-type="none">None</div>
|
||||
<div class="pill" data-discount-type="percent">%</div>
|
||||
<div class="pill" data-discount-type="fixed">Fixed</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field" id="discountValueField" style="display:none">
|
||||
<label>Discount value</label>
|
||||
<input type="number" step="0.01" id="discountValue" value="0">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Tax rate (%)</label>
|
||||
<input type="number" step="0.01" id="taxRate" value="0">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Tip</label>
|
||||
<div class="pill-group" style="margin-bottom:8px">
|
||||
<div class="pill" data-tip-pct="15">15%</div>
|
||||
<div class="pill" data-tip-pct="18">18%</div>
|
||||
<div class="pill" data-tip-pct="20">20%</div>
|
||||
</div>
|
||||
<input type="number" step="0.01" id="tipAmount" value="0" placeholder="Or enter a fixed amount">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel">
|
||||
<div class="panel-title"><span class="panel-num">5</span>Footer message</div>
|
||||
<div class="field">
|
||||
<textarea id="footerMsg">Thank you for your visit!
|
||||
All sales final after 14 days.</textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="btn-row">
|
||||
<button class="btn btn-ghost" id="resetBtn" type="button">Reset</button>
|
||||
<button class="btn btn-ghost" id="saveDraftBtn" type="button">Save draft</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- ── PREVIEW COLUMN ── -->
|
||||
<div class="preview-wrap">
|
||||
<div class="preview-toolbar">
|
||||
<div class="preview-label">Live preview</div>
|
||||
<div style="display:flex; gap:8px">
|
||||
<button class="btn btn-ghost" id="printBtn" type="button">
|
||||
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="6,9 6,2 18,2 18,9"/><path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"/><rect x="6" y="14" width="12" height="8"/></svg>
|
||||
Print
|
||||
</button>
|
||||
<button class="btn btn-primary" id="downloadBtn" type="button">
|
||||
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7,10 12,15 17,10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>
|
||||
Download PDF
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="receipt-stage" id="receiptPrintArea">
|
||||
<div id="receiptCard">
|
||||
<div id="receipt">
|
||||
<div class="rc-logo" id="rcLogoWrap" style="display:none">
|
||||
<img id="rcLogo" alt="">
|
||||
</div>
|
||||
<div class="rc-store-name" id="rcStoreName">Undo Designs Coffee Bar</div>
|
||||
<div class="rc-store-meta" id="rcStoreMeta">19 Charlotte Street, Toronto, ON<br>(416) 555-0142</div>
|
||||
|
||||
<div class="rc-dashed"></div>
|
||||
|
||||
<div class="rc-meta-row"><span>Receipt #</span><span id="rcReceiptNum">10432</span></div>
|
||||
<div class="rc-meta-row"><span>Date</span><span id="rcDate">—</span></div>
|
||||
<div class="rc-meta-row"><span>Cashier</span><span id="rcCashier">Sam</span></div>
|
||||
<div class="rc-meta-row" id="rcCustomerRow" style="display:none"><span>Customer</span><span id="rcCustomer">—</span></div>
|
||||
|
||||
<div class="rc-dashed"></div>
|
||||
|
||||
<div class="rc-item-header">
|
||||
<span style="flex:1">Item</span><span style="width:50px;text-align:center">Qty</span><span style="width:60px;text-align:right">Amt</span>
|
||||
</div>
|
||||
<div id="rcItems"></div>
|
||||
|
||||
<div class="rc-dashed"></div>
|
||||
|
||||
<div id="rcTotals"></div>
|
||||
|
||||
<div class="rc-dashed"></div>
|
||||
|
||||
<div class="rc-payment-block">
|
||||
<div class="rc-meta-row"><span>Payment</span><span id="rcPaymentMethod">Credit Card</span></div>
|
||||
<div class="rc-meta-row" id="rcCardRow"><span>Card</span><span id="rcCardInfo">•••• 4417</span></div>
|
||||
<div class="rc-meta-row" id="rcChangeRow" style="display:none"><span>Change due</span><span id="rcChange">0.00</span></div>
|
||||
</div>
|
||||
|
||||
<div class="rc-footer">
|
||||
<div class="thanks" id="rcThanks">Thank you for your visit!</div>
|
||||
<div id="rcFooterRest">All sales final after 14 days.</div>
|
||||
</div>
|
||||
|
||||
<div class="rc-barcode" id="rcBarcode"></div>
|
||||
<div class="rc-barcode-num" id="rcBarcodeNum">10432</div>
|
||||
</div>
|
||||
<div class="receipt-bottom-edge"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer class="app-footer">
|
||||
Built by <a href="https://undodesigns.com" target="_blank">Undo Designs</a> · Receipt Generator v1 · 100% client-side
|
||||
</footer>
|
||||
|
||||
<script>
|
||||
const $ = id => document.getElementById(id);
|
||||
|
||||
// ── State ────────────────────────────────────────────────────────────────────
|
||||
let items = [
|
||||
{ desc: 'Flat White', qty: 2, price: 4.75 },
|
||||
{ desc: 'Almond Croissant', qty: 1, price: 3.95 },
|
||||
];
|
||||
let logoDataUrl = null;
|
||||
let discountType = 'none'; // none | percent | fixed
|
||||
|
||||
// ── Formatting ──────────────────────────────────────────────────────────────
|
||||
function fmt(n) {
|
||||
const cur = $('currency').value.trim() || '$';
|
||||
return cur + (n < 0 ? '-' : '') + Math.abs(n || 0).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
||||
}
|
||||
function escapeHtml(s) {
|
||||
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
||||
}
|
||||
|
||||
// ── Item rows ─────────────────────────────────────────────────────────────────
|
||||
function renderItemRows() {
|
||||
const listEl = $('itemsList');
|
||||
listEl.innerHTML = '';
|
||||
items.forEach((item, idx) => {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'line-item';
|
||||
row.innerHTML = `
|
||||
<input type="text" value="${escapeHtml(item.desc)}" data-idx="${idx}" data-field="desc">
|
||||
<input type="number" step="1" min="1" value="${item.qty}" data-idx="${idx}" data-field="qty">
|
||||
<input type="number" step="0.01" value="${item.price}" data-idx="${idx}" data-field="price">
|
||||
<button class="line-remove" data-idx="${idx}" type="button">✕</button>
|
||||
`;
|
||||
listEl.appendChild(row);
|
||||
});
|
||||
listEl.querySelectorAll('input').forEach(inp => {
|
||||
inp.addEventListener('input', () => {
|
||||
const idx = +inp.dataset.idx;
|
||||
const field = inp.dataset.field;
|
||||
items[idx][field] = field === 'desc' ? inp.value : (parseFloat(inp.value) || 0);
|
||||
renderReceipt();
|
||||
});
|
||||
});
|
||||
listEl.querySelectorAll('.line-remove').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
items.splice(+btn.dataset.idx, 1);
|
||||
renderItemRows();
|
||||
renderReceipt();
|
||||
});
|
||||
});
|
||||
}
|
||||
$('addItemBtn').addEventListener('click', () => {
|
||||
items.push({ desc: '', qty: 1, price: 0 });
|
||||
renderItemRows();
|
||||
renderReceipt();
|
||||
});
|
||||
|
||||
// ── Logo upload (client-side only) ──────────────────────────────────────────
|
||||
const logoInput = $('logoInput');
|
||||
const logoZone = $('logoZone');
|
||||
function handleLogoFile(file) {
|
||||
if (!file || !file.type.startsWith('image/')) return;
|
||||
const reader = new FileReader();
|
||||
reader.onload = e => {
|
||||
logoDataUrl = e.target.result;
|
||||
$('logoPreviewImg').src = logoDataUrl;
|
||||
$('logoPreviewWrap').classList.add('visible');
|
||||
$('logoEmpty').style.display = 'none';
|
||||
renderReceipt();
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
logoInput.addEventListener('change', () => handleLogoFile(logoInput.files[0]));
|
||||
logoZone.addEventListener('dragover', e => { e.preventDefault(); logoZone.classList.add('drag-over'); });
|
||||
logoZone.addEventListener('dragleave', () => logoZone.classList.remove('drag-over'));
|
||||
logoZone.addEventListener('drop', e => {
|
||||
e.preventDefault(); logoZone.classList.remove('drag-over');
|
||||
if (e.dataTransfer.files[0]) handleLogoFile(e.dataTransfer.files[0]);
|
||||
});
|
||||
$('logoRemoveBtn').addEventListener('click', (e) => {
|
||||
e.stopPropagation();
|
||||
logoDataUrl = null;
|
||||
logoInput.value = '';
|
||||
$('logoPreviewWrap').classList.remove('visible');
|
||||
$('logoEmpty').style.display = 'block';
|
||||
renderReceipt();
|
||||
});
|
||||
|
||||
// ── Discount type pills ────────────────────────────────────────────────────────
|
||||
document.querySelectorAll('[data-discount-type]').forEach(pill => {
|
||||
pill.addEventListener('click', () => {
|
||||
document.querySelectorAll('[data-discount-type]').forEach(p => p.classList.remove('active'));
|
||||
pill.classList.add('active');
|
||||
discountType = pill.dataset.discountType;
|
||||
$('discountValueField').style.display = discountType === 'none' ? 'none' : 'block';
|
||||
renderReceipt();
|
||||
});
|
||||
});
|
||||
$('discountValue').addEventListener('input', renderReceipt);
|
||||
|
||||
// ── Tip quick-select pills ─────────────────────────────────────────────────────
|
||||
document.querySelectorAll('[data-tip-pct]').forEach(pill => {
|
||||
pill.addEventListener('click', () => {
|
||||
document.querySelectorAll('[data-tip-pct]').forEach(p => p.classList.remove('active'));
|
||||
pill.classList.add('active');
|
||||
const pct = parseFloat(pill.dataset.tipPct);
|
||||
const subtotal = items.reduce((s, i) => s + i.qty * i.price, 0);
|
||||
$('tipAmount').value = (subtotal * pct / 100).toFixed(2);
|
||||
renderReceipt();
|
||||
});
|
||||
});
|
||||
$('tipAmount').addEventListener('input', () => {
|
||||
document.querySelectorAll('[data-tip-pct]').forEach(p => p.classList.remove('active'));
|
||||
renderReceipt();
|
||||
});
|
||||
|
||||
// ── Payment method toggling ─────────────────────────────────────────────────────
|
||||
$('paymentMethod').addEventListener('change', () => {
|
||||
const method = $('paymentMethod').value;
|
||||
$('cashFieldsWrap').style.display = method === 'Cash' ? 'block' : 'none';
|
||||
const cardField = $('cardLast4').closest('.field');
|
||||
cardField.style.display = (method === 'Credit Card' || method === 'Debit Card') ? 'block' : 'none';
|
||||
renderReceipt();
|
||||
});
|
||||
$('amountTendered').addEventListener('input', renderReceipt);
|
||||
$('cardLast4').addEventListener('input', renderReceipt);
|
||||
|
||||
// ── Barcode (decorative, generated from receipt number) ────────────────────────
|
||||
function renderBarcode(seed) {
|
||||
const el = $('rcBarcode');
|
||||
el.innerHTML = '';
|
||||
const str = String(seed || '10432');
|
||||
let hash = 0;
|
||||
for (let i = 0; i < str.length; i++) hash = (hash * 31 + str.charCodeAt(i)) >>> 0;
|
||||
for (let i = 0; i < 40; i++) {
|
||||
hash = (hash * 1103515245 + 12345) >>> 0;
|
||||
const width = (hash % 3) + 1;
|
||||
const height = 18 + (hash % 16);
|
||||
const bar = document.createElement('div');
|
||||
bar.style.width = width + 'px';
|
||||
bar.style.height = height + 'px';
|
||||
el.appendChild(bar);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Live receipt rendering ──────────────────────────────────────────────────
|
||||
function renderReceipt() {
|
||||
// Header
|
||||
$('rcStoreName').textContent = $('storeName').value || 'Business Name';
|
||||
const addr = $('storeAddress').value;
|
||||
const phone = $('storePhone').value;
|
||||
$('rcStoreMeta').innerHTML = [addr, phone].filter(Boolean).map(escapeHtml).join('<br>');
|
||||
|
||||
if (logoDataUrl) {
|
||||
$('rcLogo').src = logoDataUrl;
|
||||
$('rcLogoWrap').style.display = 'block';
|
||||
} else {
|
||||
$('rcLogoWrap').style.display = 'none';
|
||||
}
|
||||
|
||||
// Meta
|
||||
$('rcReceiptNum').textContent = $('receiptNum').value || '—';
|
||||
$('rcCashier').textContent = $('cashier').value || '—';
|
||||
|
||||
const dateVal = $('receiptDate').value;
|
||||
const timeVal = $('receiptTime').value;
|
||||
const dateStr = dateVal
|
||||
? new Date(dateVal + 'T00:00:00').toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' })
|
||||
: '—';
|
||||
$('rcDate').textContent = timeVal ? `${dateStr} ${timeVal}` : dateStr;
|
||||
|
||||
const customer = $('customerName').value.trim();
|
||||
if (customer) {
|
||||
$('rcCustomer').textContent = customer;
|
||||
$('rcCustomerRow').style.display = 'flex';
|
||||
} else {
|
||||
$('rcCustomerRow').style.display = 'none';
|
||||
}
|
||||
|
||||
// Items
|
||||
const itemsEl = $('rcItems');
|
||||
itemsEl.innerHTML = items.map(i => {
|
||||
const lineTotal = (i.qty || 0) * (i.price || 0);
|
||||
return `<div class="rc-item">
|
||||
<span class="rc-item-name">${escapeHtml(i.desc || 'Item')}</span>
|
||||
<span class="rc-item-qty">${i.qty} × ${(i.price||0).toFixed(2)}</span>
|
||||
<span class="rc-item-amt">${fmt(lineTotal)}</span>
|
||||
</div>`;
|
||||
}).join('');
|
||||
|
||||
const subtotal = items.reduce((s, i) => s + (i.qty || 0) * (i.price || 0), 0);
|
||||
|
||||
// Discount
|
||||
let discountAmt = 0;
|
||||
const discountVal = parseFloat($('discountValue').value) || 0;
|
||||
if (discountType === 'percent') discountAmt = subtotal * discountVal / 100;
|
||||
else if (discountType === 'fixed') discountAmt = discountVal;
|
||||
discountAmt = Math.min(discountAmt, subtotal);
|
||||
|
||||
const taxableAmount = subtotal - discountAmt;
|
||||
const taxRate = parseFloat($('taxRate').value) || 0;
|
||||
const taxAmt = taxableAmount * taxRate / 100;
|
||||
const tip = parseFloat($('tipAmount').value) || 0;
|
||||
|
||||
const total = taxableAmount + taxAmt + tip;
|
||||
|
||||
// Totals block
|
||||
let totalsHtml = `<div class="rc-totals-row"><span>Subtotal</span><span>${fmt(subtotal)}</span></div>`;
|
||||
if (discountType !== 'none' && discountAmt > 0) {
|
||||
const label = discountType === 'percent' ? `Discount (${discountVal}%)` : 'Discount';
|
||||
totalsHtml += `<div class="rc-totals-row"><span>${label}</span><span>-${fmt(discountAmt)}</span></div>`;
|
||||
}
|
||||
if (taxRate > 0) {
|
||||
totalsHtml += `<div class="rc-totals-row"><span>Tax (${taxRate}%)</span><span>${fmt(taxAmt)}</span></div>`;
|
||||
}
|
||||
if (tip > 0) {
|
||||
totalsHtml += `<div class="rc-totals-row"><span>Tip</span><span>${fmt(tip)}</span></div>`;
|
||||
}
|
||||
totalsHtml += `<div class="rc-totals-row grand"><span>Total</span><span>${fmt(total)}</span></div>`;
|
||||
$('rcTotals').innerHTML = totalsHtml;
|
||||
|
||||
// Payment
|
||||
const method = $('paymentMethod').value;
|
||||
$('rcPaymentMethod').textContent = method;
|
||||
|
||||
const isCard = method === 'Credit Card' || method === 'Debit Card';
|
||||
$('rcCardRow').style.display = isCard ? 'flex' : 'none';
|
||||
if (isCard) $('rcCardInfo').textContent = '•••• ' + ($('cardLast4').value || '0000');
|
||||
|
||||
const isCash = method === 'Cash';
|
||||
if (isCash) {
|
||||
const tendered = parseFloat($('amountTendered').value) || 0;
|
||||
const change = tendered - total;
|
||||
$('rcChangeRow').style.display = 'flex';
|
||||
$('rcChange').textContent = fmt(Math.max(change, 0));
|
||||
} else {
|
||||
$('rcChangeRow').style.display = 'none';
|
||||
}
|
||||
|
||||
// Footer
|
||||
const footerLines = ($('footerMsg').value || '').split('\n').filter(Boolean);
|
||||
$('rcThanks').textContent = footerLines[0] || 'Thank you!';
|
||||
$('rcFooterRest').innerHTML = footerLines.slice(1).map(escapeHtml).join('<br>');
|
||||
|
||||
// Barcode
|
||||
renderBarcode($('receiptNum').value);
|
||||
$('rcBarcodeNum').textContent = $('receiptNum').value || '';
|
||||
}
|
||||
|
||||
// ── Wire up all top-level fields to live-update ────────────────────────────────
|
||||
['storeName','storeAddress','storePhone','currency','receiptNum','cashier',
|
||||
'receiptDate','receiptTime','customerName','taxRate','footerMsg'].forEach(id => {
|
||||
$(id).addEventListener('input', renderReceipt);
|
||||
});
|
||||
|
||||
// ── Reset ─────────────────────────────────────────────────────────────────────
|
||||
$('resetBtn').addEventListener('click', () => {
|
||||
if (!confirm('Clear all fields and start over?')) return;
|
||||
['storeName','storeAddress','storePhone','receiptNum','cashier','customerName'].forEach(id => $(id).value = '');
|
||||
$('currency').value = '$';
|
||||
$('receiptDate').value = '';
|
||||
$('receiptTime').value = '';
|
||||
$('taxRate').value = 0;
|
||||
$('tipAmount').value = 0;
|
||||
$('discountValue').value = 0;
|
||||
$('footerMsg').value = 'Thank you for your visit!';
|
||||
discountType = 'none';
|
||||
document.querySelectorAll('[data-discount-type]').forEach(p => p.classList.remove('active'));
|
||||
document.querySelector('[data-discount-type="none"]').classList.add('active');
|
||||
$('discountValueField').style.display = 'none';
|
||||
document.querySelectorAll('[data-tip-pct]').forEach(p => p.classList.remove('active'));
|
||||
items = [{ desc: '', qty: 1, price: 0 }];
|
||||
logoDataUrl = null;
|
||||
$('logoPreviewWrap').classList.remove('visible');
|
||||
$('logoEmpty').style.display = 'block';
|
||||
logoInput.value = '';
|
||||
renderItemRows();
|
||||
renderReceipt();
|
||||
});
|
||||
|
||||
// ── Save / load draft (localStorage) ────────────────────────────────────────────
|
||||
$('saveDraftBtn').addEventListener('click', () => {
|
||||
const draft = {
|
||||
storeName: $('storeName').value, storeAddress: $('storeAddress').value, storePhone: $('storePhone').value,
|
||||
currency: $('currency').value, receiptNum: $('receiptNum').value, cashier: $('cashier').value,
|
||||
receiptDate: $('receiptDate').value, receiptTime: $('receiptTime').value, customerName: $('customerName').value,
|
||||
paymentMethod: $('paymentMethod').value, cardLast4: $('cardLast4').value, amountTendered: $('amountTendered').value,
|
||||
discountType, discountValue: $('discountValue').value, taxRate: $('taxRate').value, tipAmount: $('tipAmount').value,
|
||||
footerMsg: $('footerMsg').value, items, logoDataUrl,
|
||||
};
|
||||
localStorage.setItem('receipt_draft', JSON.stringify(draft));
|
||||
const btn = $('saveDraftBtn');
|
||||
const original = btn.textContent;
|
||||
btn.textContent = 'Saved ✓';
|
||||
setTimeout(() => btn.textContent = original, 1500);
|
||||
});
|
||||
|
||||
function loadDraft() {
|
||||
const raw = localStorage.getItem('receipt_draft');
|
||||
if (!raw) return;
|
||||
try {
|
||||
const d = JSON.parse(raw);
|
||||
const setIf = (id, val) => { if (val !== undefined && val !== null) $(id).value = val; };
|
||||
setIf('storeName', d.storeName); setIf('storeAddress', d.storeAddress); setIf('storePhone', d.storePhone);
|
||||
setIf('currency', d.currency); setIf('receiptNum', d.receiptNum); setIf('cashier', d.cashier);
|
||||
setIf('receiptDate', d.receiptDate); setIf('receiptTime', d.receiptTime); setIf('customerName', d.customerName);
|
||||
setIf('paymentMethod', d.paymentMethod); setIf('cardLast4', d.cardLast4); setIf('amountTendered', d.amountTendered);
|
||||
setIf('discountValue', d.discountValue); setIf('taxRate', d.taxRate); setIf('tipAmount', d.tipAmount);
|
||||
setIf('footerMsg', d.footerMsg);
|
||||
if (d.discountType) {
|
||||
discountType = d.discountType;
|
||||
document.querySelectorAll('[data-discount-type]').forEach(p => {
|
||||
p.classList.toggle('active', p.dataset.discountType === discountType);
|
||||
});
|
||||
$('discountValueField').style.display = discountType === 'none' ? 'none' : 'block';
|
||||
}
|
||||
if (Array.isArray(d.items) && d.items.length) items = d.items;
|
||||
if (d.logoDataUrl) {
|
||||
logoDataUrl = d.logoDataUrl;
|
||||
$('logoPreviewImg').src = logoDataUrl;
|
||||
$('logoPreviewWrap').classList.add('visible');
|
||||
$('logoEmpty').style.display = 'none';
|
||||
}
|
||||
$('paymentMethod').dispatchEvent(new Event('change'));
|
||||
} catch (e) { /* ignore corrupt draft */ }
|
||||
}
|
||||
|
||||
// ── Print ─────────────────────────────────────────────────────────────────────
|
||||
$('printBtn').addEventListener('click', () => window.print());
|
||||
|
||||
// ── Download PDF (client-side, html2canvas + jsPDF directly) ──────────────────
|
||||
// Not html2pdf.js: its bundled html2canvas mis-renders shorter content on
|
||||
// this kind of page (confirmed on the Payslip Generator, same pattern) -
|
||||
// so html2canvas + jsPDF (current standalone versions) are driven directly.
|
||||
$('downloadBtn').addEventListener('click', async () => {
|
||||
const el = $('receipt');
|
||||
const num = ($('receiptNum').value || 'receipt').replace(/[^a-z0-9]+/gi, '_');
|
||||
const filename = `Receipt_${num}.pdf`;
|
||||
|
||||
if (!window.html2canvas || !window.jspdf) {
|
||||
alert('PDF library did not load (likely no internet connection). Use Print instead and choose "Save as PDF" in the print dialog.');
|
||||
window.print();
|
||||
return;
|
||||
}
|
||||
|
||||
const w = el.offsetWidth, h = el.offsetHeight;
|
||||
const canvas = await html2canvas(el, {
|
||||
scale: 2.5, useCORS: true, backgroundColor: '#fdfdfa',
|
||||
// The on-screen card has a drop shadow for the floating-receipt look;
|
||||
// that's screen chrome (only reads correctly against the dark page
|
||||
// background it's designed for), not part of the document, so strip
|
||||
// it for the PDF rather than bake in a shadow smudge on a plain page.
|
||||
onclone: (doc) => {
|
||||
const clone = doc.getElementById('receipt');
|
||||
if (clone) clone.style.boxShadow = 'none';
|
||||
},
|
||||
});
|
||||
const imgData = canvas.toDataURL('image/jpeg', 0.98);
|
||||
|
||||
// unit:'px' + format sized to the element's own CSS-pixel dimensions
|
||||
// makes the PDF page fit the content on one page, matching what's on
|
||||
// screen. Two things this needs to stay a single, uncropped page:
|
||||
// - hotfixes:['px_scaling'] - required for jsPDF to interpret px
|
||||
// dimensions at the correct 96dpi (CSS) scale instead of its legacy
|
||||
// 72dpi assumption, which otherwise mis-sizes the page.
|
||||
// - height is never less than width (Math.max), always with
|
||||
// orientation:'portrait', so jsPDF never has to reconcile a
|
||||
// landscape-shaped array against a portrait declaration by swapping
|
||||
// width/height on its own (which mismatches the placed image). A
|
||||
// receipt with very few items just gets a bit of blank space at the
|
||||
// bottom instead.
|
||||
// - a small +8px height buffer: a page sized to EXACTLY match content
|
||||
// height can round to just under the image height and force a
|
||||
// trailing near-empty second page; +8px gives headroom against that
|
||||
// without a visible gap.
|
||||
const { jsPDF } = window.jspdf;
|
||||
const pdf = new jsPDF({
|
||||
unit: 'px', format: [w, Math.max(h, w) + 8],
|
||||
orientation: 'portrait', hotfixes: ['px_scaling'],
|
||||
});
|
||||
pdf.addImage(imgData, 'JPEG', 0, 0, w, h);
|
||||
pdf.save(filename);
|
||||
});
|
||||
|
||||
// ── Init ──────────────────────────────────────────────────────────────────────
|
||||
renderItemRows();
|
||||
loadDraft();
|
||||
renderReceipt();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue