瀏覽器打印設(shè)置:無法通過代碼取消頁眉頁腳默認(rèn)勾選
網(wǎng)頁打印功能常常需要精細(xì)控制頁眉頁腳。本文探討如何通過JavaScript或css取消瀏覽器打印設(shè)置中頁眉頁腳的默認(rèn)勾選。 近期,有用戶反饋,使用JavaScript和printJS插件時(shí),無法通過代碼取消默認(rèn)勾選,且@media print樣式失效。
用戶代碼片段如下,使用html2canvas和printJS打印圖片:
html2canvas(this.$refs.templateToImg, { backgroundColor: null, useCORS: true, windowHeight: document.body.scrollHeight, }).then(canvas => { dom.style.height = 'calc(100vh - 400px)' dom.style.overflow = 'auto' const url = canvas.toDataURL('image/jpg') this.img = url const styles = "@media print { @page {height: 100%;@top-left {content: '頁首內(nèi)容';}@bottom-center {content: '頁腳內(nèi)容';}}}" // 無效 printJS({ printable: url, type: 'image', documentTitle: this.previewTitle(), style: styles, onLoadingEnd: () => { this.printLoading = false dom.style.height = 'auto' dom.style.overflow = 'visible' } }) })
經(jīng)分析,瀏覽器打印設(shè)置屬于系統(tǒng)級功能,用戶在打印對話框中手動(dòng)控制。雖然@media print可自定義打印樣式,但無法直接影響瀏覽器打印設(shè)置的默認(rèn)勾選狀態(tài)。 printJS插件的打印機(jī)制與標(biāo)準(zhǔn)瀏覽器打印機(jī)制不同,導(dǎo)致@media print樣式失效。
因此,目前無法通過編程手段取消瀏覽器打印設(shè)置中的頁眉頁腳默認(rèn)勾選。建議用戶打印前手動(dòng)調(diào)整打印設(shè)置,或探索其他間接實(shí)現(xiàn)方法。
立即學(xué)習(xí)“Java免費(fèi)學(xué)習(xí)筆記(深入)”;
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END
喜歡就支持一下吧
相關(guān)推薦