feat: generate barcode
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
This commit is contained in:
parent
2fa50bd7de
commit
106343d33d
3 changed files with 187 additions and 67 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import barcode from "barcode";
|
||||
import { createCanvas } from "canvas";
|
||||
import JsBarcode from "jsbarcode";
|
||||
import createReport from "docx-templates";
|
||||
import ThaiBahtText from "thai-baht-text";
|
||||
import { District, Province, SubDistrict } from "@prisma/client";
|
||||
|
|
@ -253,13 +254,23 @@ export class DocTemplateController extends Controller {
|
|||
thaiBahtText: (input: string | number) => {
|
||||
ThaiBahtText(typeof input === "string" ? input.replaceAll(",", "") : input);
|
||||
},
|
||||
barcode: async (data: string) =>
|
||||
new Promise<string>((resolve, reject) =>
|
||||
barcode("code39", { data, width: 400, height: 100 }).getBase64((err, data) => {
|
||||
if (!err) return resolve(data);
|
||||
return reject(err);
|
||||
}),
|
||||
),
|
||||
barcode: async (data: string, width?: number, height?: number) =>
|
||||
new Promise<{
|
||||
width: number;
|
||||
height: number;
|
||||
data: string;
|
||||
extension: string;
|
||||
}>((resolve) => {
|
||||
const canvas = createCanvas(400, 100);
|
||||
JsBarcode(canvas, data);
|
||||
|
||||
resolve({
|
||||
width: width ?? 8,
|
||||
height: height ?? 3,
|
||||
data: canvas.toDataURL("image/jpeg").slice("data:image/jpeg;base64".length),
|
||||
extension: ".jpeg",
|
||||
});
|
||||
}),
|
||||
},
|
||||
}).then(Buffer.from);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue