首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为文档生成条形码

为文档生成条形码
EN

Stack Overflow用户
提问于 2021-06-22 14:22:44
回答 2查看 516关注 0票数 0

我有一个棘手的情况,我被困在,我试图创建一个路线,以产生条形码。在此路径上,响应应该是一个SVG映像,其中包含来自文档ID的条形码。是否有人熟悉此操作并可以帮助我:

  • 是否有一个包裹来完成这样的请求,我该去哪里呢?
  • 条形码应该只包含文档Id,如何将其生成到代码-39中。

下面是我的路线样本,非常感谢!

代码语言:javascript
复制
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);
  }
})

EN

回答 2

Stack Overflow用户

发布于 2021-06-22 14:57:29

以下库允许生成条形码:JsBarcode

生成一个代码-39格式。只需添加以下属性:format: 'CODE39'

代码语言:javascript
复制
JsBarcode("#barcode", "DocumentId", {
  format: "CODE39"
});
票数 0
EN

Stack Overflow用户

发布于 2021-06-24 10:17:26

更新:通过遵循其他两条使用JsBarcode的建议,我能够用svg创建条形码,所以我将在这里发布答案:

代码语言:javascript
复制
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;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68085315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档