Skip to content

调用后台打印服务API

打印功能

打印类型调用方法
预览preview
直接打印print
打印设置打印printDialog

代码示例

ts

//模版JSON对象
var templateJosn={
  "paperType": "custom",
  "width": 210,
  "height": 297,
  "version": 1,
  "name": "新建模版",
  "direction": "0",
  "bgimg": {},
  "bgshow": false,
  "children": []
  ....
}

//数据源JSON对象
var datasourceJson={
    "公司名称": "石大科技",
    "订单号": "N000001",
    "订货日期": "2025-01-01",
    "交货日期": "2025-02-01",
    ....
}

var license = encodeURIComponent("注册码");
var template = encodeURIComponent(JSON.stringify(templateJosn))
var datasource = encodeURIComponent(JSON.stringify(datasourceJson))
var url = "http://127.0.0.1:9523/preview"
//var url = "http://127.0.0.1:9523/print" //直接打印地址
//var url = "http://127.0.0.1:9523/printDialog" //调用打印设置打印地址
var httpRequest = new XMLHttpRequest();
httpRequest.open("POST", url, true);
httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
httpRequest.onreadystatechange = () => {
    if (httpRequest.readyState == 4 && httpRequest.status == 200) {
        console.info(httpRequest.responseText);
        var data = JSON.parse(httpRequest.responseText);
        if(data.code=='0'){
            alert(data.msg);
        }
    }
}
httpRequest.onerror = function () {
   alert("Peach-Printer打印控件未启动", { icon: 5, title: '错误' })
};
httpRequest.send("template=" + template+ "&datasource=" + datasource + "&license=" + license);

导出功能

导出类型调用方法
PDFprintPDF

代码示例

ts

//模版JSON对象
var templateJosn={
  "paperType": "custom",
  "width": 210,
  "height": 297,
  "version": 1,
  "name": "新建模版",
  "direction": "0",
  "bgimg": {},
  "bgshow": false,
  "children": []
  ....
}

//数据源JSON对象
var datasourceJson={
    "公司名称": "石大科技",
    "订单号": "N000001",
    "订货日期": "2025-01-01",
    "交货日期": "2025-02-01",
    ....
}

var license = encodeURIComponent("注册码");
var template = encodeURIComponent(JSON.stringify(templateJosn))
var datasource = encodeURIComponent(JSON.stringify(datasourceJson))
var url = "http://127.0.0.1:9523/printPDF" //调用导出PDF地址
var httpRequest = new XMLHttpRequest();
    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    httpRequest.responseType = 'blob';
    httpRequest.onreadystatechange = () => {
        if (httpRequest.status == 200) {
            //pdf字节码blob
            let blob = httpRequest.response;
            const downurl = window.URL.createObjectURL(blob)
            const link = document.createElement('a')
            link.href = downurl;
            link.setAttribute('download', "PeachPrinter导出.pdf")
            link.click();
            window.URL.revokeObjectURL(downurl);
        }
    }
    httpRequest.onerror = function () {
        alert("Peach-Printer打印控件未启动")
    };
    httpRequest.send("template=" + template + "&datasource=" + datasource + "&license=" +license);