javascript - Converting from type: 'image/png' to ZPL in NodeJs
问题描述
What I'm doing and trying:
I'm trying to convert a screenshot taken from a < div > into ZPL string in NodeJS. Pretty much like http://labelary.com/viewer.html that would take an image and output ZPL code.
What I'm doing:
I'm using a package called 'domToImage'(https://github.com/tsayen/dom-to-image), which takes a screenshot of the DOM I'm stating. I'm currently using the domToImage.toBlob() function which then returns Blob{size: 102776, type: "image/png"}.
For testing to see it actually works I used 'FileSaver'(https://www.npmjs.com/package/file-saver) to save the file as PNG, it truly works and the picture looks great !!
Here is an easy sample code of what I'm doing
domtoimage.toBlob(document.getElementById('labelInfo'))
.then(function (blob)
{
console.log(blob)
saveAs(blob, "test.png");
});
What I'm trying to do is convert that "blob" into ZPL string or format so I can send that to a printer in the network.
What I've tried:
A) I tried installing image-to-zpl(https://www.npmjs.com/package/image-to-zpl) but I think I might be doing something wrong because I'm unable to require it like any other module I was able to install, I get an error saying : Could not find a declaration file for module 'image-to-zpl'. '/path/to/node_modules/image-to-zpl/index.js' implicitly has an 'any' type. I even tried using import instead of require but no luck.
B) I found a code in Java(http://www.jcgonzalez.com/java-image-to-zpl-example) but I'm barely understanding anything and I don't know how to send data from my application, into the Java file then take the string back to the app with NodeJS (I'm a noob).
C) I looked into Labelarys API but all it does is take ZPL into PDF or PNG but not vise versa unfortunately.
D) Thought of using zbtprinter (https://github.com/bstmedia/zbtprinter/) as it actually has the function I need but unfortunately it would send it to a printer using bluetooth directly and not output the ZPL, which in my case can't be used since the printer doesnt have bluetooth. It is on the network. Which also I'm still going to have to learn how to send the whole string directly to a printer through the network :/
I'm in desperate need of help guys pls
EDIT
So this is what I'm doing in the HTML:
<div style="width: 1000px" class="labelInfo" id="labelInfo">
<img src="images/template.bmp" style="max-width: 100%; max-height: 100%">
<div class="ref"> {{ referenceNumber }} </div>
<div class="serial">{{ serialNumber }}</div>
<div class="date" id="date">{{ yearMonth }}</div>
<canvas class="qr" id="canvas"></canvas>
</div>
Description: I'm using a template of an image with all the logos with empty parts of the label that get filled up with the 3 divs; I have 3 divs, based on the users input, those fields gets placed at specified places within the template of the image; the canvas is used for a QR code to be placed depending on the content of the Serial Number.
When the user clicks on a button, the backend code then takes an image of the whole 'labelInfo' div which should then be converted to ZPL in order to be sent to the printer (This is what I'm trying to achieve)
解决方案
将通用图像转换为 ZPL 可用的东西并非易事,并且不能用几行伪代码来描述。相反,我已经将这段代码搁置了一段时间,您的问题促使我清理它并使其可用:
https://github.com/metafloor/zpl-image
或者
npm install zpl-image
适用于浏览器和 node.js。在浏览器中,您传入一个<img>
or<canvas>
元素并以 ZPL 使用的 Z64 压缩 GRF 格式取回渲染图像。
用于打印图像的 ZPL 和斑马打印机的其他内容如下所示:
// image is a <img> or <canvas>
let res = imageToZ64(image);
let zpl = `
^XA^LH0,0^FWN^PON^PMN^LRN
^FO10,10^GFA,${res.length},${res.length},${res.rowlen},${res.z64)
^XZ`;
推荐阅读
- dataset - 数据仓库的数据集
- linux - 由于未安装 LVM 分区导致 CentOS7 意外系统故障
- visual-studio-code - 无法在 Visual Studio Code 中连接 TFS 服务器
- html - 检测 html 容器中突出显示的文本
- wordpress - 从 WordPress 网站自定义登录/重定向
- node.js - 查询数组时出现 MongoDB 数据匹配和获取问题
- pytorch - pytorch loss.backward() 持续运行数小时
- javascript - 合并两个代码
- android - 将 Jetpack Compose 添加到我现有的项目中
- node.js - 为什么当 ConsumedCapacity 超过预置吞吐量时 DynamoDB 没有拒绝?