欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue使用Print.js打印頁面樣式不出現(xiàn)的解決

 更新時間:2022年10月19日 16:50:36   作者:迷陣  
這篇文章主要介紹了vue使用Print.js打印頁面樣式不出現(xiàn)的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue Print.js打印頁面樣式不出現(xiàn)

在這里插入圖片描述

在這里插入圖片描述

解決方案

加上這句就好了!完美!

在這里插入圖片描述

vue-print-nb打印問題總結(jié)

1、表格的列缺失(element-ui table組件)

原因:table-layout: fixed導(dǎo)致的,出現(xiàn)部分列沒有被打印

讓表table布局更加符合預(yù)期,普通使用table時,其table-layout 默認(rèn)值是 auto,導(dǎo)致表格第二行和第一行不一樣寬,也就是兩行的寬度不對齊。而使用:

table { table-layout: fixed; }

則會讓表的布局以第一行為準(zhǔn),設(shè)置表格的寬度,然后其他行的表格寬度就按照第一行為準(zhǔn)。一般表格第一行是表頭標(biāo)題,第二行以后是數(shù)據(jù)行,也就是讓數(shù)據(jù)行的每列寬度向第一行列寬度看齊。

這種樣式的表格布局在性能上也快得多,這是因?yàn)檎麄€表格的內(nèi)容不需要花費(fèi)進(jìn)行分析,以便知道列的寬度。

解決方法:

<style lang="less" scoped>
? ? /deep/ table{
? ? ? ? table-layout: auto !important;
? ? }
? ? /deep/ .el-table__header-wrapper .el-table__header{
? ? ? ? width: 100% !important;
? ? }
? ? /deep/ .el-table__body-wrapper .el-table__body{
? ? ? ? width: 100% !important;
? ? }
</style>

注意點(diǎn):

/deep/ table{
? ? ? ? table-layout: auto !important;
? ? }

可能會造成樣式錯亂,比如你頁面有table,打印彈出層的table,這樣修改樣式有可能會導(dǎo)致頁面表格行錯位,解決辦法:在頁面的<el-table>標(biāo)簽上加id,比如pagetable,修改less樣式如下

<style lang="less" scoped>
? ? /deep/ table{
? ? ? ? table-layout: auto !important;
? ? }
? ? /deep/ .el-table__header-wrapper .el-table__header{
? ? ? ? width: 100% !important;
? ? }
? ? /deep/ .el-table__body-wrapper .el-table__body{
? ? ? ? width: 100% !important;
? ? }
? ? /deep/ #pagetable table{
? ? ? ? table-layout: fixed !important;
? ? }
</style>

2、打印內(nèi)容缺失(print.js/print-js獨(dú)有,固定高度導(dǎo)致)

原因:一般為了好看,會固定高度,然后超出內(nèi)容出現(xiàn)滾動條,但是打印的時候,只會打印固定高度的內(nèi)容,導(dǎo)致打印內(nèi)容缺失

解決方法:

<style scoped>
? ? @media print {
? ? ? ? #box{
? ? ? ? ? ? height: 100%;
? ? ? ? }
? ? }
</style>

或者這樣:

找到print.js的getStyle方法,加入一行代碼

str += "<style>html,body,div{height: auto !important;}</style>";
getStyle: function () {
?? ??? ?var str = "",
?? ??? ??? ?styles = document.querySelectorAll('style,link');
?? ??? ?for (var i = 0; i < styles.length; i++) {
?? ??? ??? ?str += styles[i].outerHTML;
?? ??? ?}
?? ??? ?str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
?? ??? ?str += "<style>html,body,div{height: auto !important;}</style>";
?
?? ??? ?return str;
?? ?},

注意點(diǎn):

1、box是你固定高度標(biāo)簽的id,當(dāng)然你也可以換成class或者其他,使樣式生效即可

2、@media print只影響打印的樣式,不會影響頁面樣式 

3、表格內(nèi)容缺失(表格滾動導(dǎo)致,只打印顯示區(qū)域內(nèi)容)

原因:不管是print.js還是vue-print-nb插件,都有這個問題,因?yàn)楸砀駶L動導(dǎo)致

解決方法:

用一個隱藏div包裹你要打印的表格或者還有其他內(nèi)容,總體包裹

<div id="boxbox" style="display:none;">
? ? ? ? <el-table :data="formList" style="width: 100%" border ?width="100%">
? ? ? ? ? ? <el-table-column align="center" ?width="200" prop="prop" ?label="列名"></el-table-column>
? ? ? ? ? ? <el-table-column label="是否啟用" width="100">
? ? ? ? ? ? ? ? <template slot-scope="scope">
? ? ? ? ? ? ? ? ? ? <el-switch v-model="scope.row.show" :active-value="1" :inactive-value="2" active-color="#409eff" inactive-color="#B9B9B9"
? ? ? ? ? ? ? ? ? ? @change="changeSwitch(scope.row)"/>
? ? ? ? ? ? ? ? </template>
? ? ? ? ? ? </el-table-column>
? ? ? ? ? ? <el-table-column label="是否必填" width="100">
? ? ? ? ? ? ? ? <template slot-scope="scope">
? ? ? ? ? ? ? ? ? ? <el-switch v-model="scope.row.required" :active-value="1" :inactive-value="2" active-color="#409eff" inactive-color="#B9B9B9"
? ? ? ? ? ? ? ? ? ? @change="changeSwitch(scope.row)"/>
? ? ? ? ? ? ? ? </template>
? ? ? ? ? ? </el-table-column>
? ? ? ? ? ? <el-table-column align="center" ?prop="sort" width="150" ?label="排序"></el-table-column>
? ? ? ? ? ? <el-table-column label="操作" ?align="center" ?width="200">
? ? ? ? ? ? ? ? <template slot-scope="scope">
? ? ? ? ? ? ? ? ? ? <span v-if="scope.row.sort!=0" class="editrow" @click="up(scope.row)" style="margin-right:10px">上升</span>
? ? ? ? ? ? ? ? ? ? <span v-if="scope.row.sort!=formList.length-1" class="editrow" style="margin-right:10px" @click="down(scope.row)">下降</span>
? ? ? ? ? ? ? ? ? ? <span v-if="scope.row.sort!=0" class="editrow" @click="upToZero(scope.row)" style="margin-right:10px">置頂</span>
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? </template>
? ? ? ? ? ? </el-table-column>
? ? ? ? </el-table>
?
? ? </div>

注意點(diǎn):

1、經(jīng)過測試,A4紙大小寬度大致在650px,所以你隱藏的table列,要自己設(shè)置寬度,整體寬度在750左右,大于750,列會超出,不打印,小于750,右邊會留有空白

2、<el-table>不能固定高度,所以不要設(shè)置高度

4、不能分頁

原因:不管你是下載print.js保存到本地,還是npm下載print-js,只能打印一頁,可能太菜了

解決方法:

使用插件:vue-print-nb,使用方法:vue-print-nb

此插件會根據(jù)打印內(nèi)容的高度,自己分頁,如果想自定義分頁的話,方法如下:

1、在末尾的最后一個標(biāo)簽,加入樣式 style="page-break-after: always;"

<div style="page-break-after: always;">我是本頁的末尾哦</div>

2、定義打印樣式,原理同上,但是方便需要,只需要統(tǒng)一定義class即可

@media print {
? ? ? ? @page{
? ? ? ? ? ? size: ?auto;
? ? ? ? ? ? margin: 3mm;
? ? ? ? }
? ? ? ? .footer {page-break-after: always;}
}

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論