我有一个棘手的情况,我被困在,我试图创建一个路线,以产生条形码。在此路径上,响应应该是一个SVG映像,其中包含来自文档ID的条形码。是否有人熟悉此操作并可以帮助我:
下面是我的路线样本,非常感谢!
router.get('/documents/:id/barcode', async (req, res, next) => {
try {
const document = await Document.getByIdOrName(req.params.id);
if (!document) {
// throw Error
}
// Here I need to send a response SVG image
// with a generated barcode from the the document Id
} catch (error) {
next(error);
}
})
发布于 2021-06-22 14:57:29
以下库允许生成条形码:JsBarcode
生成一个代码-39格式。只需添加以下属性:format: 'CODE39'
JsBarcode("#barcode", "DocumentId", {
format: "CODE39"
});发布于 2021-06-24 10:17:26
更新:通过遵循其他两条使用JsBarcode的建议,我能够用svg创建条形码,所以我将在这里发布答案:
router.get('/:id/barcode', async (req, res, next) => {
try {
const document = await Document.getById(req.params.id);
if (!document) {
throw new NotFoundError(
// throw error,
);
}
const svgDocument = new DOMImplementation().createDocument('');
const svg = svgDocument.createElementNS('', 'svg');
jsBarcode(svg, document.id, {
xmlDocument: svgDocument,
format: 'CODE39',
});
const barcode = new XMLSerializer().serializeToString(svg);
res.send(barcode);
} catch (error) {
next(error);
}
});
module.exports = router;
https://stackoverflow.com/questions/68085315
复制相似问题