【问题标题】:Uncaught (in promise) TypeError: o(...) is not a function when using ImageToZPL from zpl-image packageUncaught (in promise) TypeError: o(...) is not a function when using ImageToZPL from zpl-image package
【发布时间】:2019-12-23 23:03:43
【问题描述】:

我正在尝试将画布从 HRML2Canvas 转换为 PNG 图像,然后将其转换为 ZPL 图像 将 ZPL 命令发送到 Zebra 打印机,但我已经尝试过 solution

但我不断收到此错误:Uncaught (in promise) TypeError: o(...) is not a function

请问有人知道怎么解决吗?

这是我的 JS 代码:

import { Controller } from "stimulus"
import html2canvas from 'html2canvas'
import imageToZ64 from 'zpl-image'

export default class extends Controller {

    connect() {

   $('#barcode-print-button').click((e) => {
            this.printBarcode()
        });
    }
   printBarcode() {
        html2canvas(document.querySelector("#capture")).then(canvas => {
        var Image = canvas.toDataURL("image/png");
        let res = imageToZ64(Image);
        let zpl = `^GFA,${res.length},${res.length},${res.rowlen},${res.z64}`;
                var printWindow = window.open();
                printWindow.document.open("")
                printWindow.document.write(zpl);
                printWindow.document.close();
                printWindow.focus();
                printWindow.print();
                printWindow.close();
          
        });
}
}

【问题讨论】:

    标签: javascript html2canvas zebra-printers stimulusjs


    【解决方案1】:

    两件事:

    您需要使用以下方法捕获命名导出:

    import { imageToZ64 } from 'zpl-image'
    

    您正在使用canvas.toDataURL() 将画布转换为字符串。只需将画布直接传递给imageToZPL()

    html2canvas(document.querySelector("#capture")).then(canvas => {
        let res = imageToZ64(canvas);
        let zpl = `^GFA,${res.length},${res.length},${res.rowlen},${res.z64}`;
        ...
    });
    

    【讨论】:

    • 谢谢,成功了!但现在我收到了这个错误: main.js:66 Uncaught (in promise) ReferenceError: pako is not defined at t (main.js:66) at imageToZ64 (main.js:66) at main.js:28 it无法访问 pako.js 文件我不知道为什么!
    • zpl-image 模块是假设 CJS 或通用浏览器使用的旧代码。对于浏览器,它期望存在一个全局变量pako,用于与 Z64 编码一起使用的 zlib deflate。你能在你的html模板中添加一个<script>标签来包含pako吗?
    • 你说得对,我将脚本的引用添加到 HTML 只是为了尝试并且它有效!我现在可以从我的控制器访问这些功能,这真的很奇怪,谢谢马克!
    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 2013-10-20
    • 2019-12-22
    • 2020-03-17
    • 2020-01-06
    • 2021-03-10
    • 2021-09-30
    • 2020-12-22
    相关资源
    最近更新 更多