打印 print-js


 

一、傳送門:官方文檔大神文檔

二、單獨使用 print-js

1、封裝 print

 1   print(ref, title, style, type, jsonData, borderHeadStyle, gridStyle, css) {
 2     printJS({
 3       printable: ref,
 4       header: title || null,
 5       type: type || 'html',
 6       headerStyle: 'font-size:6px;font-weight:600;text-align:center;padding:15px 0 10px 0;',//標題設置
 7       properties: jsonData || [],//json數據元
 8       gridHeaderStyle: borderHeadStyle || 'font-size:6px;font-weight:400;height:40px;line-height:40px;border: 1px solid #ccc;padding:3px 5px 3px 5px;text-align:center;',//json格式表頭樣式
 9       gridStyle: gridStyle || 'font-size:1px;font-weight:200;border: 1px solid #ccc;padding:3px 5px 3px 5px;text-align:center;',//json各式表哥央視
10       scanStyles: false,//不適用默認樣式
11       repeatTableHeader: false,//打印json表頭只顯示在第一頁
12       style: style || '@page{size:auto;margin: 0cm 1cm 0cm 1cm;}',//去除頁眉頁腳
13       css: css || null,//css url
14     })
15   }

 

2、調用

// 打印
// this.$refs.printForm 要打印的 內容
goPrint() {
    this.print(this.$refs.printForm)
},

 

 

三、將內容轉圖片格式進行打印(需要額外安裝 html2canvas 插件)

 1   toPrint(ref, title, Rendering) {
 2     html2canvas(ref, {
 3       backgroundColor: null,
 4       useCORS: true,
 5       foreignObjectRendering: Rendering || false,
 6       windowWidth: document.body.scrollWidth,
 7       windowHeight: document.body.scrollHeight
 8     }).then((canvas) => {
 9       const url = canvas.toDataURL()
10       this.img = url
11       printJS({
12         printable: url,
13         type: 'image',
14         documentTitle: title
15       })
16     })
17   }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM