Work on badge printing and the actual badge layout.

This commit is contained in:
Scott Idem
2025-10-09 19:26:35 -04:00
parent 0f05fd708f
commit 74cc5c5d0d
7 changed files with 811 additions and 79 deletions

View File

@@ -34,6 +34,7 @@ import {
import {
generate_qr_code,
js_generate_qr_code,
} from "$lib/ae_core/core__qr_code";
import {
@@ -697,6 +698,7 @@ let export_obj = {
handle_update_ae_obj_id_crud: handle_update_ae_obj_id_crud,
update_ae_obj_id_crud_v2: update_ae_obj_id_crud_v2,
handle_download_export__obj_type: handle_download_export__obj_type,
generate_qr_code: generate_qr_code
generate_qr_code: generate_qr_code,
js_generate_qr_code: js_generate_qr_code,
};
export let core_func = export_obj;

View File

@@ -1,3 +1,5 @@
import QRCode from 'qrcode'
import type { key_val } from '$lib/ae_stores';
import { api } from '$lib/api';
@@ -136,4 +138,103 @@ export async function generate_qr_code(
// If not returning a blob, return the raw API response
return ae_promises.generate_qr_code;
}
// Updated 2025-10-09
/**
* Generates a QR code image as a Base64 data URL based on provided parameters.
*
* NOTE: This function handles the data formatting and QR generation,
* but does NOT handle server-side logic like file saving, disk path checks,
* or database lookups (like redis_lookup_id_random).
*
* @param {string} qr_type - The type of data format ('mecard', 'vcard', 'obj', 'kv', 'js', 'str').
* @param {Object} params - An object containing all necessary query parameters (n, fn, url, email, etc.).
* @param {string} qr_id - A unique ID (unused in pure client-side generation, but kept for context).
* @returns {Promise<string>} A promise that resolves to a Base64 data URL of the QR code image.
* @throws {Error} If the qr_type is unknown or data is missing.
*/
export async function js_generate_qr_code(qr_type, params = {}) {
const {
n = '', fn = '', org = '', url = '', email = '', tel = '',
adr_poa = '', adr_ext = '', adr_str = '', adr_loc = '', adr_reg = '', adr_postal = '', adr_country = '',
obj_type, obj_id, key, val, js, str
} = params;
console.log(`*** js_generate_qr_code() *** qr_type=${qr_type}`, params);
let qrData = null;
// --- 1. Replicate Data Formatting Logic ---
switch (qr_type) {
case 'mecard':
// Format: MECARD:N:name;EMAIL:email;ADR:address;;
// Note: The original Python code had adr, but we'll use n and email as a minimum.
qrData = `MECARD:N:${n};EMAIL:${email};;`;
// You can enhance this with other MeCard fields if needed.
break;
case 'vcard':
// Format: BEGIN:VCARD...END:VCARD
qrData = `BEGIN:VCARD\nVERSION:3.0\nN:${n}\nFN:${fn}\nORG:${org}\nEMAIL:${email}\n`;
if (url) { qrData += `URL:${url}\n`; }
if (tel) { qrData += `TEL:${tel}\n`; }
if (adr_loc) {
// ADR format: TYPE=postal:Po box;Ext;Street;Locality;Region;Postal code;Country
qrData += `ADR:${adr_poa};${adr_ext};${adr_str};${adr_loc};${adr_reg};${adr_postal};${adr_country}\n`;
}
qrData += 'END:VCARD';
break;
case 'obj':
// Custom format: OBJ:ot:obj_type,oi:obj_id
if (!obj_type || !obj_id) throw new Error('Missing obj_type or obj_id for type "obj".');
qrData = `OBJ:ot:${obj_type},oi:${obj_id}`;
break;
case 'kv':
// Custom format: KV:k:"key",v:"val"
if (!key || !val) throw new Error('Missing key or val for type "kv".');
qrData = `KV:k:"${key}",v:"${val}"`;
break;
case 'js':
// Assumes 'js' is a stringified JSON object
if (!js) throw new Error('Missing js string for type "js".');
qrData = `JS:${js}`;
break;
case 'str':
// Raw string data
if (!str) throw new Error('Missing raw string data for type "str".');
qrData = str;
break;
default:
throw new Error(`Unknown QR type: ${qr_type}`);
}
if (!qrData) {
throw new Error('Failed to create QR code data string.');
}
// --- 2. Generate QR Code Image ---
try {
// Options match the Python 'qrcode' library defaults closely:
// error_correction = qrcode.constants.ERROR_CORRECT_M
// box_size = 10, border = 1
const dataUrl = await QRCode.toDataURL(qrData, {
errorCorrectionLevel: 'M',
margin: 1, // Corresponds to border
scale: 10, // Corresponds to box_size
type: 'image/png',
});
console.log('Generated QR code data URL:', dataUrl);
return dataUrl;
} catch (error) {
console.error('Error generating QR code:', error);
throw new Error('Could not generate QR code image.');
}
}