[聚合文章] 使用jqprint插件实现打印页面内容

JavaScript 2017-03-12 14 阅读
实现效果图

引入js文件

<script type="text/javascript" src="__JS__/jquery-migrate-1.2.1.min.js"></script><script type="text/javascript" src="__JS__/jqprint-0.3.js"></script>

注意这里是先引入的jQuery文件,如果没有引入第一个js文件的话,会出现兼容性问题,导致使用jqprint打印时报错。

html页面

这里面的html标签很多的<div class="wrap-content container" id="container">    <table border="0" cellpadding="0" cellspacing="0" class="store-joinin baseinfo">        <thead>            <tr>                <th colspan="40">用户信息</th>            </tr>        </thead>        <tbody>            <tr >                <th>姓名:</th>                <td colspan="40">18030632605</td>            </tr>            <tr >                <th>性别:</th>                <td colspan="40">男</td>            </tr>            <tr >                <th>年龄:</th>                <td colspan="40">41</td>            </tr>            <tr >                <th>身份证:</th>                <td colspan="40">52272419770101059X</td>            </tr>            <tr >                <th>所属机构:</th>                <td colspan="40">上海市政法委</td>            </tr>        </tbody>      </table>    ......    <button class="btn btn-danger printBtn1" onclick="btnPrintClick()" type="button">打 印</button>

当然下面还有很多的html标签,在这里就不展示了。

打印按钮点击之后执行的函数

function btnPrintClick(){    var imgBox = $('#img_box');    var chartBox = $('#main');    if (imgBox.length <= 0) {        chartBox.after('<div id="img_box"></div>');        imgBox = $('#img_box');    }    // 将echart生成图片并放入img-box,并显示图片img-box    imgBox.html('< img src="' + myChart.getDataURL() + '"/>').css('display','block');    // 隐藏echart图chart-box    chartBox.css('display','none');    // 调整img大小    var img = imgBox.find('img');    var imgWidth = img.width();    var showWidth = 1000; // 显示宽度,即图片缩小到的宽度    if (imgWidth > showWidth) { // 只有当图片大了才缩小        var imgNewHeight = img.height() / (imgWidth / showWidth);        img.css({'width': showWidth + 'px', 'height': imgNewHeight + 'px'});    }    var imgBox2 = $('#img_box2');    var chartBox2 = $('#main2');    if (imgBox2.length <= 0) {        chartBox2.after('<div id="img_box2"></div>');        imgBox2 = $('#img_box2');    }    // 将echart生成图片并放入img-box,并显示图片img-box    imgBox2.html('< img src="' + myChart2.getDataURL() + '"/>').css('display','block');    // 隐藏echart图chart-box        chartBox2.css('display','none');    // 调整img大小    var img2 = imgBox2.find('img');    var img2Width = img2.width();    var show2Width = 1000; // 显示宽度,即图片缩小到的宽度    if (img2Width > show2Width) { // 只有当图片大了才缩小        var img2NewHeight = img2.height() / (img2Width / show2Width);        img2.css({'width': show2Width + 'px', 'height': img2NewHeight + 'px'});    }    // 打印    $("#TestQuestions").jqprint();    // 执行打印后再切换回来    // 显示echart图chart-box    chartBox.css('display','block');    chartBox2.css('display','block');    // 隐藏图片img-box    imgBox.css('display','none');    imgBox2.css('display','none');}

注意事项:

  1. 这里使用了图片的缩放比例的方式。比如Echarts图表要显示的内容特别多的话,一般在html页面中采用的是在X轴上使用滚动条的方式,但是打印时可以将Echarts图表先转换成图片,并等比例进行缩放,打印完毕之后再还原回来即可。

  2. 任何不需要打印的标签都可以先隐藏。对于不需要出现在打印页面的标签内容,我们在打印之前使用jQuery查找到对应的元素,将该元素隐藏起来,打印结束之后,再将隐藏的元素显示出来即可。

注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。