Lodop打印记录
一、Lodop打印高度自适应
打印高度自适应可以避免因内容超过纸张大小导致内容不能完全打印的问题
LODOP.SET_PRINT_PAGESIZE(3,1385,45,"");
//这里3表示纵向打印且纸高“按内容的高度”;1385表示纸宽138.5mm;45表示页底空白4.5mm
二、Lodop打印Canvas
var canvasArr = document.querySelectorAll('canvas');
LODOP.PRINT_INIT("打印Canvas");
for(var i = 0; i < canvasArr.length; i ++){
var img = new Image();
img.src = canvasArr[i].toDataURL("image/png"); //canvas转图片
LODOP.ADD_PRINT_IMAGE(10,10,2480 * 0.3,3508 * 0.3,img.outerHTML); //添加打印图片
LODOP.SET_PRINT_STYLEA(0, "Stretch", 1); //扩展缩放模式
LODOP.NEWPAGE(); //新建一页
}
LODOP.PREVIEW();
二、Lodop获取本地文件内容并打印
注意:Clodop与lodop不同,需要通过回调函数(CLODOP.On_Return)来接收参数,lodop可以直接接受返回值,以下示例为Clodop
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="http://localhost:8000/CLodopFuncs.js"></script>
</head>
<body style="margin: 0px">
<h3>文件路径</h3>
<input id="fileUrl" value="C:\Users\admin\Desktop\receipt.txt" style="width:500px"></input>
<button onclick="isfileExist()">1.文件是否存在</button>
<button onclick="readfile()">2.获取文件内容</button>
<button onclick="print()">3.打印</button>
<h3>文件内容</h3>
<textarea id="content" style="width:400px;height:500px"></textarea>
</body>
<script>
function isfileExist() {
var strFilename = document.getElementById('fileUrl').value;
if (LODOP.CVERSION) CLODOP.On_Return=function(TaskID,Value){
if (Value) alert("文件存在!");else alert("文件不存在!");
};
LODOP.IS_FILE_EXIST(strFilename);
};
function readfile() {
var strFilename = document.getElementById('fileUrl').value;
if (LODOP.CVERSION) CLODOP.On_Return=function(TaskID,Value){document.getElementById('content').value=Value;};
LODOP.GET_FILE_TEXT(strFilename);
};
function print(){
LODOP.PRINT_INIT("打印文本");
//距离上边的距离,数字越大,距离左边越远
//距离左边的距离,数字越大,距离上边越远
//文本的宽
//文本的高
LODOP.ADD_PRINT_TEXT(50,100,500,800,document.getElementById('content').value);
LODOP.PREVIEW();
}
</script>
</html>