NTP_V2/web/src/components/Barcode.tsx

23 lines
999 B
TypeScript

import { useEffect, useRef } from 'react';
import JsBarcode from 'jsbarcode';
/**
* Code 128 barcode of `value`, drawn on a canvas (black bars on white, no caption). Drawn synchronously in the effect so the
* PDF capture never sees an empty canvas. Legacy printed the permit number as a barcode image
* (application.objCMN.generatebarCode); this is the same idea.
*/
export function Barcode({ value, height = 64, barWidth = 2 }: { value: string; height?: number; barWidth?: number }) {
const ref = useRef<HTMLCanvasElement>(null);
useEffect(() => {
if (!value || !ref.current) return;
try {
JsBarcode(ref.current, value, { format: 'CODE128', displayValue: false, width: barWidth, height, margin: 0, background: '#ffffff', lineColor: '#000000' });
} catch {
/* an unprintable value simply leaves the canvas empty */
}
}, [value, height, barWidth]);
return <canvas ref={ref} role="img" aria-label={value} style={{ maxWidth: '100%', display: 'block' }} />;
}