SoFunction
Updated on 2025-04-03

vue3 uses vue3-print-nb to implement the area printing function

Scene

Most background systems have the need for printing. When there is a printing requirement, it is of course easier for the front-end to print the page directly. So this article is in vue3, using the vue3-print-nb plug-in to print the area to achieve wherever you point!

1. Install vue3-print-nb

npm install vue3-print-nb

2. Introduced in

//Introduceimport print from 'vue3-print-nb'
//mountconst app = createApp(App)
(print)

Three.HTML

<!-- Print area container -->
<div >
<span>I'm the printed content</span>
&lt;span&gt;exist#printBox container will be printed</span>&lt;/div&gt;
&lt;!-- Button binding print --&gt;
&lt;button v-print="print"&gt;Click to open the print preview&lt;/button&gt;

4. Parameter configuration

// Here is the printed configuration itemconst  print=ref({
        id: 'printBox',//The id here is the id of our printing area above, which is to click wherever we refer to        popTitle: 'Configure header title', // Print the title above the configuration page        extraHead: '', // The top head text, additional tags attached to the head tag, split using commas        preview: false, // Whether to start preview mode, default is false        previewTitle: 'Preview title', // Print the preview title        previewPrintBtnLabel: 'The preview ends,Start printing', // Print the text of the button below the preview title, click to enter the print        zIndex: 20002, // The z-index of the preview window is 20002 by default, preferably higher than the default value        previewBeforeOpenCallback() { ('The preview window is loading!  '); }, // The preview window opens before the callback        previewOpenCallback() { ('The preview window has been loaded, and the preview is open!  ') }, // Callback when the preview window opens        beforeOpenCallback() { ('Before printing!  ') }, // Start printing the callback        openCallback() { ('Execute printing!  ') }, // Callback when printing        closeCallback() { ('Close the printing tool!  ') }, // Close the printed callback (cannot distinguish between confirmation or cancellation)        clickMounted() { ('Click the v-print bound button!  ') },
      })

Attachment: Paging printing sample code

&lt;template&gt;
    &lt;div&gt;
        &lt;button v-print="'#a'">Print</button>        &lt;div &gt;
             // Method 1             // Use div to wrap the blocks that need to be paging using css attribute page-break-after: always for paging            &lt;div style="page-break-after:always"&gt;Page 1&lt;/div&gt;
            &lt;div style="page-break-after:always"&gt;Page 2&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/template&gt;
&lt;style&gt;
     // Method 2     // Use media query Set the height of body and html to auto when printing     @media print {
        @page {
          size:  auto;
        }
        body, html {   //If vue's outermost id is, the default is #app. If height:100%; is set, then #app will also add height: auto !important;        }
      }
&lt;/style&gt;

Summarize

This is the article about vue3 using vue3-print-nb to implement regional printing function. For more related content related to vue3 to implement regional printing, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!