王新阳

wangxinyang

我的LODOP打印控件代码备份

<script src="/static/lodop/LodopFuncs.js?v=reg"></script>
<script>
//定义打印控件
var LODOP = null;
//已打印标识
var printFlag = [0,0]; //打印次数、页数
window.onload=function(){LODOP=getLodop();};

function createPage(data){
/**
 * LODOP基础知识
 * 计量单位关系如下(其中px单位与像素类似,但不是一个概念)
 * 1in = 2.54cm = 25.4mm = 72pt = 96px
 * 1mm = 2.83465pt = 3.77953px
 * 1pt = 0.352777mm = 1.333333px
 * 1px = 0.264583mm = 0.75pt
 *
 * 字号默认单位为pt,不设置默认为9pt
 * 长度单位默认为px(SET_PRINT_PAGESIZE中的PageWidth和PageHeight除外,默认为0.1mm)
 *
 * 设置纸张大小(initOrient为1-3时会锁定打印方向,为0时由用户控制)
 * SET_PRINT_PAGESIZE(intOrient, PageWidth,PageHeight,strPageName)
 * 不调用此函数则由用户在打印设置中自主设置
 */
	
	//LODOP.PRINT_INIT('证书打印');
	LODOP.PRINT_INITA(0,0,'330mm','240mm','证书打印'); //top,left,width,height,printName,整体打印有偏差时,只需要调整top和left值即可
	LODOP.SET_PRINT_MODE('CUSTOM_TASK_NAME', data.name); //打印任务名
	LODOP.SET_PRINT_MODE('POS_BASEON_PAPER',true); //true以纸张边缘为基点;false可打区域边缘为基点
	LODOP.SET_PRINT_MODE('AUTO_CLOSE_PREWINDOW',1); //打印后自动关闭预览窗口
	//LODOP.SET_PRINT_PAGESIZE(0, '240mm','330mm', 'CreateCustomPage'); // 第一个参数:1纵向打印2横向打印3宽度固定高度自适应0用户控制
	LODOP.SET_PRINT_PAGESIZE(2,0,0,''); //后三个值无效时只用来设置打印方向
	
	LODOP.SET_PRINT_STYLE('FontSize',13.5);  //全局字号,单位pt
	LODOP.SET_PRINT_STYLE('FontName', '新宋体'); //全局字体(宋体、新宋体设置加粗无效)
	LODOP.SET_PRINT_STYLE('Bold',1); //全局加粗
	LODOP.SET_SHOW_MODE('BKIMG_PRINT',false); //是否打印背景图
	//LODOP.SET_PRINT_STYLE('Alignment', 2); //全局水平居中
	
	//LODOP.ADD_PRINT_TEXT(top, left, width, height, textContent); //默认单位px
	LODOP.ADD_PRINT_TEXT(157,912,234,30, data.name);
	//LODOP.SET_PRINT_STYLEA(0, 'FontSize', 13.5); //为前面刚添加的项目设置字号、字体、颜色等打印式样
	LODOP.ADD_PRINT_TEXT(231,912,234,30, data.gender);
	LODOP.ADD_PRINT_TEXT(306,912,234,30, data.nationality);
	LODOP.ADD_PRINT_TEXT(381,912,234,30, data.ethnic_group);
	LODOP.ADD_PRINT_TEXT(455,946,234,30, data.birth);
	LODOP.ADD_PRINT_TEXT(338,289,260,30, data.major_name);
	LODOP.ADD_PRINT_TEXT(418,254,260,30, data.grade);
	LODOP.ADD_PRINT_TEXT(498,289,260,30, data.certdate);
	LODOP.ADD_PRINT_TEXT(578,289,260,30, data.certno);
	LODOP.ADD_PRINT_TEXT(659,275,260,30, data.grade_begin_end);

	if(data.photo){
		LODOP.ADD_PRINT_IMAGE(155,659,'36mm','49mm','<img border="0" src="'+data.photo+'?rnd='+Math.random()+'">');
		LODOP.SET_PRINT_STYLEA(0,'Stretch',2);//默认不设置或0为截取模式;1按打印区域尺寸缩放(变形);2按原图比例(不变形)缩放模式
	}
}

//更新打印日志
function update_print_log(row){
	row.print=parseInt(row.print)+1;
	table.updateRow('myTable', {index:row.LAY_INDEX, data:row}, true);
	$.ajax({
		type: 'POST',
		data: {act:'update_print_log', id:row.id},
		success: function(res){
			if(res.code)return layer.alert(res.msg);
		}
	});
}

function batch_print(){
	var data=table.checkStatus('myTable').dataCache;
	if(!data.length)return layer.msg('请选择要打印的记录', {time:2e3});
	
	$.each(data, function(index,row){
		createPage(row);
		LODOP.PRINT();
		update_print_log(row)
	});
}

//选择打印机
function print_printer(){
	//return layer.alert('请在打印预览界面中点击菜单栏的"设置"选择打印机或打印设置。');
/*
	var printer_list=[];
	for(var i=0; i<LODOP.GET_PRINTER_COUNT(); ++i){
		printer_list.push(LODOP.GET_PRINTER_NAME(i));
	}
	console.log(printer_list);
	return;
*/
}
//打印维护
function print_edit(){
	var row=create_data();
	if(row===null)return layer.alert('无数据');
	createPage(row);
	LODOP.ADD_PRINT_SETUP_BKIMG('<img border="0" src="/static/images/cert.jpg">');
	LODOP.SET_SHOW_MODE('SHOW_SCALEBAR',1); //显示标尺
	LODOP.SET_SHOW_MODE("BKIMG_LEFT",0);
	LODOP.SET_SHOW_MODE("BKIMG_TOP",0);
	LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_SETUP',true);
	LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW',true);
	LODOP.PRINT_SETUP();
}
//打印预览
function print_view(){
	var row=create_data();
	if(row===null)return layer.alert('无数据');
	createPage(row);
	LODOP.ADD_PRINT_SETUP_BKIMG('<img border="0" src="/static/images/cert.jpg">');
	LODOP.SET_SHOW_MODE('BKIMG_IN_PREVIEW',1); //打印预览显示背景图
	LODOP.SET_SHOW_MODE('SHOW_SCALEBAR',1); //显示标尺
	LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD',1); //隐藏打印预览背景进纸版的图案
	LODOP.SET_SHOW_MODE("BKIMG_LEFT",0);
	LODOP.SET_SHOW_MODE("BKIMG_TOP",0);
	LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_SETUP',true);
	LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW',true);
	LODOP.PREVIEW();
}
function print_design(){
	LODOP.PRINT_INITA(0,0, '330mm','240mm', '打印设计');
	LODOP.PRINT_DESIGN();
}
//版本检测
function print_version(){
	try{
		if(LODOP.VERSION) {
			let str=LODOP.CVERSION ? 'C-Lodop版本:'+LODOP.CVERSION+'<br>Lodop版本:'+LODOP.VERSION : '本机已安装Lodop控件!<br>版本号:'+LODOP.VERSION;
			layer.alert(str);
		};
	}catch(e){}
}
//打印预览、打印维护时展示的数据
function create_data(){
	var data=table.getData('myTable');
	if(!data.length)return null;
	var checkData=table.checkStatus('myTable').data;
	return checkData.length ? checkData[0] : data[0];
}
</script>
2025-03-19
2025-04-04 星期五 农历三月初七