vue2中Print.js的使用超詳細(xì)講解(pdf、html、json、image)
概要
前端實(shí)現(xiàn)打?。ò琾df、html、json、image)
安裝
npm install print-js --save
JSON使用
在項(xiàng)目vue文件中引入
import printJS from "print-js";
點(diǎn)擊按鈕時調(diào)用插件方法
<a-button class="not-print" @click="handlePrint" type="primary" style="margin-top: 20px" >打印</a-button >
handlePrint(data = this.data) { console.log(data); printJS({ // header: '表格標(biāo)題', type: "json", properties: [ { field: "age", displayName: "年齡" }, { field: "name", displayName: "姓名" }, { field: "address", displayName: "地址" }, ], printable: data, // gridHeaderStyle: 'color: red; border: 2px solid #3971A5;', // gridStyle: 'border: 2px solid #3971A5;' header: `<h3 class="custom-h3">${this.title}</h3>`, style: ".custom-h3 { color: red;text-align:center}", }); },
- type:類型(可以是html pdf image json)
- properties:配置json相關(guān)的內(nèi)容(filed要跟json的字段必須一樣?。。。?/li>
- displayName:就是表格的表頭信息
- printable:需要打印的數(shù)據(jù)
- header:可以在表格上方增加一個類似標(biāo)題信息
- style:配置樣式
圖片使用
printJS({printable: 'images/print-01-highres.jpg', type: 'image', header: 'My image header'})
配置都是類似的,單張寫圖片路徑,多張寫成數(shù)組就可以了
Pdf使用
<button type="button" onclick="printJS('docs/printjs.pdf')"> Print PDF </button>
還可以為base64格式
<button type="button" onclick="printJS({printable: base64, type: 'pdf', base64: true})"> Print PDF with Message </button>
最實(shí)用的來了
小編在工作需求中是遇到了打印各種混合的類型,比如說一個表格里面有圖片,其他信息等。因?yàn)閳D片是后端返回來的鏈接,一開始用JSON格式打印出來是表格的形式,圖片這一塊就是個鏈接地址,并沒有跟我們想的一樣是圖片
后來參考Print.js的官網(wǎng),研究了一下,發(fā)現(xiàn)以html形式打印,它會將你整個頁面打印出來。這才是我們想要的格式
Print.js官網(wǎng):
Print.js官網(wǎng):https://printjs.crabbly.com/
<button type="button" onclick="printJS({ printable: 'printJS-form', type: 'html', header: 'PrintJS - Form Element Selection' })"> Print Form with Header </button>
注意此時的printable配置不再是跟JSON、image一樣的數(shù)據(jù)了。這里需要的是一個唯一的元素id,
例如
<a-drawer title="Basic Drawer" placement="right" :closable="false" :visible="visibleD" :after-visible-change="afterVisibleChange" @close="onClose" width="50%" :maskClosable="true"> <div id="basic"> <div v-for="item in 4"> <a-card hoverable style="width: 240px"> <img slot="cover" alt="example" src="https://os.alipayobjects.com/rmsportal/QBnOOoLaAfKPirc.png" /> <a-card-meta title="Europe Street beat"> <template slot="description"> www.instagram.com </template> </a-card-meta> </a-card> </div> </div> <div> <a-button @click="printSure">確定打印</a-button> </div> </a-drawer>
這里的basic就是我要打印的一個id,可以將需要打印的頁面寫在這個地方,循環(huán)遍歷渲染數(shù)據(jù),這樣就很方便了。
demo示例
總結(jié)
到此這篇關(guān)于vue2中Print.js使用的文章就介紹到這了,更多相關(guān)vue2中Print.js使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.js Ajax動態(tài)參數(shù)與列表顯示實(shí)現(xiàn)方法
Vue.js是一個輕巧、高性能、可組件化的MVVM庫,同時擁有非常容易上手的API。下面通過本文給大家介紹vue.js ajax動態(tài)參數(shù)與列表顯示實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2016-10-10Vue.js+HighCharts實(shí)現(xiàn)動態(tài)請求展示時序數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Vue.js+HighCharts實(shí)現(xiàn)動態(tài)請求展示時序數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03解決Vue.js 2.0 有時雙向綁定img src屬性失敗的問題
下面小編就為大家分享一篇解決Vue.js 2.0 有時雙向綁定img src屬性失敗的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03rollup3.x+vue2打包組件的實(shí)現(xiàn)
本文主要介紹了rollup3.x+vue2打包組件的實(shí)現(xiàn),詳細(xì)的介紹了打包會存在的問題,包版本的問題,babel 轉(zhuǎn)換jsx等問題,具有一定的參考價值,感興趣的可以了解一下2023-03-03Vue.js 中 ref 和 reactive 的區(qū)別及用法解析
在Vue.js中,ref主要用于創(chuàng)建響應(yīng)式的引用,通過.value屬性來訪問和修改值,特別適用于需要頻繁更改整個值的場景,而reactive則用于創(chuàng)建深度響應(yīng)的對象或數(shù)組,本文給大家介紹Vue.js 中 ref 和 reactive 的區(qū)別及用法,感興趣的朋友跟隨小編一起看看吧2024-09-09Nuxt升級2.0.0時出現(xiàn)的問題(小結(jié))
這篇文章主要介紹了Nuxt升級2.0.0時出現(xiàn)的問題(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10vue2.x+webpack快速搭建前端項(xiàng)目框架詳解
本文給大家介紹了vue2.x、webpack、vuex、sass+axios、elementUI等快速搭建前端項(xiàng)目框架的詳細(xì)操作方法,需要的跟著學(xué)習(xí)下吧。2017-11-11vue.extend,mixins和vue.component的區(qū)別及說明
Vue.extend 創(chuàng)建Vue的子類,可視為組件構(gòu)造函數(shù),Vue.mixin 允許全局添加方法或?qū)傩?方便所有組件使用,Vue.component 是插件注冊方法,通過Vue.extend創(chuàng)建的組件實(shí)例可以注冊到Vue全局,使其在任何組件中可用2024-09-09