生活随笔
收集整理的這篇文章主要介紹了
前端打印功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法一:
html代碼:
在要打印的內容上加入:<!--startprint-->和<!--endprint-->
js代碼:
$('.printData').click(function(){bdhtml=window.document.body.innerHTML; sprnstr="<!--startprint-->"; eprnstr="<!--endprint-->"; prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); window.document.body.innerHTML=prnhtml; window.print();})
去除打印按鈕,在css中加入:
@media print {.noprint{display: none;}}
方法二:
<buttonn
>打印
</buttton
><div id
="print">
<!-- 要打印的內容
-->
<div
>
$('.printData').click(function(){
let newstr
= document
.getElementById('print').innerHTML
;console
.log(newstr
)window
.document
.body
.innerHTML
= newstr
;let oldstr
= window
.document
.body
.innerHTML
;window
.print();window
.location
.reload(); window
.document
.body
.innerHTML
= oldstr
;return false;
})
方法三:lodop 官網:http://www.lodop.net/index.html
語法:更詳細的可以戳 “這里”
(1) PRINT_INIT(strPrintTaskName)打印初始化
(2)SET_PRINT_PAGESIZE(intOrient,intPageWidth,intPageHeight,strPageName)設定紙張大小
(3)ADD_PRINT_HTM(intTop,intLeft,intWidth,intHeight,strHtml)增加超文本項
(4) ADD_PRINT_TEXT(intTop,intLeft,intWidth,intHeight,strContent)增加純文本項
(5) ADD_PRINT_TABLE(intTop,intLeft,intWidth,intHeight,strHtml)增加表格項
(6) ADD_PRINT_SHAPE(intShapeType,intTop,intLeft,intWidth,intHeight,intLineStyle,intLineWidth,intColor)畫圖形
(7)SET_PRINT_STYLE(strStyleName, varStyleValue)設置對象風格
(8)PREVIEW打印預覽
(9)PRINT直接打印
(10)PRINT_SETUP打印維護
(11)PRINT_DESIGN打印設計去下載中心下載案例,保留 js 文件。
我這里用在 vue 項目中,所以要對 js 進行改造。感謝一下https://www.jianshu.com/p/d288963c1a80提供的代碼
var CreatedOKLodop7766
= nullexport function needCLodop() {try {var ua
= navigator
.userAgent
if (ua
.match(/Windows\sPhone/i) != null) return trueif (ua
.match(/iPhone|iPod/i) != null) return trueif (ua
.match(/Android/i) != null) return trueif (ua
.match(/Edge\D?\d+/i) != null) return truevar verTrident
= ua
.match(/Trident\D?\d+/i)var verIE
= ua
.match(/MSIE\D?\d+/i)var verOPR
= ua
.match(/OPR\D?\d+/i)var verFF
= ua
.match(/Firefox\D?\d+/i)var x64
= ua
.match(/x64/i)if (verTrident
== null && verIE
== null && x64
!== null) return trueelse if (verFF
!== null) {verFF
= verFF
[0].match(/\d+/)if (verFF
[0] >= 41 || x64
!== null) return true} else if (verOPR
!== null) {verOPR
= verOPR
[0].match(/\d+/)if (verOPR
[0] >= 32) return true} else if (verTrident
== null && verIE
== null) {var verChrome
= ua
.match(/Chrome\D?\d+/i)if (verChrome
!== null) {verChrome
= verChrome
[0].match(/\d+/)if (verChrome
[0] >= 41) return true}}return false} catch (err) {return true}
}
if (needCLodop()) {var head
=document
.head
||document
.getElementsByTagName('head')[0] ||document
.documentElement
var oscript
= document
.createElement('script')oscript
.src
= 'http://localhost:8000/CLodopfuncs.js?priority=1'head
.insertBefore(oscript
, head
.firstChild
)oscript
= document
.createElement('script')oscript
.src
= 'http://localhost:18000/CLodopfuncs.js?priority=0'head
.insertBefore(oscript
, head
.firstChild
)
}
export function getLodop(oOBJECT
, oEMBED
) {var strHtmInstall
="<br><font color='#FF00FF'>打印控件未安裝!點擊這里<a href='install_lodop32.exe' target='_self'>執行安裝</a>,安裝后建議重新打開瀏覽器刷新頁面或重新打開瀏覽器。</font>"var strHtmUpdate
="<br><font color='#FF00FF'>打印控件需要升級!點擊這里<a href='install_lodop32.exe' target='_self'>執行升級</a>,升級后請重新打開瀏覽器。</font>"var strHtm64_Install
="<br><font color='#FF00FF'>打印控件未安裝!點擊這里<a href='install_lodop64.exe' target='_self'>執行安裝</a>,安裝后請刷新頁面或重新打開瀏覽器。</font>"var strHtm64_Update
="<br><font color='#FF00FF'>打印控件需要升級!點擊這里<a href='install_lodop64.exe' target='_self'>執行升級</a>,升級后請重新打開瀏覽器。</font>"var strHtmFireFox
="<br><br><font color='#FF00FF'>(注意:如曾安裝過Lodop舊版附件npActiveXPLugin,請在【工具】->【附加組件】->【擴展】中先卸它)</font>"var strHtmChrome
="<br><br><font color='#FF00FF'>(如果此前正常,僅因瀏覽器升級或重安裝而出問題,需重新執行以上安裝)</font>"var strCLodopInstall
="<br><font color='#FF00FF'>CLodop云打印服務(localhost本地)未安裝啟動!點擊這里<a href='http://www.lodop.net/download/CLodop_Setup_for_Win32NT_https_3.046Extend.zip' download target='_self'>執行安裝</a>,安裝后請刷新頁面。</font>"var strCLodopUpdate
="<br><font color='#FF00FF'>CLodop云打印服務需升級!點擊這里<a href='http://www.lodop.net/download/CLodop_Setup_for_Win32NT_https_3.046Extend.zip' download target='_self'>執行升級</a>,升級后請刷新頁面。</font>"var LODOPtry {var isIE
=navigator
.userAgent
.indexOf('MSIE') >= 0 ||navigator
.userAgent
.indexOf('Trident') >= 0if (needCLodop()) {try {LODOP = getCLodop()} catch (err) {}if (!LODOP && document
.readyState
!== 'complete') {alert('C-Lodop沒準備好,請稍后再試!')return}if (!LODOP) {if (isIE
) document
.write(strCLodopInstall
)elsedocument
.body
.innerHTML
=strCLodopInstall
+ document
.body
.innerHTML
return} else {if (CLODOP.CVERSION < '3.0.4.3') {if (isIE
) document
.write(strCLodopUpdate
)elsedocument
.body
.innerHTML
=strCLodopUpdate
+ document
.body
.innerHTML
}if (oEMBED
&& oEMBED
.parentNode
)oEMBED
.parentNode
.removeChild(oEMBED
)if (oOBJECT
&& oOBJECT
.parentNode
)oOBJECT
.parentNode
.removeChild(oOBJECT
)}} else {var is64IE
= isIE
&& navigator
.userAgent
.indexOf('x64') >= 0if (oOBJECT
!= undefined
|| oEMBED
!= undefined
) {if (isIE
) LODOP = oOBJECT
else LODOP = oEMBED
} else if (CreatedOKLodop7766
== null) {LODOP = document
.createElement('object')LODOP.setAttribute('width', 0)LODOP.setAttribute('height', 0)LODOP.setAttribute('style','position:absolute;left:0px;top:-100px;width:0px;height:0px;',)if (isIE
)LODOP.setAttribute('classid','clsid:2105C259-1E0C-4534-8141-A753534CB4CA',)else LODOP.setAttribute('type', 'application/x-print-lodop')document
.documentElement
.appendChild(LODOP)CreatedOKLodop7766
= LODOP} else LODOP = CreatedOKLodop7766
if (LODOP == null || typeof LODOP.VERSION == 'undefined') {if (navigator
.userAgent
.indexOf('Chrome') >= 0)document
.body
.innerHTML
=strHtmChrome
+ document
.body
.innerHTML
if (navigator
.userAgent
.indexOf('Firefox') >= 0)document
.body
.innerHTML
=strHtmFireFox
+ document
.body
.innerHTML
if (is64IE
) document
.write(strHtm64_Install
)else if (isIE
) document
.write(strHtmInstall
)elsedocument
.body
.innerHTML
=strHtmInstall
+ document
.body
.innerHTML
return LODOP}}if (LODOP.VERSION < '6.2.2.3') {if (!needCLodop()) {if (is64IE
) document
.write(strHtm64_Update
)else if (isIE
) document
.write(strHtmUpdate
)elsedocument
.body
.innerHTML
=strHtmUpdate
+ document
.body
.innerHTML
}return LODOP}return LODOP} catch (err) {alert('getLodop出錯:' + err
)}
}
把它放到 src 下的 plugs 文件夾中,在需要用的頁面中引入
import {getLodop} from '@/plugs/vuePrint/LodopFuncs.js'
5.要注意的是,lodop 不能讀取外部樣式
復制demo中的 js 部分到 methods
總結
以上是生活随笔為你收集整理的前端打印功能的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。