• 前端实现打印功能Print.js


    前端实现打印的方式有很多种,本人恰好经历了几个项目都涉及到了前端打印,目前较为推荐Print.js来实现前端打印

    话不多说,直接上教程

    官方链接: Print.js官网
    在这里插入图片描述

    1. 在项目中如何下载Print.js

      使用npm下载:npm install print-js --save
      使用yarn下载: yarn add print-js

    2. 在项目中如何引入print.js

    // 第一种 通过import的方式引入
    import print from 'print-js'
    
    // 第二种 通过CDN  
    https://printjs-4de6.kxcdn.com/print.min.js
    https://printjs-4de6.kxcdn.com/print.min.css的方式引入
    
    <script src="print.js"></script>
    <link rel="stylesheet" type="text/css" href="print.css"> //使用Print.js模版的时候引入
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. print.js的参数配置(常用)
    参数描述
    printable(默认null)pdf或图像的url,html元素的id或json数据的对象
    type打印的类型(默认pdf) pdf, html, image, json and raw-html.
    css打印的css,可以是单个URL的字符串,也可以是多个URL的数组
    style格式为一个字符串,该字符串应该应用于正在打印的html样式
    targetStyles打印的样式数值,如需采用自身页面的样式 [‘*’]
    onPrintDialogClose关闭浏览器打印对话框后执行的回调方法
    font_size参数适用于html/json,默认12pt
    1. Print.js如何使用
    import printJS from 'print-js';
    const style = `@page {margin:0mm 0mm 0mm 0mm;}`; //设置打印页面的样式
    printJS({
        printable: idName, // 标签元素id
        type: 'html',
        header: '',
        targetStyles: ['*'],
        style: style,
        font_size: '',
        onPrintDialogClose: () => {
         //打印弹窗关闭后的回调方法
        }
      });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 注意事项
    //如需使用自身页面的样式
     	targetStyles: ['*'],
    	font_size: '',
    //这两个参数必须同时存在
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    千万级订单生成的痛点与架构
    编程学习的方向与赛道的选择没有最优解的
    扎根理论分析软件NVivo原理与技术应用
    使用Java操作Redis
    组合继承
    docker的conda环境中安装mindspore(CPU版本?)
    大气化学在线耦合模式WRF/Chem
    .net SDK 版本信息查询
    探秘扩散模型:训练算法与采样算法的双重解读
    通用代码生成器应用场景三,遗留项目反向工程
  • 原文地址:https://blog.csdn.net/weixin_44180579/article/details/134077696