Fetch fresh calibration on every print #19

Merged
rhino merged 1 commits from cursor/fresh-print-calibration into main 2026-10-05 11:04:29 +00:00
4 changed files with 76 additions and 34 deletions

View File

@@ -53,32 +53,26 @@ Because the application prints dynamic text directly over your physical, pre-pri
Follow these steps to calibrate the alignment for your specific printer: Follow these steps to calibrate the alignment for your specific printer:
### 1. Print the Calibration Sheet ### 1. Open the Calibrate tab
1. Open the application and navigate to the **Calibrate** tab in the top menu. 1. Open the application and navigate to **Calibrate** in the top menu.
2. Load a **blank** piece of A4 paper into your printer. 2. You will see a live A4 preview (2×2 slips) and a control panel with millimeter positions.
3. Click the "Print Calibration Sheet" button to print it out.
### 2. Check the Alignment ### 2. Print and check the alignment
1. Take your printed blank calibration sheet. 1. Load a **blank** piece of A4 paper into your printer.
2. Place it perfectly behind one of your **actual pre-printed label sheets**. 2. Click **Print** on the calibrate page.
3. Hold them both up to a light source (like a window or lamp). 3. Place the printed sheet perfectly behind one of your **actual pre-printed label sheets**.
4. Look for the crosshairs (`┼`). They should align perfectly inside the target boxes (Batch, Date, Next Control, and Weight). 4. Hold them both up to a light source (window or lamp).
5. The crosshairs (`┼`) should sit inside the target boxes (Batch, Date, Next Control, and Weight).
### 3. Adjusting the Millimeters (If Necessary) ### 3. Adjust positions in the UI
If the text is printing slightly too high, too low, or too far to a side, you can adjust the CSS margins in the codebase to shift the text. If text is too high, low, or off to the side, nudge it in the panel — no code edits or Docker restart needed.
1. Open the file: `slips/templates/slips/print_page.html` 1. Use **+/−** (or type a value) for each field’s `top` / `left` (and font if needed).
2. Scroll to the `<style>` block at the top, and find the `.var-field` CSS section. - Move **DOWN**: increase `top`.
3. You will see absolute positioning values in millimeters (`mm`), for example: - Move **RIGHT**: increase `left`.
```css 2. Click a crosshair on the preview to jump to that field’s controls; arrow keys also nudge the selection.
.val-batch { top: 15mm; left: 40mm; } 3. Click **Save** when it looks right. Values are stored in `data/calibration.json` (same persistent data volume as the database).
``` 4. Print a new calibration sheet and overlay again until perfect.
4. **Adjust the numbers**: 5. **Reset defaults** restores the built-in starting positions.
- To move text **DOWN**: Increase the `top` value (e.g., `15mm` -> `17mm`).
- To move text **RIGHT**: Increase the `left` value (e.g., `40mm` -> `42mm`). Order print pages load the latest saved calibration automatically when you open Print (no hard refresh required).
5. Save the file.
6. Restart the Docker container so the changes take effect:
```bash
docker compose restart app
```
7. Print a new calibration sheet and test again until perfect!

View File

@@ -1,6 +1,6 @@
function printOrder(id) { function printOrder(id) {
// Open the print page in a new window/tab // Cache-bust so a second print of the same order is not a stale HTML document
const win = window.open(`/print-page/${id}/`, '_blank'); const win = window.open(`/print-page/${id}/?t=${Date.now()}`, '_blank');
if (win) { if (win) {
win.focus(); win.focus();
} else { } else {

View File

@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Print Order #{{ order.id }}</title> <title>Print Order #{{ order.id }}</title>
<link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}"> <link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}">
<style> <style id="calibration-vars">
{{ calibration_css|safe }} {{ calibration_css|safe }}
</style> </style>
<style> <style>
@@ -48,10 +48,15 @@
font-weight: bold; font-weight: bold;
} }
.preview-controls button:hover { .preview-controls button:hover:not(:disabled) {
background: #2563eb; background: #2563eb;
} }
.preview-controls button:disabled {
opacity: 0.55;
cursor: wait;
}
.page-wrapper { .page-wrapper {
margin-top: 80px; /* Space for controls */ margin-top: 80px; /* Space for controls */
display: flex; display: flex;
@@ -126,8 +131,8 @@
{{ order.canister_count }} Canisters ({{ order.canister_pages }} Pages) | {{ order.canister_count }} Canisters ({{ order.canister_pages }} Pages) |
{{ order.box_count }} Boxes ({{ order.box_pages }} Pages) {{ order.box_count }} Boxes ({{ order.box_pages }} Pages)
</p> </p>
<button onclick="window.print()">🖨️ Print Now</button> <button id="btn-print" type="button" disabled>🖨️ Print Now</button>
<button onclick="window.close()" style="background:#64748b; margin-left: 10px;">Close</button> <button type="button" onclick="window.close()" style="background:#64748b; margin-left: 10px;">Close</button>
</div> </div>
<div class="page-wrapper"> <div class="page-wrapper">
@@ -148,8 +153,48 @@
</div> </div>
<script> <script>
// Optional: auto-trigger print dialog when opened (function () {
// window.onload = function() { window.print(); } const FIELDS = ['batch', 'date', 'next', 'weight'];
const varsEl = document.getElementById('calibration-vars');
const btn = document.getElementById('btn-print');
function cssFromCal(cal) {
const lines = [':root {'];
for (const field of FIELDS) {
const v = cal[field];
lines.push(' --' + field + '-top: ' + v.top + 'mm;');
lines.push(' --' + field + '-left: ' + v.left + 'mm;');
lines.push(' --' + field + '-font: ' + v.font + 'pt;');
lines.push(' --' + field + '-bottom-top: ' + v.bottom_top + 'mm;');
if (v.bottom_left != null) {
lines.push(' --' + field + '-bottom-left: ' + v.bottom_left + 'mm;');
}
if (v.right_left != null) {
lines.push(' --' + field + '-right-left: ' + v.right_left + 'mm;');
}
}
lines.push('}');
return lines.join('\n');
}
function enablePrint() {
btn.disabled = false;
btn.onclick = function () { window.print(); };
}
fetch('/api/calibration', { cache: 'no-store' })
.then(function (res) {
if (!res.ok) throw new Error('HTTP ' + res.status);
return res.json();
})
.then(function (cal) {
varsEl.textContent = cssFromCal(cal);
})
.catch(function () {
// Keep server-rendered vars; still allow print
})
.finally(enablePrint);
})();
</script> </script>
</body> </body>
</html> </html>

View File

@@ -1,4 +1,5 @@
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.cache import never_cache
from .calibration import css_vars, load_calibration from .calibration import css_vars, load_calibration
from .models import Product, Order from .models import Product, Order
from .print_layout import build_print_pages from .print_layout import build_print_pages
@@ -26,6 +27,7 @@ def print_queue(request):
}) })
@never_cache
def print_page(request, order_id): def print_page(request, order_id):
order = get_object_or_404(Order.objects.select_related('product'), id=order_id) order = get_object_or_404(Order.objects.select_related('product'), id=order_id)
pages = build_print_pages(order) pages = build_print_pages(order)
@@ -40,6 +42,7 @@ def print_page(request, order_id):
}) })
@never_cache
def calibrate(request): def calibrate(request):
cal = load_calibration() cal = load_calibration()
return render(request, 'slips/calibrate.html', { return render(request, 'slips/calibrate.html', {