Detailed explanation of html printing related operations and implementation

Detailed explanation of html printing related operations and implementation

The principle is to call the window.print() method, but this method can only print the entire current page, so the following solution is used to solve partial printing

1: Use iframe to inject the elements and styles that need to be printed and then call print

// Example code function print () {
    let ifElement = document.getElementById('ifId')
    const addHtmlPrint = () => {
        const content = ifElement.contentWindow || ifElement.contentDocument
        content.document.body.innerHTML = this.detailTable
        const styleEle = document.createElement('style')
        /* Remove the header and footer when printing*/
        styleEle.innerHTML = '@media print {@page { margin: 5mm; }}'
        content.document.getElementsByTagName('head')[0].appendChild(styleEle)

        /* Ensure that the resources in the iframe are loaded, and the image should be imported in the form of img*/
        ifElement.onload = () => {
            content.print()
        }
    }
    this.getDetailTable()

    if (ifElement) {
        // If it has been created, print it directly addHtmlPrint()
    } else {
        ifElement = document.createElement('iframe')
        ifElement.setAttribute('id', 'ifId')
        ifElement.setAttribute('style', 'display:none')
        document.body.appendChild(ifElement)

        addHtmlPrint()
    }
}

2: Use @media print to set the elements that need to be hidden when printing on the current page

@media print{
    /* Here, set the elements that do not need to be printed to not be displayed*/
    .hidden-element{
        display:none;
        /* visibility:hidden; */
    }
    /*The paper is set to 1200px wide and 800px high*/
    @page{
        size:1200px 800px;
    }
}
  • <link href="/example.css" media="print" rel="stylesheet" /> Marks the style that will be used when printing
  • Listen for print events window.addEventListener('beforeprint|| afterprint', ()=> {});

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Detailed analysis of the chmod command to modify file permissions under Linux

>>:  Introduction to fourteen cases of SQL database

Recommend

Detailed explanation of Vue's keyboard events

Table of contents Common key aliases Key without ...

Analysis of Nginx Rewrite usage scenarios and configuration methods

Nginx Rewrite usage scenarios 1. URL address jump...

SystemC environment configuration method under Linux system

The following is the configuration method under c...

Example code for implementing card waterfall layout with css3 column

This article introduces the sample code of CSS3 c...

The most convenient way to build a Zookeeper server in history (recommended)

What is ZooKeeper ZooKeeper is a top-level projec...

Implementation of Vue single file component

I recently read about vue. I found a single-file ...

How to create a view in MySQL

Basic syntax You can create a view using the CREA...

HTML 5.1 learning: 14 new features and application examples

Preface As we all know, HTML5 belongs to the Worl...

Vue's guide to pitfalls using throttling functions

Preface In a common business scenario, we need to...

Summary of methods for cleaning Mysql general_log

Method 1: SET GLOBAL general_log = 'OFF';...

HTML table markup tutorial (6): dark border color attribute BORDERCOLORDARK

In a table, you can define the color of the lower...

Beginners understand MySQL deadlock problem from source code

After many difficult single-step debugging late a...

How to import Chinese data into csv in Navicat for SQLite

This article shares with you the specific method ...