Skip to content

fix(printer): allow about:blank popups and auto-fallback to browser print when no hardware printer is configured - #418

Merged
khaira777 merged 5 commits into
mainfrom
fix/printer-auto-fallback-and-popup
Aug 20, 2026
Merged

khaira777 merged 5 commits into
mainfrom
fix/printer-auto-fallback-and-popup

Conversation

@khaira777

Copy link
Copy Markdown
Contributor

Intent

fix(printer): allow about:blank popups and auto-fallback to browser print when no hardware printer is configured

What Changed

  • Permitted about:blank and empty window URLs in the URL allowlist and Electron window opener handler with tailored popup dimensions (800x600) and menu bar settings for browser print popups.
  • Added automatic fallback to browser/system printing with an informational toast in usePrinter when ESC/POS printing is requested without a configured or connected hardware printer.
  • Supported fallback 80mm formatting for bill preview generation when no hardware printers exist, updated backend printer resolution to fallback to the first available printer, and handled unconfigured printer states gracefully without unhandled exceptions.

Risk Assessment

✅ Low: The changes cleanly allow about:blank popups in Electron for browser printing and provide safe fallback to browser print dialogs when no thermal hardware printer is configured.

Testing

Exercised URL allowlist popup permissions ('about:blank' and empty URL), Electron window open handler for print popups, zero-printer backend receipt preview fallback and hardware failure fast-rejection, frontend store auto-fallback to browser printing with toast notifications when no thermal printer is configured, and captured full visual screenshots and rendered HTML artifacts across 80mm/58mm formats.

  • Evidence: Print Receipt popup window simulation with fallback toast banner (local file: /Users/gurkiratkhaira/.no-mistakes/evidence/01M0EKM3F11WJKFGM6FVP4QF3B/popup_window_simulation.png)
Evidence: Print Receipt popup window HTML
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Print Receipt - Electron Window Simulation</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      background: #1e1e2e;
      color: #cdd6f4;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      display: flex;
      flex-direction: column;
      height: 100vh;
    }
    .window-header {
      background: #181825;
      padding: 10px 16px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #313244;
    }
    .window-title {
      font-weight: 600;
      font-size: 13px;
      display: flex;
      align-items: center;
      gap: 8px;
    }
    .badge {
      background: #89b4fa;
      color: #11111b;
      padding: 2px 8px;
      border-radius: 12px;
      font-size: 11px;
      font-weight: bold;
    }
    .toast-banner {
      background: #313244;
      border-left: 4px solid #89b4fa;
      margin: 12px 16px;
      padding: 10px 14px;
      border-radius: 6px;
      display: flex;
      align-items: center;
      gap: 10px;
      font-size: 13px;
      color: #cdd6f4;
      box-shadow: 0 4px 6px rgba(0,0,0,0.3);
    }
    .content {
      flex: 1;
      padding: 16px;
      display: flex;
      justify-content: center;
      overflow: auto;
    }
    .receipt-frame {
      background: #ffffff;
      color: #11111b;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 8px 24px rgba(0,0,0,0.5);
      max-width: 80mm;
      width: 100%;
    }
  </style>
</head>
<body>
  <div class="window-header">
    <div class="window-title">
      <span>Print Receipt</span>
      <span class="badge">about:blank Popup (800×600)</span>
    </div>
    <div style="font-size: 12px; color: #a6adc8;">autoHideMenuBar: true</div>
  </div>

  <div class="toast-banner">
    <span style="font-size: 18px;">ℹ️</span>
    <div>
      <strong>Automatic Fallback Active:</strong>
      <span>No thermal printer configured — printing via system print</span>
    </div>
  </div>

  <div class="content">
    <div class="receipt-frame">
      
  <div class="bill-container">
    
    <!-- Header -->
    <div class="header">
      <h1>The Roasted Bean Cafe</h1>
      <p>742 Evergreen Terrace, Springfield</p>
      <p>Ph: <span class="ltr" dir="ltr">+1 (555) 867-5309</span></p>
      <p>EIN: <span class="ltr" dir="ltr">TAX-US-987654</span></p>
    </div>

    <!-- Bill Details -->
    <div class="bill-details">
      <table>
        <tr>
          <td><strong>Bill #</strong> <span class="ltr" dir="ltr">INV-2026-0801</span></td>
          <td class="text-end"><strong>Date</strong> Aug 19, 2026, 04:00 PM</td>
        </tr>
        <tr><td><strong>Table</strong> Table 4 (Patio)</td><td></td></tr>
        <tr><td><strong>Customer</strong> Sarah Jenkins</td><td></td></tr>
        <tr><td><strong>Customer No</strong> <span class="ltr" dir="ltr">+1 (555) 234-5678</span></td><td></td></tr>
      </table>
    </div>

    <!-- Items Table -->
    <table class="items-table">
      <thead>
        <tr>
          <th>Item</th>
          <th class="text-end">Qty</th>
          <th class="text-end">Rate</th>
          <th class="text-end">Amount</th>
        </tr>
      </thead>
      <tbody>
        
          <tr>
            <td>
              Artisan Flat White
              <br><small class="text-muted">+ Oat Milk ×2</small>
              <br><small class="text-italic">Extra hot</small>
            </td>
            <td class="text-end num">2</td>
            <td class="text-end num">$6.00</td>
            <td class="text-end num">$12.96</td>
          </tr>
        
          <tr>
            <td>
              Avocado Sourdough Toast
              <br><small class="text-muted">+ Poached Egg ×2</small>
              
            </td>
            <td class="text-end num">2</td>
            <td class="text-end num">$15.00</td>
            <td class="text-end num">$32.40</td>
          </tr>
        
      </tbody>
    </table>

    <!-- Tax Breakdown -->
    
    <table class="tax-table">
      <thead>
        <tr><th colspan="2">Tax Details</th></tr>
      </thead>
      <tbody>
        
          <tr><td>State Tax (8%) @8%</td><td class="text-end num">$3.36</td></tr>
        
      </tbody>
    </table>
    

    <!-- Totals -->
    <table class="totals-table">
      <tr><td>Subtotal</td><td class="text-end num">$42.00</td></tr>
      <tr><td>Discount</td><td class="text-end num">-$5.00</td></tr>
      <tr><td>Total Tax</td><td class="text-end num">$3.36</td></tr>
      <tr><td>Service Charge</td><td class="text-end num">$2.00</td></tr>
      
      <tr class="total-row"><td><strong>Grand Total</strong></td><td class="text-end num"><strong>$42.36</strong></td></tr>
    </table>

    <!-- Payments -->
    
    <table class="payments-table">
      <thead>
        <tr><th colspan="2">Payments</th></tr>
      </thead>
      <tbody>
        
          <tr><td>Card</td><td class="text-end num">$42.36</td></tr>
        
      </tbody>
    </table>
    

    <!-- Footer -->
    <div class="footer">
      <p>Thank you for dining with us! Free Wi-Fi: RoastedBeanGuest</p>
      <p>Tax included where applicable</p>
      <p class="powered-by">Powered by FloPOS<br>https://flopos.com</p>
    </div>
  </div>

  <div class="no-print" style="text-align:center;margin-top:20px;">
    <button onclick="window.print()" style="padding:10px 20px;font-size:16px;cursor:pointer;">Print Bill</button>
  </div>

  
    </div>
  </div>
</body>
</html>
- Evidence: Fallback 80mm thermal receipt preview rendered image (local file: /Users/gurkiratkhaira/.no-mistakes/evidence/01M0EKM3F11WJKFGM6FVP4QF3B/fallback_receipt_80mm_preview.png)
Evidence: Fallback 80mm thermal receipt HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <title>Bill # INV-2026-0801</title>
  <style>
    
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: -apple-system, 'Segoe UI', Tahoma, 'Noto Naskh Arabic', 'Helvetica Neue', Arial, sans-serif; font-size: 12px; line-height: 1.4; color: #333; }
    .bill-container { max-width: 100%; margin: 0 auto; }
    .reprint-banner { text-align: center; font-size: 22px; font-weight: bold; letter-spacing: 2px; color: #c00; border: 3px solid #c00; padding: 6px; margin-bottom: 15px; }
    .header { text-align: center; margin-bottom: 20px; padding-bottom: 10px; border-bottom: 1px solid #ccc; }
    .header h1 { font-size: 24px; margin-bottom: 5px; }
    .bill-details { margin-bottom: 15px; }
    .bill-details table { width: 100%; }
    .items-table { width: 100%; border-collapse: collapse; margin-bottom: 15px; }
    .items-table th, .items-table td { padding: 8px; border-bottom: 1px solid #eee; text-align: start; }
    .items-table th { background: #f5f5f5; font-weight: bold; }
    .tax-table, .payments-table { width: 50%; margin-inline-start: 50%; border-collapse: collapse; margin-bottom: 15px; }
    .tax-table th, .tax-table td, .payments-table th, .payments-table td { padding: 6px 8px; }
    .tax-table th, .payments-table th { background: #f9f9f9; text-align: start; }
    .totals-table { width: 100%; border-collapse: collapse; margin-bottom: 15px; }
    .totals-table td { padding: 6px 8px; }
    .total-row { border-top: 2px solid #333; font-size: 16px; }
    .footer { text-align: center; margin-top: 30px; padding-top: 15px; border-top: 1px solid #ccc; }
    .powered-by { font-size: 10px; margin-top: 8px; color: #555; }
    .text-end { text-align: end !important; }
    .num { unicode-bidi: isolate; white-space: nowrap; }
    .ltr { direction: ltr; unicode-bidi: isolate; }
    .text-muted { color: #666; }
    .text-italic { font-style: italic; color: #888; }
  
        .bill-container { padding: 10px; max-width: 80mm; font-size: 11px; }
        .header h1 { font-size: 16px; }
      
    @media print {
      .no-print { display: none !important; }
      body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    }
  </style>
</head>
<body>
  <div class="bill-container">
    
    <!-- Header -->
    <div class="header">
      <h1>The Roasted Bean Cafe</h1>
      <p>742 Evergreen Terrace, Springfield</p>
      <p>Ph: <span class="ltr" dir="ltr">+1 (555) 867-5309</span></p>
      <p>EIN: <span class="ltr" dir="ltr">TAX-US-987654</span></p>
    </div>

    <!-- Bill Details -->
    <div class="bill-details">
      <table>
        <tr>
          <td><strong>Bill #</strong> <span class="ltr" dir="ltr">INV-2026-0801</span></td>
          <td class="text-end"><strong>Date</strong> Aug 19, 2026, 04:00 PM</td>
        </tr>
        <tr><td><strong>Table</strong> Table 4 (Patio)</td><td></td></tr>
        <tr><td><strong>Customer</strong> Sarah Jenkins</td><td></td></tr>
        <tr><td><strong>Customer No</strong> <span class="ltr" dir="ltr">+1 (555) 234-5678</span></td><td></td></tr>
      </table>
    </div>

    <!-- Items Table -->
    <table class="items-table">
      <thead>
        <tr>
          <th>Item</th>
          <th class="text-end">Qty</th>
          <th class="text-end">Rate</th>
          <th class="text-end">Amount</th>
        </tr>
      </thead>
      <tbody>
        
          <tr>
            <td>
              Artisan Flat White
              <br><small class="text-muted">+ Oat Milk ×2</small>
              <br><small class="text-italic">Extra hot</small>
            </td>
            <td class="text-end num">2</td>
            <td class="text-end num">$6.00</td>
            <td class="text-end num">$12.96</td>
          </tr>
        
          <tr>
            <td>
              Avocado Sourdough Toast
              <br><small class="text-muted">+ Poached Egg ×2</small>
              
            </td>
            <td class="text-end num">2</td>
            <td class="text-end num">$15.00</td>
            <td class="text-end num">$32.40</td>
          </tr>
        
      </tbody>
    </table>

    <!-- Tax Breakdown -->
    
    <table class="tax-table">
      <thead>
        <tr><th colspan="2">Tax Details</th></tr>
      </thead>
      <tbody>
        
          <tr><td>State Tax (8%) @8%</td><td class="text-end num">$3.36</td></tr>
        
      </tbody>
    </table>
    

    <!-- Totals -->
    <table class="totals-table">
      <tr><td>Subtotal</td><td class="text-end num">$42.00</td></tr>
      <tr><td>Discount</td><td class="text-end num">-$5.00</td></tr>
      <tr><td>Total Tax</td><td class="text-end num">$3.36</td></tr>
      <tr><td>Service Charge</td><td class="text-end num">$2.00</td></tr>
      
      <tr class="total-row"><td><strong>Grand Total</strong></td><td class="text-end num"><strong>$42.36</strong></td></tr>
    </table>

    <!-- Payments -->
    
    <table class="payments-table">
      <thead>
        <tr><th colspan="2">Payments</th></tr>
      </thead>
      <tbody>
        
          <tr><td>Card</td><td class="text-end num">$42.36</td></tr>
        
      </tbody>
    </table>
    

    <!-- Footer -->
    <div class="footer">
      <p>Thank you for dining with us! Free Wi-Fi: RoastedBeanGuest</p>
      <p>Tax included where applicable</p>
      <p class="powered-by">Powered by FloPOS<br>https://flopos.com</p>
    </div>
  </div>

  <div class="no-print" style="text-align:center;margin-top:20px;">
    <button onclick="window.print()" style="padding:10px 20px;font-size:16px;cursor:pointer;">Print Bill</button>
  </div>
</body>
</html>
  
- Evidence: Fallback 58mm thermal receipt preview rendered image (local file: /Users/gurkiratkhaira/.no-mistakes/evidence/01M0EKM3F11WJKFGM6FVP4QF3B/fallback_receipt_58mm_preview.png)
Evidence: Fallback 58mm thermal receipt HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <title>Bill # INV-2026-0801</title>
  <style>
    
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: -apple-system, 'Segoe UI', Tahoma, 'Noto Naskh Arabic', 'Helvetica Neue', Arial, sans-serif; font-size: 12px; line-height: 1.4; color: #333; }
    .bill-container { max-width: 100%; margin: 0 auto; }
    .reprint-banner { text-align: center; font-size: 22px; font-weight: bold; letter-spacing: 2px; color: #c00; border: 3px solid #c00; padding: 6px; margin-bottom: 15px; }
    .header { text-align: center; margin-bottom: 20px; padding-bottom: 10px; border-bottom: 1px solid #ccc; }
    .header h1 { font-size: 24px; margin-bottom: 5px; }
    .bill-details { margin-bottom: 15px; }
    .bill-details table { width: 100%; }
    .items-table { width: 100%; border-collapse: collapse; margin-bottom: 15px; }
    .items-table th, .items-table td { padding: 8px; border-bottom: 1px solid #eee; text-align: start; }
    .items-table th { background: #f5f5f5; font-weight: bold; }
    .tax-table, .payments-table { width: 50%; margin-inline-start: 50%; border-collapse: collapse; margin-bottom: 15px; }
    .tax-table th, .tax-table td, .payments-table th, .payments-table td { padding: 6px 8px; }
    .tax-table th, .payments-table th { background: #f9f9f9; text-align: start; }
    .totals-table { width: 100%; border-collapse: collapse; margin-bottom: 15px; }
    .totals-table td { padding: 6px 8px; }
    .total-row { border-top: 2px solid #333; font-size: 16px; }
    .footer { text-align: center; margin-top: 30px; padding-top: 15px; border-top: 1px solid #ccc; }
    .powered-by { font-size: 10px; margin-top: 8px; color: #555; }
    .text-end { text-align: end !important; }
    .num { unicode-bidi: isolate; white-space: nowrap; }
    .ltr { direction: ltr; unicode-bidi: isolate; }
    .text-muted { color: #666; }
    .text-italic { font-style: italic; color: #888; }
  
        .bill-container { padding: 5px; max-width: 58mm; font-size: 10px; }
        .header h1 { font-size: 14px; }
        .items-table th, .items-table td, .tax-table td, .totals-table td, .payments-table td { padding: 2px 4px; }
      
    @media print {
      .no-print { display: none !important; }
      body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    }
  </style>
</head>
<body>
  <div class="bill-container">
    
    <!-- Header -->
    <div class="header">
      <h1>The Roasted Bean Cafe</h1>
      <p>742 Evergreen Terrace</p>
      <p>Ph: <span class="ltr" dir="ltr">+1 (555) 867-5309</span></p>
      <p>EIN: <span class="ltr" dir="ltr">TAX-US-987654</span></p>
    </div>

    <!-- Bill Details -->
    <div class="bill-details">
      <table>
        <tr>
          <td><strong>Bill #</strong> <span class="ltr" dir="ltr">INV-2026-0801</span></td>
          <td class="text-end"><strong>Date</strong> Aug 19, 2026, 04:00 PM</td>
        </tr>
        <tr><td><strong>Table</strong> Table 4 (Patio)</td><td></td></tr>
        <tr><td><strong>Customer</strong> Sarah Jenkins</td><td></td></tr>
        <tr><td><strong>Customer No</strong> <span class="ltr" dir="ltr">+1 (555) 234-5678</span></td><td></td></tr>
      </table>
    </div>

    <!-- Items Table -->
    <table class="items-table">
      <thead>
        <tr>
          <th>Item</th>
          <th class="text-end">Qty</th>
          <th class="text-end">Rate</th>
          <th class="text-end">Amount</th>
        </tr>
      </thead>
      <tbody>
        
          <tr>
            <td>
              Artisan Flat White
              <br><small class="text-muted">+ Oat Milk ×2</small>
              <br><small class="text-italic">Extra hot</small>
            </td>
            <td class="text-end num">2</td>
            <td class="text-end num">$6.00</td>
            <td class="text-end num">$12.96</td>
          </tr>
        
          <tr>
            <td>
              Avocado Sourdough Toast
              <br><small class="text-muted">+ Poached Egg ×2</small>
              
            </td>
            <td class="text-end num">2</td>
            <td class="text-end num">$15.00</td>
            <td class="text-end num">$32.40</td>
          </tr>
        
      </tbody>
    </table>

    <!-- Tax Breakdown -->
    
    <table class="tax-table">
      <thead>
        <tr><th colspan="2">Tax Details</th></tr>
      </thead>
      <tbody>
        
          <tr><td>State Tax (8%) @8%</td><td class="text-end num">$3.36</td></tr>
        
      </tbody>
    </table>
    

    <!-- Totals -->
    <table class="totals-table">
      <tr><td>Subtotal</td><td class="text-end num">$42.00</td></tr>
      <tr><td>Discount</td><td class="text-end num">-$5.00</td></tr>
      <tr><td>Total Tax</td><td class="text-end num">$3.36</td></tr>
      <tr><td>Service Charge</td><td class="text-end num">$2.00</td></tr>
      
      <tr class="total-row"><td><strong>Grand Total</strong></td><td class="text-end num"><strong>$42.36</strong></td></tr>
    </table>

    <!-- Payments -->
    
    <table class="payments-table">
      <thead>
        <tr><th colspan="2">Payments</th></tr>
      </thead>
      <tbody>
        
          <tr><td>Card</td><td class="text-end num">$42.36</td></tr>
        
      </tbody>
    </table>
    

    <!-- Footer -->
    <div class="footer">
      <p>Thank you for your visit!</p>
      <p>Tax included where applicable</p>
      <p class="powered-by">Powered by FloPOS<br>https://flopos.com</p>
    </div>
  </div>

  <div class="no-print" style="text-align:center;margin-top:20px;">
    <button onclick="window.print()" style="padding:10px 20px;font-size:16px;cursor:pointer;">Print Bill</button>
  </div>
</body>
</html>
  

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 2 issues found → auto-fixed ✅
  • ⚠️ frontend/src/hooks/usePrinter.ts:156 - The fallback check on line 156 matches all cases where 'hw' is falsy under 'escpos' mode, making lines 163-196 unreachable dead code and causing active WebUSB printer connections to be bypassed in favor of browser print dialogs.
  • ℹ️ main/printers/thermal.ts:796 - prepareReceipt injects a dummy network printer (127.0.0.1:9100) when no printer is configured; direct callers of printReceipt without an explicit printer check will attempt a TCP connection to localhost instead of failing fast like printKOT.

🔧 Fix: Fix WebUSB fallback check and unconfigured printer dispatch
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • npm run test:url-allowlist
  • npm run test:printer-api
  • npm run test:bills-print-api
  • npm run test:printer
  • NODE_PATH=./frontend/node_modules npx tsx tests/browser-receipts.test.ts
  • NODE_PATH=./frontend/node_modules npx tsx tests/printer-fallback-and-popup-verification.ts
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@greptile-apps

greptile-apps Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR enables secure blank print popups and falls back to browser/system printing when no usable hardware printer is configured.

  • Allows about:blank and empty popup URLs with print-oriented Electron window dimensions.
  • Aligns frontend and backend selection of usable non-WebUSB printers.
  • Supports 80 mm receipt previews without a configured hardware printer.
  • Adds printer fallback, API, and URL-allowlist verification coverage.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
frontend/src/hooks/usePrinter.ts Adds browser-print fallback while preserving connected WebUSB and configured backend printer paths.
main/index.ts Allows blank print popups with constrained Electron window settings while retaining URL checks.
main/printers/thermal.ts Resolves usable non-WebUSB printers consistently and separates preview fallback formatting from physical dispatch.
main/routes/printers.ts Aligns bill-printer resolution with frontend ordering and permits hardware-free 80 mm previews.
main/security/url-allowlist.ts Extends the local-window allowlist to blank URLs used by browser printing.
tests/printer-fallback-and-popup-verification.ts Exercises popup handling, printer fallback, fast rejection, and receipt formatting behavior.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Bill print requested] --> B{Print method}
  B -->|Browser| E[Open browser print popup]
  B -->|ESC/POS| C{Usable non-WebUSB printer configured?}
  C -->|Yes| D[Dispatch through backend printer]
  C -->|No| F[Show fallback notification]
  F --> E
  G[Preview requested] --> H{Usable printer configured?}
  H -->|Yes| I[Format using selected printer profile]
  H -->|No| J[Format with default 80 mm profile]
Loading

Reviews (3): Last reviewed commit: "Merge branch 'main' into fix/printer-aut..." | Re-trigger Greptile

Comment thread main/routes/printers.ts Resolved
@khaira777
khaira777 enabled auto-merge (squash) August 20, 2026 06:17
@khaira777
khaira777 merged commit 310be5d into main Aug 20, 2026
13 checks passed
@khaira777
khaira777 deleted the fix/printer-auto-fallback-and-popup branch August 20, 2026 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant