亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Print.Js網(wǎng)頁打印標簽詳細代碼示例

 更新時間:2025年06月28日 08:54:13   作者:許多寶滴IT  
Print.js一個小的javascript庫,可幫助您從網(wǎng)絡(luò)上打印,這篇文章主要介紹了Print.Js網(wǎng)頁打印標簽的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

1、功能背景需求

后臺讀取數(shù)據(jù),預覽頁面再打印出小標簽,涉及打印機設(shè)置紙張大小,與代碼編寫兼容等等

2、打印機設(shè)置紙張大?。?/h2>

按照下圖順序設(shè)置需要打印的紙張大小,例如50mm*50mm

不知道,問產(chǎn)品打印的紙張大小是多少,再去設(shè)置!

3、相關(guān)代碼(PrintJS)

代碼頁面要準備一個預覽頁面,PrintJS打印時要綁定ID去打印。

3.1預覽打印效果圖:

3.2相關(guān)代碼:

<template>
  <!-- 網(wǎng)頁打印小票預覽頁 -->
  <div class="tick">
    <div class="tick-bg">
      <div id="print-tick">
        <div class="tick-bg-content" v-for="(item, index) in 1" :key="index">
          <div class="header">
            <b class="header-title">酸菜豬肉燉粉條長名就換行顯示..</b>
            <div class="header-price">
              <span>¥10.00</span>
              <span class="header-price-num">1份</span>
            </div>
          </div>
          <div class="body-address">住院部6F新生兒內(nèi)科②[番禺院區(qū)1]</div>
          <div class="footer">
            <div class="footer-flex">
              <span class="userName">張三</span>
              <span>1802****704</span>
            </div>
            <div class="footer-page">{{ index+1 }}/2</div>
          </div>
        </div>
      </div>

      <el-button class="w-80 ps-origin-btn" size="mini" @click="convertToImageAndPrint">打印</el-button>
    </div>
  </div>
</template>

<script>
import print from 'print-js'

export default {
  created() {
    this.type = this.$route.query.type
    this.printList = JSON.parse(localStorage.printList)
    console.log('this.printList', this.printList)
  },
  data() {
    return {
      type: 'takeaway',
      printList: []
    }
  },
  methods: {
    // 確認打印
    convertToImageAndPrint() {
      print({
        printable: 'print-tick',
        type: 'html',
        scanStyles: false,
        // style: '@media print { body { margin: 0; padding: 0;}  .print-tick { margin: 0 auto; width: 100%;} }',
        css: 'data:text/css,' + encodeURIComponent(this.rechargeStyle())
      })
    },
    // 打印樣式
    rechargeStyle() {
      return `
        @page {
          width: 100%;
          margin: 0 !important;
          padding: 0 !important;
          box-sizing: border-box !important;
        }
        *, body {
          width: 100% !important;
          margin: 0 !important;
          padding: 0 !important;
          box-sizing: border-box !important;
          font-size: 16px !important;
        }
        #print-tick .tick-bg-content{
          padding: 0 18px !important;
        }
        #print-tick .header {
          width: 100% !important;
          padding-bottom: 5px !important;
          margin-bottom: 5px !important;
          border-bottom: 2px dashed #000 !important;
        }
        #print-tick .header .header-title{
          font-weight: bold !important;
          font-size: 18px !important;
          display: -webkit-box !important;
          display: -moz-box !important;
          overflow: hidden !important;
          line-clamp: 2 !important;
          -webkit-box-orient: vertical !important;
          -webkit-line-clamp: 2 !important;
          -moz-box-orient: vertical !important;
          -moz-line-clamp: 2 !important;
        }
        #print-tick .header .header-price{
          display: flex !important;
        }
        #print-tick .header-price .header-price-num{
          display: inline-block;
          padding-left: 10px;
        }
        #print-tick .body-address{
          width: 100% !important;
          margin-bottom: 5px !important;
          display: -webkit-box !important;
          display: -moz-box !important;
          overflow: hidden !important;
          line-clamp: 2 !important;
          -webkit-box-orient: vertical !important;
          -webkit-line-clamp: 2 !important;
          -moz-box-orient: vertical !important;
          -moz-line-clamp: 2 !important;
        }
        #print-tick .footer{
          padding-top: 5px !important;
          border-top: 2px dashed #000 !important;
        }
        #print-tick .footer .footer-flex{
          width: 100% !important;
          display: flex !important;
          justify-content: space-between !important;
          align-items: center !important;
        }
        #print-tick .footer .footer-page{
          display: flex !important;
          justify-content: end !important;
          padding-top: 10px !important;
        }

        @-moz-document url-prefix() {
          *, body {
            letter-spacing: -1px !important;
            width: 100% !important;
            margin: 0 !important;
            padding: 0 !important;
            box-sizing: border-box !important;
            font-size: 16px !important;
          }
          #print-tick .tick-bg-content {
            padding:0 0 0 30px !important;
          }
          #print-tick .header .header-price{
            display: flex !important;
            margin-top: 10px !important;
          }
        }
      `
    }
  }
}
</script>

<style lang="scss" scoped>
.tick {
  &-bg {
    background-color: grey;
    min-height: 100vh;
    width: 100%;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    #print-tick {
      font-size: 12px;
      line-height: 16px;
      font-weight: 500;
      .tick-bg-content {
        padding: 20px;
        margin: 0 auto;
        width: 290px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        background-color: #fff;
        margin-bottom: 20px;
        .header {
          width: 100%;
          padding-bottom: 10px;
          margin-bottom: 10px;
          border-bottom: 2px dashed #dddddd;
          .header-title {
            display: -webkit-box !important;
            display: -moz-box !important;
            overflow: hidden !important;
            line-clamp: 2 !important;
            -webkit-box-orient: vertical !important;
            -webkit-line-clamp: 2 !important;
            -moz-box-orient: vertical !important;
            -moz-line-clamp: 2 !important;
          }
          .header-price {
            // margin-top: 10px;
            // transform: translate(100%, 0);
            // width: 50%;
            // display: flex;
            // justify-content: space-between;
            // align-items: center;
          .header-price-num{
              margin-top: 10px;
              display: inline-block;
              padding-left: 50px;
            }
          }
        }
        .body-address {
          width: 100%;
          margin-bottom: 10px;
          display: -webkit-box !important;
          -webkit-box-orient: vertical !important;
          -webkit-line-clamp: 2 !important;
          line-clamp: 2 !important;
          overflow: hidden !important;
        }
        .footer {
          width: 100%;
          border-top: 2px dashed #ddd;
          padding-top: 10px !important;
          .footer-flex {
            display: flex;
            justify-content: space-between;
            align-items: center;
          }
          .footer-page {
            padding-top: 10px;
            display: flex;
            justify-content: end;
            align-items: center;
          }
        }
      }
    }
  }
}
</style>

以上關(guān)鍵點在于print()打印的方法,可能要注意的是打印時的樣式調(diào)整,每個瀏覽器要處理下樣式兼容問題,火狐比較特殊點,也做了打印時的樣式特殊處理如下:

@-moz-document url-prefix() {
          *, body {
            letter-spacing: -1px !important;
            width: 100% !important;
            margin: 0 !important;
            padding: 0 !important;
            box-sizing: border-box !important;
            font-size: 16px !important;
          }
          #print-tick .tick-bg-content {
            padding:0 0 0 30px !important;
          }
          #print-tick .header .header-price{
            display: flex !important;
            margin-top: 10px !important;
          }
        }

3.3點擊打印時,會彈出:

4、總結(jié):

網(wǎng)頁打印其實就是設(shè)置好打印設(shè)備,調(diào)試好機器,代碼方面寫一個預覽頁面,用插件PrintJs就好,其他有問題再慢慢調(diào)試就行。

到此這篇關(guān)于Print.Js網(wǎng)頁打印標簽的文章就介紹到這了,更多相關(guān)PrintJs網(wǎng)頁打印標簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談webpack打包過程中因為圖片的路徑導致的問題

    淺談webpack打包過程中因為圖片的路徑導致的問題

    下面小編就為大家分享一篇淺談webpack打包過程中因為圖片的路徑導致的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • JavaScript中防抖和節(jié)流的原理和區(qū)別詳解

    JavaScript中防抖和節(jié)流的原理和區(qū)別詳解

    JavaScript 中,防抖和節(jié)流是一種用于優(yōu)化事件處理函數(shù)調(diào)用頻率的技術(shù),防抖和節(jié)流的目的都是為了避免頻繁地觸發(fā)事件處理函數(shù),從而減少瀏覽器和服務(wù)器的負擔,本文將給大家介紹一下JavaScript中防抖和節(jié)流的原理和區(qū)別,需要的朋友可以參考下
    2023-09-09
  • Bootstrap網(wǎng)格系統(tǒng)詳解

    Bootstrap網(wǎng)格系統(tǒng)詳解

    bootstrap框架中的網(wǎng)格系統(tǒng)就是將容器平分成12份,在使用的時候可以根據(jù)實際情況重新編譯LESS/SASS源碼來修改12這個數(shù)值。接下來通過本文給大家介紹Bootstrap網(wǎng)格系統(tǒng),感興趣的朋友一起學習
    2016-04-04
  • Js中Symbol的靜態(tài)屬性及用途詳解

    Js中Symbol的靜態(tài)屬性及用途詳解

    JavaScript 語言在 ES6 規(guī)范中引入了 Symbol 類型,它是一種原始數(shù)據(jù)類型,用于創(chuàng)建唯一的標識符,本文將介紹 Symbol 類型的所有靜態(tài)屬性,并舉例說明它們的用途和使用場景,希望對大家有所幫助
    2023-12-12
  • 微信小程序開發(fā)實現(xiàn)消息推送

    微信小程序開發(fā)實現(xiàn)消息推送

    這篇文章主要為大家詳細介紹了微信小程序開發(fā)實現(xiàn)消息推送,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • js常用節(jié)點操作實例總結(jié)

    js常用節(jié)點操作實例總結(jié)

    這篇文章主要介紹了js常用節(jié)點操作,結(jié)合實例形式總結(jié)分析了JavaScript針對dom節(jié)點的遍歷、查找、創(chuàng)建、刪除、克隆等相關(guān)實現(xiàn)技巧與注意事項,需要的朋友可以參考下
    2023-04-04
  • 使用pjax實現(xiàn)無刷新更改頁面url

    使用pjax實現(xiàn)無刷新更改頁面url

    pjax=pushState+ajax,相信用過github的同學都知道,github部分頁面采用了pjax這個項目來實現(xiàn)ajax無刷新加載的同時改變頁面url。一起來學習一下這個插件吧。
    2015-02-02
  • jquery獲取img的src值的簡單實例

    jquery獲取img的src值的簡單實例

    下面小編就為大家?guī)硪黄猨query獲取img的src值的簡單實例。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • 讓DIV的滾動條自動滾動到最底部的3種方法(推薦)

    讓DIV的滾動條自動滾動到最底部的3種方法(推薦)

    下面小編就為大家?guī)硪黄孌IV的滾動條自動滾動到最底部的3種方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Electron實現(xiàn)應(yīng)用打包、自動升級過程解析

    Electron實現(xiàn)應(yīng)用打包、自動升級過程解析

    這篇文章主要介紹了Electron實現(xiàn)應(yīng)用打包、自動升級過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07

最新評論