<template>
<div>
<div id="invoice" class="invoice">
<header class="invoice-header">
<div class="company-info">
<h1>Company Name</h1>
<p>123 Business St</p>
<p>City, State 12345</p>
</div>
<div class="invoice-meta">
<h2>INVOICE</h2>
<p><strong>Invoice #:</strong> {{ invoiceNumber }}</p>
<p><strong>Date:</strong> {{ invoiceDate }}</p>
<p><strong>Due:</strong> {{ dueDate }}</p>
</div>
</header>
<section class="bill-to">
<h3>Bill To:</h3>
<p>{{ customer.name }}</p>
<p>{{ customer.address }}</p>
</section>
<table class="items">
<thead>
<tr>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr v-for="item in items" :key="item.id">
<td>{{ item.description }}</td>
<td>{{ item.quantity }}</td>
<td>${{ item.price }}</td>
<td>${{ item.quantity * item.price }}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3" class="text-right"><strong>Subtotal:</strong></td>
<td>${{ subtotal }}</td>
</tr>
<tr>
<td colspan="3" class="text-right"><strong>Tax (8%):</strong></td>
<td>${{ tax }}</td>
</tr>
<tr class="total-row">
<td colspan="3" class="text-right"><strong>Total:</strong></td>
<td><strong>${{ total }}</strong></td>
</tr>
</tfoot>
</table>
<footer class="invoice-footer">
<p>Thank you for your business!</p>
</footer>
</div>
<button @click="handlePrint" class="no-print">Print Invoice</button>
</div>
</template>
<script setup>
import { computed } from 'vue'
import { usePrint } from 'vue-print-it'
const { print } = usePrint()
const invoiceNumber = 'INV-2024-001'
const invoiceDate = '2024-03-04'
const dueDate = '2024-03-18'
const customer = {
name: 'John Doe',
address: '456 Client Ave, City, State 12345'
}
const items = [
{ id: 1, description: 'Web Development', quantity: 40, price: 75 },
{ id: 2, description: 'Design Services', quantity: 10, price: 100 }
]
const subtotal = computed(() =>
items.reduce((sum, item) => sum + (item.quantity * item.price), 0)
)
const tax = computed(() => subtotal.value * 0.08)
const total = computed(() => subtotal.value + tax.value)
function handlePrint() {
print('invoice', {
windowTitle: `Invoice ${invoiceNumber}`,
styles: [
'/* Page setup */',
'@page {',
' size: A4;',
' margin: 0.75in;',
'}',
'',
'/* Base styles */',
'body {',
' font-family: Arial, sans-serif;',
' font-size: 11pt;',
' color: #000;',
'}',
'',
'/* Invoice layout */',
'.invoice {',
' max-width: 100%;',
' margin: 0;',
' padding: 0;',
'}',
'',
'/* Header */',
'.invoice-header {',
' display: flex;',
' justify-content: space-between;',
' margin-bottom: 30px;',
' padding-bottom: 20px;',
' border-bottom: 2px solid #000;',
'}',
'',
'.company-info h1 {',
' margin: 0 0 10px 0;',
' font-size: 20pt;',
'}',
'',
'.invoice-meta {',
' text-align: right;',
'}',
'',
'.invoice-meta h2 {',
' margin: 0 0 10px 0;',
' font-size: 24pt;',
'}',
'',
'/* Bill to section */',
'.bill-to {',
' margin-bottom: 30px;',
'}',
'',
'.bill-to h3 {',
' margin: 0 0 10px 0;',
' font-size: 12pt;',
'}',
'',
'/* Table */',
'.items {',
' width: 100%;',
' border-collapse: collapse;',
' margin-bottom: 30px;',
'}',
'',
'.items th,',
'.items td {',
' padding: 10px;',
' border: 1px solid #ddd;',
' text-align: left;',
'}',
'',
'.items th {',
' background-color: #f0f0f0;',
' font-weight: bold;',
' -webkit-print-color-adjust: exact;',
' print-color-adjust: exact;',
'}',
'',
'.items .text-right {',
' text-align: right;',
'}',
'',
'.total-row {',
' background-color: #f5f5f5;',
' -webkit-print-color-adjust: exact;',
' print-color-adjust: exact;',
'}',
'',
'/* Footer */',
'.invoice-footer {',
' margin-top: 50px;',
' text-align: center;',
' font-style: italic;',
' color: #666;',
'}',
'',
'/* Hide print button */',
'.no-print {',
' display: none !important;',
'}'
]
})
}
</script>