feat: migrate PDF417 barcode generation to zxing-wasm/writer

Replace custom PDF417 generation (generateBarcode/renderBarcode) with zxing-wasm's writeBarcode for improved reliability and smaller codebase. Updated all 4 components (BillEditForm, PrintPreview, ViewBillCard, ViewLocationCard) to use new Pdf417BarcodeWasm component with ecLevel 5 for error correction.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Knee Cola
2025-12-20 08:46:38 +01:00
parent 9679246f62
commit b8afb2ef0d
7 changed files with 86 additions and 22 deletions

View File

@@ -19,7 +19,8 @@
"Bash(find:*)",
"mcp__context7__resolve-library-id",
"mcp__context7__get-library-docs",
"mcp__serena__create_text_file"
"mcp__serena__create_text_file",
"Bash(curl:*)"
]
},
"enableAllProjectMcpServers": true,

View File

@@ -9,8 +9,8 @@ import Link from "next/link";
import { formatYearMonth } from "../lib/format";
import { DecodeResult, findDecodePdf417 } from "../lib/pdf/barcodeDecoderWasm";
import { useLocale, useTranslations } from "next-intl";
import { Pdf417Barcode } from "./Pdf417Barcode";
import { InfoBox } from "./InfoBox";
import { Pdf417BarcodeWasm } from "./Pdf417BarcodeWasm";
// Next.js does not encode an utf-8 file name correctly when sending a form with a file attachment
// This is a workaround for that
@@ -203,7 +203,7 @@ export const BillEditForm: FC<BillEditFormProps> = ({ location, bill }) => {
hub3aText ?
<div className="form-control p-1">
<label className="label p-2 grow bg-white border border-gray-300 rounded-box justify-center">
<Pdf417Barcode hub3aText={hub3aText} />
<Pdf417BarcodeWasm hub3aText={hub3aText} />
</label>
<p className="text-xs my-1">{t.rich('barcode-disclaimer', { br: () => <br /> })}</p>
</div> : null

View File

@@ -0,0 +1,76 @@
'use client';
import { useState, useEffect, FC } from 'react';
import { writeBarcode, prepareZXingModule, type WriterOptions } from 'zxing-wasm/writer';
// Configure WASM file location for writer
prepareZXingModule({
overrides: {
locateFile: (path, prefix) => {
if (path.endsWith('.wasm')) {
return window.location.origin + '/zxing_writer.wasm';
}
return prefix + path;
}
}
});
export const Pdf417BarcodeWasm: FC<{ hub3aText: string, className?: string }> = ({ hub3aText, className }) => {
const [barcodeDataUrl, setBarcodeDataUrl] = useState<string | undefined>(undefined);
const [error, setError] = useState<string | undefined>(undefined);
useEffect(() => {
const generateBarcode = async () => {
try {
setError(undefined);
setBarcodeDataUrl(undefined);
const writerOptions: WriterOptions = {
format: 'PDF417',
ecLevel: "5",
scale: 2,
};
const result = await writeBarcode(hub3aText, writerOptions);
// Convert PNG blob to data URL
const reader = new FileReader();
reader.onloadend = () => {
setBarcodeDataUrl(reader.result as string);
};
reader.readAsDataURL(result.image as Blob);
} catch (err) {
console.error('Failed to generate PDF417 barcode:', err);
setError('Failed to generate barcode');
}
};
if (hub3aText) {
generateBarcode();
}
}, [hub3aText]);
// Show error state
if (error) {
return (
<div style={{ width: "350px", height: "92px" }} className="flex items-center justify-center">
<span className="text-error text-sm">{error}</span>
</div>
);
}
// Don't render until barcode is generated (prevents hydration mismatch)
if (!barcodeDataUrl) {
return (
<div style={{ width: "350px", height: "92px" }} className="flex items-center justify-center">
<span className="loading loading-spinner loading-lg"></span>
</div>
);
}
return (
// eslint-disable-next-line @next/next/no-img-element
<img src={barcodeDataUrl} alt="PDF417 Barcode" className={className} />
);
}

View File

@@ -1,7 +1,7 @@
'use client';
import { PrintBarcodeData } from '../lib/actions/printActions';
import { Pdf417Barcode } from './Pdf417Barcode';
import { Pdf417BarcodeWasm } from './Pdf417BarcodeWasm';
export interface PrintPreviewProps {
data: PrintBarcodeData[];
@@ -132,21 +132,8 @@ export const PrintPreview: React.FC<PrintPreviewProps> = ({ data, year, month, t
<td className="border-2 border-gray-800 px-3 py-1.5 text-center">
<div className="flex justify-center items-center">
{
item.hub3aText ?
<Pdf417Barcode hub3aText={item.hub3aText} className="max-h-28 w-auto max-w-[270px] print:m-[5em_auto] print:h-[auto] print:max-h-[85px] print:w-[69.6mm] print:max-w-[69.6mm]" />
: (
// LEGACY SUPPORT ... untill all bills have been migrated
item.barcodeImage ?
// eslint-disable-next-line @next/next/no-img-element
<img
src={item.barcodeImage.startsWith('data:') ? item.barcodeImage : `data:image/png;base64,${item.barcodeImage}`}
alt={`Barcode for ${item.billName}`}
className="max-h-28 w-auto max-w-[270px] print:m-[5em_auto] print:h-[auto] print:max-h-[85px] print:w-[69.6mm] print:max-w-[69.6mm]"
/> : null
)
item.hub3aText ? <Pdf417BarcodeWasm hub3aText={item.hub3aText} className="print:m-[5em_auto]" /> : null
}
</div>
</td>
</tr>

View File

@@ -7,8 +7,8 @@ import Link from "next/link";
import { useRouter } from "next/navigation";
import { formatYearMonth } from "../lib/format";
import { useTranslations } from "next-intl";
import { Pdf417Barcode } from "./Pdf417Barcode";
import { uploadProofOfPayment } from "../lib/actions/billActions";
import { Pdf417BarcodeWasm } from "./Pdf417BarcodeWasm";
export interface ViewBillCardProps {
location: BillingLocation;
@@ -111,7 +111,7 @@ export const ViewBillCard: FC<ViewBillCardProps> = ({ location, bill, shareId })
hub3aText ?
<div className="form-control p-1">
<label className="label p-2 grow bg-white border border-gray-300 rounded-box justify-center">
<Pdf417Barcode hub3aText={hub3aText} />
<Pdf417BarcodeWasm hub3aText={hub3aText} />
</label>
<p className="text-xs my-1">{t.rich('barcode-disclaimer', { br: () => <br /> })}</p>
</div> : null

View File

@@ -7,13 +7,13 @@ import { formatCurrency, formatIban } from "../lib/formatStrings";
import { useTranslations } from "next-intl";
import { useRouter } from "next/navigation";
import { ViewBillBadge } from "./ViewBillBadge";
import { Pdf417Barcode } from "./Pdf417Barcode";
import { EncodePayment, PaymentParams } from "hub-3a-payment-encoder";
import Link from "next/link";
import { LinkIcon } from "@heroicons/react/24/outline";
import { uploadUtilBillsProofOfPayment } from "../lib/actions/locationActions";
import QRCode from "react-qr-code";
import { TicketIcon } from "@heroicons/react/24/solid";
import { Pdf417BarcodeWasm } from "./Pdf417BarcodeWasm";
export interface ViewLocationCardProps {
location: BillingLocation;
@@ -153,7 +153,7 @@ export const ViewLocationCard: FC<ViewLocationCardProps> = ({ location, userSett
<li><strong>{t("payment-reference-label")}</strong><pre className="inline pl-1">{paymentParams.PozivNaBroj}</pre></li>
</ul>
<label className="label p-2 grow bg-white border border-gray-300 rounded-box justify-center">
<Pdf417Barcode hub3aText={hub3aText} />
<Pdf417BarcodeWasm hub3aText={hub3aText} />
</label>
</>
: null

BIN
public/zxing_writer.wasm Normal file

Binary file not shown.