New plugin: print part labels on a Brother QL printer

Print label in the top-right actions of issues, estimates and invoices
prints the customer's last name and the PO number (W/E/I + number) in big
bold letters on a 62mm continuous roll, with a preview of the label and
its status until it's printed. A print agent on the printer's network
(redmine_qbo_label_agent) asks Redmine for labels every couple of seconds
with its printer's token, so labels print from anywhere.

Administration » Label printers: printers, agent tokens and status,
recent labels. Settings: label lengths, capitals, poll interval, how long
a label waits for an offline agent, and logging the agents' requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-16 08:42:16 -05:00
commit 3e29f14f76
41 files changed
+2194

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
qbo-label-bold.ttf is DejaVu Sans Bold (https://dejavu-fonts.github.io/), cut down to Latin
letters (Basic Latin, Latin-1, Latin Extended-A) without kerning or hinting, so the print
dialog's preview and the print agent's labels come out the same.
Copyright: Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved.
Bitstream Vera is a trademark of Bitstream, Inc.
DejaVu changes are in public domain.
License: bitstream-vera
Permission is hereby granted, free of charge, to any person obtaining a copy
of the fonts accompanying this license ("Fonts") and associated
documentation files (the "Font Software"), to reproduce and distribute the
Font Software, including without limitation the rights to use, copy, merge,
publish, distribute, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to the
following conditions:
The above copyright and trademark notices and this permission notice shall
be included in all copies of one or more of the Font Software typefaces.
The Font Software may be modified, altered, or added to, and in particular
the designs of glyphs or characters in the Fonts may be modified and
additional glyphs or characters may be added to the Fonts, only if the fonts
are renamed to names not containing either the words "Bitstream" or the word
"Vera".
This License becomes null and void to the extent applicable to Fonts or Font
Software that has been modified and is distributed under the "Bitstream
Vera" names.
The Font Software may be sold as part of a larger software package but no
copy of one or more of the Font Software typefaces may be sold by itself.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT,
TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME
FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING
ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE
FONT SOFTWARE.
Except as contained in this notice, the names of Gnome, the Gnome
Foundation, and Bitstream Inc., shall not be used in advertising or
otherwise to promote the sale, use or other dealings in this Font Software
without prior written authorization from the Gnome Foundation or Bitstream
Inc., respectively. For further information, contact: fonts at gnome dot
org.
Binary file not shown.
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<!-- Label printing icons (Tabler outline icons, MIT), in the layout of Redmine's own sprite -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon--sprite">
<defs>
<symbol viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" id="icon--qbo-label-print">
<path d="M17 17h2a2 2 0 0 0 2 -2v-4a2 2 0 0 0 -2 -2h-14a2 2 0 0 0 -2 2v4a2 2 0 0 0 2 2h2"/>
<path d="M17 9v-4a2 2 0 0 0 -2 -2h-6a2 2 0 0 0 -2 2v4"/>
<path d="M7 15a2 2 0 0 1 2 -2h6a2 2 0 0 1 2 2v4a2 2 0 0 1 -2 2h-6a2 2 0 0 1 -2 -2z"/>
</symbol>
</defs>
</svg>

After

Width:  |  Height:  |  Size: 581 B

+238
View File
@@ -0,0 +1,238 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2026 rick barrette
*
* Print label dialog (redmine_qbo_labels): draws a preview of the label the way the print agent draws it,
* then follows the print job until it's printed.
*
* Label layout (keep in step with the agent's label.py): the label runs along a 62mm continuous roll at
* 300 dpi, so it's 696 dots tall and as long as its text needs, between the minimum and maximum length.
* The lines are stacked, left aligned, each in the largest bold letters that fit.
*/
var QboLabels = (function($) {
'use strict';
var LAYOUT = {
dpi: 300,
height: 696, // printable dots across a 62mm roll
marginX: 36, // ~3mm at each end
marginY: 24, // ~2mm top and bottom
gapRatio: 0.12, // space between lines, as a share of the (smallest) font size
minSize: 8,
maxSize: 700,
sameSizeWithin: 1.25, // lines whose biggest sizes are this close are drawn the same size
font: 'QboLabel'
};
var pollTimer = null;
var following = null;
function mmToDots(mm) {
return Math.round(mm / 25.4 * LAYOUT.dpi);
}
function fontAt(size) {
return '700 ' + size + 'px ' + LAYOUT.font + ', "DejaVu Sans", Arial, sans-serif';
}
// A line's ink at a font size: { left, right, ascent, descent, width, height }
function box(ctx, text, size) {
ctx.font = fontAt(size);
var m = ctx.measureText(text);
var left = m.actualBoundingBoxLeft || 0, right = m.actualBoundingBoxRight || m.width;
var ascent = m.actualBoundingBoxAscent || size * 0.73, descent = m.actualBoundingBoxDescent || 0;
return { left: left, right: right, ascent: ascent, descent: descent,
width: left + right, height: ascent + descent };
}
// The largest font size from minSize to maxSize that passes the test (minSize if none does)
function largest(test) {
var lo = LAYOUT.minSize, hi = LAYOUT.maxSize;
while (lo < hi) {
var mid = Math.floor((lo + hi + 1) / 2);
if (test(mid)) { lo = mid; } else { hi = mid - 1; }
}
return lo;
}
function gapFor(sizes) {
return Math.round(Math.min.apply(null, sizes) * LAYOUT.gapRatio);
}
function totalHeight(ctx, lines, sizes) {
return lines.reduce(function(sum, text, i) { return sum + box(ctx, text, sizes[i]).height; }, 0) +
gapFor(sizes) * (lines.length - 1);
}
// Each line's font size and box, and the label's length in dots. Every line is as big as it can be: no
// wider than the longest label allows, and together no taller than the roll. Lines that would come out
// about the same size are made the same size; when one is held back by its length (a long name), the
// others still grow to fill the height.
function layout(ctx, lines, minLengthMm, maxLengthMm) {
var maxLength = mmToDots(maxLengthMm), minLength = mmToDots(minLengthMm);
var maxWidth = maxLength - 2 * LAYOUT.marginX, maxHeight = LAYOUT.height - 2 * LAYOUT.marginY;
var limits = lines.map(function(text) {
return largest(function(size) {
var b = box(ctx, text, size);
return b.width <= maxWidth && b.height <= maxHeight;
});
});
function sizesAt(cap) { return limits.map(function(limit) { return Math.min(cap, limit); }); }
var cap = largest(function(c) { return totalHeight(ctx, lines, sizesAt(c)) <= maxHeight; });
var sizes = sizesAt(cap);
var smallest = Math.min.apply(null, sizes);
if (Math.max.apply(null, sizes) < smallest * LAYOUT.sameSizeWithin) {
sizes = sizes.map(function() { return smallest; });
}
var boxes = lines.map(function(text, i) { return box(ctx, text, sizes[i]); });
var width = Math.max.apply(null, boxes.map(function(b) { return b.width; }));
var length = Math.min(maxLength, Math.max(minLength, Math.ceil(width + 2 * LAYOUT.marginX)));
return { sizes: sizes, boxes: boxes, length: length };
}
function draw(canvas, lines, opts) {
var ctx = canvas.getContext('2d');
if (!lines.length) {
canvas.width = mmToDots(opts.minLengthMm); canvas.height = LAYOUT.height;
ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, canvas.width, canvas.height);
return null;
}
var result = layout(ctx, lines, opts.minLengthMm, opts.maxLengthMm);
canvas.width = result.length;
canvas.height = LAYOUT.height;
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#000';
ctx.textBaseline = 'alphabetic';
var gap = gapFor(result.sizes);
var total = result.boxes.reduce(function(sum, b) { return sum + b.height; }, 0) + gap * (lines.length - 1);
var y = Math.round((LAYOUT.height - total) / 2);
lines.forEach(function(text, i) {
var b = result.boxes[i];
ctx.font = fontAt(result.sizes[i]);
ctx.fillText(text, LAYOUT.marginX + b.left, y + b.ascent);
y += b.height + gap;
});
return result;
}
function dialog() { return $('#qbo-label-dialog'); }
function currentLines($dialog) {
var upper = $dialog.data('uppercase') === true || $dialog.data('uppercase') === 'true';
return $dialog.find('.qbo-label-line').map(function() {
var text = $.trim($(this).val()).replace(/\s+/g, ' ');
return upper ? text.toUpperCase() : text;
}).get().filter(function(text) { return text.length > 0; });
}
function redraw() {
var $dialog = dialog();
var canvas = $dialog.find('canvas.qbo-label-preview')[0];
if (!canvas) { return; }
var lines = currentLines($dialog);
var result = draw(canvas, lines, {
minLengthMm: parseFloat($dialog.data('min-length-mm')),
maxLengthMm: parseFloat($dialog.data('max-length-mm'))
});
var inches = (canvas.width / LAYOUT.dpi).toFixed(1);
$dialog.find('.qbo-label-size').text(result ? '2.4 × ' + inches + ' in' : '');
$dialog.find('.qbo-label-submit').prop('disabled', !result);
}
function initDialog() {
var $dialog = dialog();
stopFollowing();
$dialog.on('input change', '.qbo-label-line', redraw);
redraw();
// Redraw once the label font has loaded (the first draw may use a fallback font)
if (document.fonts && document.fonts.load) {
document.fonts.load(fontAt(100), 'A').then(function() { redraw(); }, function() {});
}
$dialog.find('.qbo-label-line').first().trigger('focus');
// Stop following the job when the dialog is closed
$('#ajax-modal').off('dialogclose.qboLabels').on('dialogclose.qboLabels', stopFollowing);
}
function setStatus(text, state) {
var $status = dialog().find('.qbo-label-status');
$status.prop('hidden', false).attr('data-state', state || 'working');
$status.find('.qbo-label-status-text').text(text);
$status.find('.qbo-label-again').toggle(state === 'printed' || state === 'failed');
}
function stopFollowing() {
if (pollTimer) { clearTimeout(pollTimer); }
pollTimer = null;
following = null;
}
// Follows a print job until it's printed, failed or expired
function follow(job) {
var $dialog = dialog();
stopFollowing();
following = { url: job.url, errors: 0 };
// .hide(): Redmine's styles show forms whatever their hidden attribute says
$dialog.find('#qbo-label-form').hide();
setStatus(job.printer_online ? $dialog.data('text-printing') : $dialog.data('text-offline'));
poll(following);
}
function poll(state) {
if (following !== state) { return; }
$.ajax({ url: state.url, dataType: 'json', cache: false }).done(function(data) {
if (following !== state) { return; }
state.errors = 0;
if (data.finished) {
var text = data.status_label + (data.error ? ': ' + data.error : '');
setStatus(text, data.status === 'printed' ? 'printed' : 'failed');
stopFollowing();
return;
}
var $dialog = dialog();
if (data.status === 'printing') {
setStatus($dialog.data('text-printing'));
} else {
setStatus(data.printer_online ? $dialog.data('text-waiting') : $dialog.data('text-offline'));
}
pollTimer = setTimeout(function() { poll(state); }, 1000);
}).fail(function() {
if (following !== state) { return; }
state.errors += 1;
if (state.errors >= 5) {
setStatus(dialog().data('text-lost'), 'failed');
stopFollowing();
} else {
pollTimer = setTimeout(function() { poll(state); }, 2000);
}
});
}
// Back to the form, to print the label again or another one
function again() {
var $dialog = dialog();
stopFollowing();
$dialog.find('.qbo-label-status').prop('hidden', true);
$dialog.find('#qbo-label-form').show().find('.qbo-label-submit').prop('disabled', false);
redraw();
}
// Moves the issue's Print label link into its top-right actions, before the "..." menu
function placeIssueLink(selector) {
var template = $(selector)[0];
if (!template) { return; }
var $menu = $('#content > .contextual').not('.next-prev-links').first();
if (!$menu.length) { return; }
var $link = $(document.importNode(template.content, true));
var $drdn = $menu.children('.drdn');
if ($drdn.length) { $drdn.first().before($link); } else { $menu.append($link); }
}
return { initDialog: initDialog, follow: follow, again: again, placeIssueLink: placeIssueLink,
layout: layout, LAYOUT: LAYOUT };
})(jQuery);
+102
View File
@@ -0,0 +1,102 @@
/* Print label dialog (redmine_qbo_labels) */
/* The label font, also used by the print agent (DejaVu Sans Bold, Latin subset; see assets/fonts/LICENSE) */
@font-face {
font-family: 'QboLabel';
src: url('../fonts/qbo-label-bold.ttf') format('truetype');
font-weight: 700;
font-style: normal;
font-display: swap;
}
#qbo-label-dialog .qbo-label-preview-wrap {
margin: 0 0 1em;
text-align: center;
}
/* The label as it comes off the roll, scaled to the dialog */
#qbo-label-dialog canvas.qbo-label-preview {
display: block;
margin: 0 auto;
max-width: 100%;
max-height: 160px;
width: auto;
height: auto;
background: #fff;
border: 1px solid #ccc;
border-radius: 6px;
box-shadow: 0 1px 3px rgba(0, 0, 0, .15);
}
#qbo-label-dialog .qbo-label-size {
margin-top: .3em;
color: #888;
font-size: .9em;
}
#qbo-label-dialog .tabular p {
padding-left: 130px;
}
#qbo-label-dialog .tabular label {
width: 120px;
margin-left: -130px;
}
#qbo-label-dialog input.qbo-label-line {
width: 95%;
font-weight: bold;
font-size: 1.1em;
}
#qbo-label-dialog input.qbo-label-copies {
width: 5em;
}
#qbo-label-dialog .qbo-label-warning,
#qbo-label-dialog .qbo-label-status[data-state="failed"] .qbo-label-status-text {
color: #c22;
}
#qbo-label-dialog .qbo-label-status-text {
font-size: 1.2em;
text-align: center;
padding: .5em 0;
}
#qbo-label-dialog .qbo-label-status[data-state="printed"] .qbo-label-status-text {
color: #1a7f37;
font-weight: bold;
}
#qbo-label-dialog .qbo-label-status[data-state="working"] .qbo-label-status-text::before {
content: '';
display: inline-block;
width: .8em;
height: .8em;
margin-right: .5em;
border: 2px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
vertical-align: -1px;
animation: qbo-label-spin .8s linear infinite;
}
@keyframes qbo-label-spin {
to { transform: rotate(360deg); }
}
/* Administration » Label printers */
table.label-printers .status-online { color: #1a7f37; font-weight: bold; }
table.label-printers .status-offline { color: #c22; }
table.label-printers .qbo-label-muted { color: #888; font-style: italic; }
table.label-jobs td.lines { font-weight: bold; white-space: nowrap; }
table.label-jobs td.status-failed,
table.label-jobs td.status-expired { color: #c22; }
table.label-jobs td.status-printed { color: #1a7f37; }
pre.qbo-label-token { white-space: pre-wrap; word-break: break-all; }
@media screen and (max-width: 899px) {
#qbo-label-dialog .tabular p { padding-left: 0; }
#qbo-label-dialog .tabular label { width: auto; margin-left: 0; display: block; text-align: left; float: none; }
}