Summary of two methods to implement vue printing function

Summary of two methods to implement vue printing function

Method 1: Install the plugin via npm

1. Install npm install vue-print-nb --save

2. After installation, import it into the main.js file

import Print from 'vue-print-nb' Vue.use(Print); //Register

3. Now you can use it

<div id="printTest" >

  <p>The moon shines on the mountains</p>

    <p>The breeze comes from the river</p>

</div>

<button v-print="'#printTest'">Print</button>

4. To print via the link address: window.location.href = airway_bill; airway_bill is the link address.

5. If the content is not printed completely, click More Settings during the print operation, and then set the zoom.

The second method: manually download the plug-in to the local

Plugin address:

https://github.com/xyl66/vuePlugs_printjs

1. Create a new folder plugs under src, put the downloaded print.js into the plugs folder, and then do the following

import Print from '@/plugs/print'
Vue.use(Print) // Register <template>
  <section ref="print">
    Print content <div class="no-print">Don't print me</div>
  </section>
</template>
this.$print(this.$refs.print) // Usage

2. Note: You need to use ref to get the DOM node. If you get it directly by id or class, the printed content will be empty after webpack packaging and deployment.

3. Specify the non-printing area

Method 1. Add no-print style class

<div class="no-print">Don't print me</div>

Method 2. Custom class name

<div class="do-not-print-me-xxx">Don't print me</div>this.$print(this.$refs.print,{'no-print':'.do-not-print-me-xxx'}) // Use

Batch Printing

Batch printing here actually uses pure js writing, let's go directly to the code:

<template>
    <div>
        <ul class="print-ul">
            <li v-for="(item,index) of tableData" :key="index" 
                :id="'printDiv' + index" style="page-break-after:always;">
                <div>
                    <p>{{item.date}}</p>
                    <p>{{item.name}}</p>
                    <p>{{item.province}}</p>
                    <p>{{item.city}}</p>
                    <p>{{item.address}}</p>
                    <p>{{item.zip}}</p>
                </div>
            </li>
        </ul>
        <div @click="handlePrint">Print</div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: [{
                    date: '2016-05-03',
                    name: 'Wang Xiaohu',
                    province: 'Shanghai',
                    city: 'Putuo District',
                    address: 'No. 1518, Jinshajiang Road, Putuo District, Shanghai',
                    zip: 200333
                }, {
                    date: '2016-05-02',
                    name: 'Wang Xiaohu',
                    province: 'Shanghai',
                    city: 'Putuo District',
                    address: 'No. 1518, Jinshajiang Road, Putuo District, Shanghai',
                    zip: 200333
                }, {
                    date: '2016-05-04',
                    name: 'Wang Xiaohu',
                    province: 'Shanghai',
                    city: 'Putuo District',
                    address: 'No. 1518, Jinshajiang Road, Putuo District, Shanghai',
                    zip: 200333
                }, {
                    date: '2016-05-01',
                    name: 'Wang Xiaohu',
                    province: 'Shanghai',
                    city: 'Putuo District',
                    address: 'No. 1518, Jinshajiang Road, Putuo District, Shanghai',
                    zip: 200333
                }, {
                    date: '2016-05-08',
                    name: 'Wang Xiaohu',
                    province: 'Shanghai',
                    city: 'Putuo District',
                    address: 'No. 1518, Jinshajiang Road, Putuo District, Shanghai',
                    zip: 200333
                }, {
                    date: '2016-05-06',
                    name: 'Wang Xiaohu',
                    province: 'Shanghai',
                    city: 'Putuo District',
                    address: 'No. 1518, Jinshajiang Road, Putuo District, Shanghai',
                    zip: 200333
                }, {
                    date: '2016-05-07',
                    name: 'Wang Xiaohu',
                    province: 'Shanghai',
                    city: 'Putuo District',
                    address: 'No. 1518, Jinshajiang Road, Putuo District, Shanghai',
                    zip: 200333
                }]
            }
        },
        methods: {
            handlePrint() {
                var newWin = window.open(""); //Open a new empty window for (var i = 0; i < this.tableData.length; i++) {
                    var imageToPrint = document.getElementById("printDiv" + i); //Get the content to be printed newWin.document.write(imageToPrint.outerHTML); //Add the content to be printed to the new window}
                const styleSheet = `<style>li{list-style:none}</style>`; 
                newWin.document.head.innerHTML = styleSheet; //Add style to the printed content newWin.document.close(); //This sentence must be added when used in IE browser newWin.focus(); //This sentence must be added when used in IE browser setTimeout(function() {
                    newWin.print(); //Print newWin.close(); //Close the window }, 100);
            }
        }
    }
</script>

<style>
    .print-ul {
        width: 600px;
        list-style: none;
        border: 1px solid #e8e8e8;
    }
</style>

Summarize

This is the end of this article about the implementation of vue printing function. For more relevant vue printing function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Two ways to implement printing function in Vue
  • Using vue-print.js in vue to achieve multi-page printing
  • Implementation code of vue printing plug-in vue-print-nb
  • How to print pages with vue-print-nb in vue

<<:  Solution to the problem of installing MySQL compressed version zip

>>:  How to completely delete the MySQL service (clean the registry)

Recommend

How to use CSS style to vertically center the font in the table

The method of using CSS style to vertically cente...

Vue implements infinite loading waterfall flow

This article example shares the specific code of ...

More Ways to Use Angle Brackets in Bash

Preface In this article, we will continue to expl...

Implementation of CSS heart-shaped loading animation source code

Without further ado, let me show you the code. Th...

Talking about ContentType(s) from image/x-png

This also caused the inability to upload png files...

A simple method to implement scheduled backup of MySQL database in Linux

Here are the detailed steps: 1. Check the disk sp...

11 common CSS tips and experience collection

1. How do I remove the blank space of a few pixels...

Implementation of MySQL Multi-version Concurrency Control MVCC

Table of contents What is MVCC MVCC Implementatio...

Detailed explanation of MySQL alter ignore syntax

When I was at work today, the business side asked...

A Brief Analysis of Subqueries and Advanced Applications in MySql Database

Subquery in MySql database: Subquery: nesting ano...

Mysql multi-condition query statement with And keyword

MySQL multi-condition query with AND keyword. In ...

A detailed introduction to Linux file permissions

The excellence of Linux lies in its multi-user, m...