vue-print-nb uses
Install vue-print-nb
npm install vue- print- nb -- save
Introduced in main.js
import Print from 'vue-print-nb'
Vue. use ( Print)
Use vue-print-nb
< el- button v- print= "printObj" > 打印< / el- button>
printObj : {
id : 'print' ,
popTitle : '' ,
extraCss : '' ,
... ...
} ,
< div id= "print" > 这块内容将会被打印< / div>
Solve the problem of incomplete printing element form
Because the source code needs to be modified, copy the following files directly and put them in the assets directory of the project (copy the node_modules/vue-print-nb/print file to the project, not necessarily the assets directory, where you want to put it, the following is in main Just fill in the corresponding path when importing in .js)
Introduced in main.js (the vue-print-nb introduced in main.js before can be removed)
import Print from './assets/print'
Vue. use ( Print)
Find print.js under the print folder in the assets directory to modify the code and add the beforeEntryIframe hook
new Print ( {
ids : id,
vue,
url : binding. value. url,
standard : '' ,
extraHead : binding. value. extraHead,
extraCss : binding. value. extraCss,
previewTitle : binding. value. previewTitle || '打印预览' ,
zIndex : binding. value. zIndex || 20002 ,
previewPrintBtnLabel : binding. value. previewPrintBtnLabel || '打印' ,
popTitle : binding. value. popTitle,
preview : binding. value. preview || false ,
asyncUrl : binding. value. asyncUrl,
beforeEntryIframe ( ) {
binding. value. beforeEntryIframe && binding. value. beforeEntryIframe ( vue)
} ,
previewBeforeOpenCallback ( ) {
binding. value. previewBeforeOpenCallback && binding. value. previewBeforeOpenCallback ( vue)
} ,
previewOpenCallback ( ) {
binding. value. previewOpenCallback && binding. value. previewOpenCallback ( vue)
} ,
openCallback ( ) {
binding. value. openCallback && binding. value. openCallback ( vue)
} ,
closeCallback ( ) {
binding. value. closeCallback && binding. value. closeCallback ( vue)
} ,
beforeOpenCallback ( ) {
binding. value. beforeOpenCallback && binding. value. beforeOpenCallback ( vue)
}
} ) ;
Find the printarea.js modification code under the print folder in the assets directory, and add a line of code to trigger the beforeEntryIframe function above
getBody ( ) {
let ids = this . settings. ids;
ids = ids. replace ( new RegExp ( "#" , "g" ) , '' ) ;
this . settings. beforeEntryIframe ( ) ;
this . elsdom = this . beforeHanler ( document. getElementById ( ids) ) ;
let ele = this . getFormData ( this . elsdom) ;
let htm = ele. outerHTML;
return '<body>' + htm + '</body>' ;
}
Modify the printObj object in the page using vue-print-nb (vue-print-nb above uses step 3)
printObj : {
id : 'print' ,
popTitle : '' ,
extraCss : '' ,
beforeEntryIframe ( ) {
const cells = document. querySelectorAll ( '.cell' )
const tableNode = document. querySelectorAll ( '.el-table__header,.el-table__body' )
for ( let j = 0 ; j < tableNode. length; j++ ) {
const tableItem = tableNode[ j]
tableItem. style. width = '100%'
const child = tableItem. childNodes
for ( let i = 0 ; i < child. length; i++ ) {
const element = child[ i]
if ( element. localName === 'colgroup' ) {
element. innerHTML = ''
}
}
}
for ( let i = 0 ; i < cells. length; i++ ) {
const cell = cells[ i]
cell. style. width = '100%'
cell. removeAttribute ( 'style' )
}
}
} ,
After the modification, run the code to check the printing effect. When a line cannot fit, the text will be wrapped.