fastlion-amis
Version:
一种MIS页面生成工具
274 lines (273 loc) • 15.7 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.filePrint = exports.labelPrint = exports.billPrint = exports.templateDesign = exports.getPrinters = void 0;
var LodopFuncs_1 = require("./LodopFuncs");
var js_base64_1 = require("js-base64");
function default_1() { }
exports.default = default_1;
// 由于getLodop()获取的LODOP是同一个对象,因此LODOP.On_Return重复赋值会被覆盖掉
// 所以将每次打印赋给On_Return的函数依次放到printCallbackArr里,(LODOP的打印任务是依次执行),每次取第一个数组的第一个函数执行
// 当当前任务的打印数达到目标后,删除对应的函数(数组第一个),此时第二个元素变成第一个,再赋值给On_Return
// var printCallbackArr: any[] = []
function new_eval(str) {
var fn = Function;
return new fn(str)();
}
// 获取系统打印机设备信息
function getPrinters() {
var LODOP = (0, LodopFuncs_1.getLodop)();
// 获取系统打印设备数量
var count = LODOP.GET_PRINTER_COUNT();
var printers = [];
for (var i = 0; i < count; i++) {
printers.push(LODOP.GET_PRINTER_NAME(i));
}
return printers;
}
exports.getPrinters = getPrinters;
function templateDesign(rowData, onCompleted) {
var LODOP = (0, LodopFuncs_1.getLodop)();
var tempContent = rowData.TEMP_CONTENT, width = rowData.WIDTH, height = rowData.HEIGHT;
LODOP.PRINT_INITA(0, 0, width + "mm", height + "mm", 'design');
if (tempContent) {
var temp = (0, js_base64_1.decode)(tempContent); // 解码base64
// 保存模板包括了初始化
// 将初始化语句分离出来
var initalSentenceIndex = temp.indexOf(';');
// 将初始化语句分离后的剩余的模板语句
var restSentence = temp.substring(initalSentenceIndex + 1);
new_eval(restSentence);
}
LODOP.PRINT_DESIGN();
LODOP.On_Return = function (_taskId, value) {
onCompleted((0, js_base64_1.encode)(value));
};
}
exports.templateDesign = templateDesign;
function billPrint(params, onCompleted) {
var LODOP = (0, LodopFuncs_1.getLodop)();
var templateDatas = params.templateDatas, direction = params.direction, pageWidth = params.pageWidth, pageHeight = params.pageHeight, printer = params.printer, preview = params.preview, count = params.count;
LODOP.PRINT_INITA(0, 0, pageWidth + "mm", pageHeight + "mm", 'billprint');
LODOP.SET_PRINT_PAGESIZE(direction ? 1 : 2, pageWidth * 10, pageHeight * 10, '');
for (var i = 0; i < templateDatas.length; i++) {
LODOP.NEWPAGEA();
var templateData = templateDatas[i];
for (var j = 0; j < templateData.length; j++) {
var labelData = templateData[j];
var labelTexts = labelData.labelTexts, labelHtmls = labelData.labelHtmls, labelImages = labelData.labelImages, labelTables = labelData.labelTables, labelBarCodes = labelData.labelBarCodes, labelRects = labelData.labelRects, labelEllipses = labelData.labelEllipses, labelLines = labelData.labelLines, labelShapes = labelData.labelShapes;
labelTables.forEach(function (table) {
LODOP.ADD_PRINT_TABLE(table.top.strValue, table.left.strValue, table.width.strValue, table.height.strValue, table.content);
var styles = table.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelTexts.forEach(function (text) {
var fieldName = text.fieldName;
if (fieldName) {
LODOP.ADD_PRINT_TEXTA(fieldName, text.top.strValue, text.left.strValue, text.width.strValue, text.height.strValue, text.content);
}
else {
LODOP.ADD_PRINT_TEXT(text.top.strValue, text.left.strValue, text.width.strValue, text.height.strValue, text.content);
}
var styles = text.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelHtmls.forEach(function (html) {
LODOP.ADD_PRINT_HTM(html.top.strValue, html.left.strValue, html.width.strValue, html.height.strValue, html.content);
var styles = html.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelImages.forEach(function (image) {
LODOP.ADD_PRINT_IMAGE(image.top.strValue, image.left.strValue, image.width.strValue, image.height.strValue, image.url);
var styles = image.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelBarCodes.forEach(function (barCode) {
var fieldName = barCode.fieldName;
if (fieldName) {
LODOP.ADD_PRINT_BARCODEA(fieldName, barCode.top.strValue, barCode.left.strValue, barCode.width.strValue, barCode.height.strValue, barCode.codeType, barCode.codeValue);
}
else {
LODOP.ADD_PRINT_BARCODE(barCode.top.strValue, barCode.left.strValue, barCode.width.strValue, barCode.height.strValue, barCode.codeType, barCode.codeValue);
}
var styles = barCode.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelRects.forEach(function (rect) { return LODOP.ADD_PRINT_RECT(rect.top.strValue, rect.left.strValue, rect.width.strValue, rect.height.strValue, rect.lineStyle, rect.lineWidth); });
labelEllipses.forEach(function (ellipse) { return LODOP.ADD_PRINT_ELLIPSE(ellipse.top.strValue, ellipse.left.strValue, ellipse.width.strValue, ellipse.height.strValue, ellipse.lineStyle, ellipse.lineWidth); });
labelLines.forEach(function (line) { return LODOP.ADD_PRINT_LINE(line.top.strValue, line.left.strValue, line.width.strValue, line.height.strValue, line.lineStyle, line.lineWidth); });
labelShapes.forEach(function (shape) { return LODOP.ADD_PRINT_SHAPE(shape.shapeType, shape.top.strValue, shape.left.strValue, shape.width.strValue, shape.height.strValue, shape.lineStyle, shape.lineWidth, shape.fillColor); });
}
}
if (preview) {
LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true);
LODOP.SET_PRINTER_INDEX(printer);
LODOP.PREVIEW('label-preview');
}
else {
LODOP.SET_PRINT_COPIES(count);
LODOP.SET_PRINTER_INDEX(printer);
LODOP.PRINT();
}
if (onCompleted) {
LODOP.On_Return = function () { return onCompleted(); };
}
}
exports.billPrint = billPrint;
function labelPrint(params, onCompleted) {
var LODOP = (0, LodopFuncs_1.getLodop)();
var templateDatas = params.templateDatas, marginTop = params.marginTop, marginLeft = params.marginLeft, direction = params.direction, pageWidth = params.pageWidth, pageHeight = params.pageHeight, preview = params.preview, printer = params.printer, count = params.count;
LODOP.PRINT_INITA(marginTop + "mm", marginLeft + "mm", pageWidth + "mm", pageHeight + "mm", 'print');
LODOP.SET_PRINT_PAGESIZE(direction ? 1 : 2, pageWidth * 10, pageHeight * 10, '');
for (var i = 0; i < templateDatas.length; i++) {
LODOP.NEWPAGEA();
var templateData = templateDatas[i];
for (var j = 0; j < templateData.length; j++) {
var labelData = templateData[j];
var tempTop = labelData.tempTop, tempLeft = labelData.tempLeft, tempWidth = labelData.tempWidth, tempHeight = labelData.tempHeight, labelTexts = labelData.labelTexts, labelHtmls = labelData.labelHtmls, labelImages = labelData.labelImages, labelBarCodes = labelData.labelBarCodes, labelRects = labelData.labelRects, labelEllipses = labelData.labelEllipses, labelLines = labelData.labelLines, labelShapes = labelData.labelShapes;
labelTexts.forEach(function (text) {
var fieldName = text.fieldName;
if (fieldName) {
LODOP.ADD_PRINT_TEXTA(fieldName, text.top.strValue, text.left.strValue, text.width.strValue, text.height.strValue, text.content);
}
else {
LODOP.ADD_PRINT_TEXT(text.top.strValue, text.left.strValue, text.width.strValue, text.height.strValue, text.content);
}
var styles = text.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelHtmls.forEach(function (html) {
LODOP.ADD_PRINT_HTM(html.top.strValue, html.left.strValue, html.width.strValue, html.height.strValue, html.content);
var styles = html.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelImages.forEach(function (image) {
LODOP.ADD_PRINT_IMAGE(image.top.strValue, image.left.strValue, image.width.strValue, image.height.strValue, image.url);
var styles = image.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelBarCodes.forEach(function (barCode) {
var fieldName = barCode.fieldName;
if (fieldName) {
LODOP.ADD_PRINT_BARCODEA(fieldName, barCode.top.strValue, barCode.left.strValue, barCode.width.strValue, barCode.height.strValue, barCode.codeType, barCode.codeValue);
}
else {
LODOP.ADD_PRINT_BARCODE(barCode.top.strValue, barCode.left.strValue, barCode.width.strValue, barCode.height.strValue, barCode.codeType, barCode.codeValue);
}
var styles = barCode.styles;
if (styles) {
styles.forEach(function (_a) {
var id = _a[0], name = _a[1], value = _a[2];
LODOP.SET_PRINT_STYLEA(id, name, value);
});
}
});
labelRects.forEach(function (rect) { return LODOP.ADD_PRINT_RECT(rect.top.strValue, rect.left.strValue, rect.width.strValue, rect.height.strValue, rect.lineStyle, rect.lineWidth); });
labelEllipses.forEach(function (ellipse) { return LODOP.ADD_PRINT_ELLIPSE(ellipse.top.strValue, ellipse.left.strValue, ellipse.width.strValue, ellipse.height.strValue, ellipse.lineStyle, ellipse.lineWidth); });
labelLines.forEach(function (line) { return LODOP.ADD_PRINT_LINE(line.top.strValue, line.left.strValue, line.width.strValue, line.height.strValue, line.lineStyle, line.lineWidth); });
labelShapes.forEach(function (shape) { return LODOP.ADD_PRINT_SHAPE(shape.shapeType, shape.top.strValue, shape.left.strValue, shape.width.strValue, shape.height.strValue, shape.lineStyle, shape.lineWidth, shape.fillColor); });
if (preview) {
LODOP.ADD_PRINT_RECT(tempTop.strValue, tempLeft.strValue, tempWidth.strValue, tempHeight.strValue, 0, 1);
}
}
}
if (preview) {
LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true);
LODOP.SET_PRINTER_INDEX(printer);
LODOP.PREVIEW('label-preview');
}
else {
LODOP.SET_PRINT_COPIES(count);
LODOP.SET_PRINTER_INDEX(printer);
LODOP.PRINT();
}
if (onCompleted) {
LODOP.On_Return = function () { return onCompleted(); };
}
}
exports.labelPrint = labelPrint;
// 文件仅预览
function filePrint(params, onCompleted) {
var LODOP = (0, LodopFuncs_1.getLodop)();
var fileDatas = params.fileDatas, printer = params.printer, direction = params.direction, pageWidth = params.pageWidth, pageHeight = params.pageHeight, preview = params.preview;
LODOP.PRINT_INIT();
for (var i = 0; i < fileDatas.length; i++) {
LODOP.NEWPAGEA();
var content = fileDatas[i].content;
if (fileDatas[i].type === 'pdf') {
LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', content); // 这里设置的宽高是无效的
LODOP.SET_PRINT_STYLEA(0, 'PDFScalMode', 1);
}
else {
// 测试地址图片 'https://img.zcool.cn/community/0147e86243ff520002c3290f77f99d.jpg@1280w_1l_2o_100sh.jpg'
LODOP.ADD_PRINT_HTML(0, 0, '100%', '100%', "<img style=\"width:100%\" src=" + content + " />");
}
// 整页按比例缩放
// 上面的语句会将内容铺满整张纸,由于打印机的可打印区域不是整张纸,因此打印可能会被裁掉部分内容,所以将内容缩小一些
LODOP.SET_PRINT_MODE('PRINT_PAGE_PERCENT', '95%');
// 对于单页输出,如希望内容超出纸宽或纸高时对应缩小,用如下俩语句:
// LODOP.SET_PRINT_MODE('FULL_WIDTH_FOR_OVERFLOW', true);
// LODOP.SET_PRINT_MODE('FULL_HEIGHT_FOR_OVERFLOW', true);
// LODOP.SET_PRINT_STYLEA(0, 'ScalX', 1.5); // 这个可以设置缩放高度、宽度比例
}
LODOP.SET_PRINT_PAGESIZE(direction ? 1 : 2, pageWidth * 10, pageHeight * 10, ''); // 设置纸张大小
LODOP.SET_PRINTER_INDEX(printer); // 设置打印机
LODOP.SET_SHOW_MODE('HIDE_SBUTTIN_PREVIEW', true); // 预览窗口隐藏设置按钮
LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true); // 预览窗口隐藏打印按钮
LODOP.SET_SHOW_MODE('LANDSCAPE_DEFROTATED', true); // 横向打印时正向显示
// LODOP.SET_PRINT_MODE('AUTO_CLOSE_PREWINDOW', true); // 打印完毕自动关闭预览窗口
// LODOP.SET_PREVIEW_WINDOW(1, 0, 1, 900, 600); // 设置预览窗口
if (preview) {
LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true);
LODOP.SET_PRINTER_INDEX(printer);
LODOP.PREVIEW('label-preview');
}
else {
LODOP.SET_PRINTER_INDEX(printer);
LODOP.PRINT();
}
if (onCompleted) {
LODOP.On_Return = function () { return onCompleted(); };
}
}
exports.filePrint = filePrint;
//# sourceMappingURL=./utils/print/index.js.map
;