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:
### 1. Print the Calibration Sheet
1. Open the application and navigate to the **Calibrate** tab in the top menu.
2. Load a **blank** piece of A4 paper into your printer.
3. Click the "Print Calibration Sheet" button to print it out.
### 1. Open the Calibrate tab
1. Open the application and navigate to **Calibrate** in the top menu.
2. You will see a live A4 preview (2×2 slips) and a control panel with millimeter positions.
### 2. Check the Alignment
1. Take your printed blank calibration sheet.
2. Place it perfectly behind one of your **actual pre-printed label sheets**.
3. Hold them both up to a light source (like a window or lamp).
4. Look for the crosshairs (`┼`). They should align perfectly inside the target boxes (Batch, Date, Next Control, and Weight).
### 2. Print and check the alignment
1. Load a **blank** piece of A4 paper into your printer.
2. Click **Print** on the calibrate page.
3. Place the printed sheet perfectly behind one of your **actual pre-printed label sheets**.
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)
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.
### 3. Adjust positions in the UI
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`
2. Scroll to the `<style>` block at the top, and find the `.var-field` CSS section.
3. You will see absolute positioning values in millimeters (`mm`), for example:
```css
.val-batch { top: 15mm; left: 40mm; }
```
4. **Adjust the numbers**:
- To move text **DOWN**: Increase the `top` value (e.g., `15mm` -> `17mm`).
- To move text **RIGHT**: Increase the `left` value (e.g., `40mm` -> `42mm`).
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!
1. Use **+/−** (or type a value) for each field’s `top` / `left` (and font if needed).
- Move **DOWN**: increase `top`.
- Move **RIGHT**: increase `left`.
2. Click a crosshair on the preview to jump to that field’s controls; arrow keys also nudge the selection.
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.
5. **Reset defaults** restores the built-in starting positions.
Order print pages load the latest saved calibration automatically when you open Print (no hard refresh required).

View File

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

View File

@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Print Order #{{ order.id }}</title>
<link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}">
<style>
<style id="calibration-vars">
{{ calibration_css|safe }}
</style>
<style>
@@ -48,10 +48,15 @@
font-weight: bold;
}
.preview-controls button:hover {
.preview-controls button:hover:not(:disabled) {
background: #2563eb;
}
.preview-controls button:disabled {
opacity: 0.55;
cursor: wait;
}
.page-wrapper {
margin-top: 80px; /* Space for controls */
display: flex;
@@ -126,8 +131,8 @@
{{ order.canister_count }} Canisters ({{ order.canister_pages }} Pages) |
{{ order.box_count }} Boxes ({{ order.box_pages }} Pages)
</p>
<button onclick="window.print()">🖨️ Print Now</button>
<button onclick="window.close()" style="background:#64748b; margin-left: 10px;">Close</button>
<button id="btn-print" type="button" disabled>🖨️ Print Now</button>
<button type="button" onclick="window.close()" style="background:#64748b; margin-left: 10px;">Close</button>
</div>
<div class="page-wrapper">
@@ -148,8 +153,48 @@
</div>
<script>
// Optional: auto-trigger print dialog when opened
// window.onload = function() { window.print(); }
(function () {
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>
</body>
</html>

View File

@@ -1,4 +1,5 @@
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 .models import Product, Order
from .print_layout import build_print_pages
@@ -26,6 +27,7 @@ def print_queue(request):
})
@never_cache
def print_page(request, order_id):
order = get_object_or_404(Order.objects.select_related('product'), id=order_id)
pages = build_print_pages(order)
@@ -40,6 +42,7 @@ def print_page(request, order_id):
})
@never_cache
def calibrate(request):
cal = load_calibration()
return render(request, 'slips/calibrate.html', {