js實現(xiàn)調用瀏覽器自帶打印工單功能

本例子基于vue實現(xiàn)默穴,但邏輯是通用的三热。
HTML部分 :邏輯使用table,為保證視覺統(tǒng)一念祭,每行分24列睛藻,通過colspan合并列
JS部分

<div style="max-height:100%;width:100%;overflow:auto;" :id="printId">
  <table class="font-s10" border="1" cellpadding="0" cellspacing="0">
        <tr class="font-s20 font-b align-center">
          <td colspan="24" class="color-bg_lightyellow color-black">
            項目審批表
          </td>
        </tr>

        <tr>
          <td colspan="1" class="td-t font-s8">序號</td>
          <td colspan="9" class="td-t">流程名稱</td>
          <td colspan="2" class="td-t">流程時間</td>
          <td colspan="2" class="td-t">流程金額</td>
          <td colspan="10" class="td-t font-s6">描述</td>
        </tr>
         <tr v-for="(item, idx) in  detail.processArr || []" :key="idx">
          <td colspan="1">{{ idx + 1 }}</td>
          <td colspan="9" class="font-s7">{{ item.xx}}</td>
          <td colspan="2" class="font-s7">{{ (item.xx || "").substr(0, 10) }}</td>
          <td colspan="2" class="color-red font-s8"> {{ item.xx2  }}元</td>
          <td colspan="12" class="color-red font-s8"> {{ item.xx3  }}</td>
        </tr>
        <!--空行-->
         <tr><td colspan="24" class="space0"></td> </tr>
        <tr>
          <td class="td-t mode-rl align-center font-s14" rowspan="3" colspan="2" >
            簽批流
          </td>
          <td colspan="4" class="td-t lineheight_18">采購經辦負責人</td>
          <td colspan="7"></td>
          <td colspan="4" class="td-t">發(fā)起審批時間</td>
          <td colspan="7"></td>
        </tr>
        <tr>
          <td colspan="4" class="td-t lineheight_18">項目負責人</td>
          <td colspan="7"></td>
          <td colspan="4" class="td-t">運營</td>
          <td colspan="7"></td>
        </tr>
        <tr>
          <td colspan="4" class="td-t lineheight_18">事業(yè)部負責人</td>
          <td colspan="7"></td>
          <td colspan="4" class="td-t">總經理</td>
          <td colspan="7"></td>
        </tr>
    </table>
</div>

css部分用同一套統(tǒng)一樣式启上,如果是scss,自己在線轉一下就得了,這段轉的css,在下面js打印中會用到的

.printTable {
  .pre {
    white-space: pre-line;
  }
  .lineheight {
    &_12 {
      height: 12px;
    }
    &_18 {
      height: 18px;
    }
    &_24 {
      height: 24px;
    }
    &_36 {
      height: 36px;
    }
    &_48 {
      height: 48px;
    }
  }
  .font {
    &-s6 {
      font-size: 6px;
      line-height: 8px;
    }
    &-s7 {
      font-size: 7px;
      line-height: 9px;
    }
    &-s8 {
      font-size: 8px;
      line-height: 10px;
    }
    &-s9 {
      font-size: 9px;
      line-height: 12px;
    }
    &-s10 {
      font-size: 10px;
      line-height: 12px;
    }
    &-s14 {
      font-size: 14px;
      line-height: 16px;
    }
    &-s20 {
      font-size: 20px;
      line-height: 28px;
    }
    &-b {
      font-weight: 600;
    }
    &-bb {
      font-weight: 900;
    }
    &-nowrap {
      white-space: nowrap;
    }
  }

  .color {
    &-bg {
      &_yellow {
        background: #ffc000;
      }
      &_lightyellow {
        background: #ffff00;
      }
      &_grey {
        background: #e4e4e4;
      }
      &_pink {
        background: #fce4d6;
      }
      &_green {
        color: #60a265;
      }
      &_black {
        color: #000;
      }
    }
    &-red {
      color: #f00;
    }
    &-grey {
      color: grey;
    }
    &-green {
      color: #60a265;
    }
    &-pink {
      color: #fce4d6;
    }
    &-lightyellow {
      color: #ffff00;
    }
    &-yellow {
      color: #ffc000;
    }
  }

  .align-center {
    text-align: center;
  }

  table {
    table-layout: fixed;
    font-size: 12px;
    line-height: 30px;
    width: 100%;
    border: 0px solid #dddddd;
    border-collapse: collapse;
    color: #444;
  }

  table tr {
    border-bottom: 1px solid #d1d1d1;
  }
  table tr:first-child {
    border-top: 1px solid #d1d1d1;
  }
  table tr > td {
    border-right: 1px solid #d1d1d1;
  }
  table tr > td:first-child {
    border-left: 1px solid #d1d1d1;
  }

  table th,
  table td {
    padding: 0 5px;
    white-space: wrap;
    word-wrap: break-word;
  }

  table .td-t {
    background: #e4e4e4;
    color: #666;
  }

  table .td-c {
    color: #444;
  }

  td.space {
    height: 10px;
    line-height: 10px;
    background: #f5f3f3;
  }

  td.space0 {
    height: 5px;
    line-height: 5px;
    background: #f5f3f3;
  }

  table tr.strip:nth-child(2n + 1) {
    background: #eff6ff;
  }

  table tr.strip:nth-child(2n) {
    background: #ffffff;
  }

  table .mode-rl {
    writing-mode: vertical-rl;
    vertical-align: middle;
    text-align: center;
    letter-spacing: 5px;
  }
}

JS部分

/*
* 處理工單審批流的數(shù)據
*/
async print(id) {
      this.printId = "flow_" + id;
      this.show = false;
      
      setTimeout(async () => {
        await this.handlePrintPage(this.printId); //這個id最好有意義生成店印,為防止重復調用獲取到非本次數(shù)據
      }, 500);
},
 /**
  * @param id 
   * @param isPortrait 是否垂直打印
   * @param isAddSinglePageStyle  是否添加單頁始終插入分頁符
   * css參數(shù)不明白的自己去查去 `page-break-after`
   */
handlePrintPage(id, isPortrait = true, isAddSinglePageStyle = true) {
    // 這段就是上面那個css啦冈在,真的長
    var printStyles = `
    .single{page-break-after:${ isAddSinglePageStyle ? "always" : "auto" };margin-top:0cm;}
    .single .single-content{page-break-after:${ isAddSinglePageStyle ? "always" : "auto"};margin-top:1cm;}
    .pre{white-space:pre-line}
    .lineheight_12{height:12px}
    .lineheight_18{height:18px}
    .lineheight_24{height:24px}
    .lineheight_36{height:36px}
    .lineheight_48{height:48px}
    .font-s6{font-size:6px;line-height:8px}
    .font-s7{font-size:7px;line-height:9px}
    .font-s8{font-size:8px;line-height:10px}
    .font-s9{font-size:9px;line-height:12px}
    .font-s10{font-size:10px;line-height:12px}
    .font-s14{font-size:14px;line-height:16px}
    .font-s20{font-size:20px;line-height:28px}
    .font-b{font-weight:600}
    .font-bb{font-weight:900}
    .font-nowrap{white-space:nowrap}
    .color-bg_yellow{background:#ffc000}
    .color-bg_lightyellow{background:#ffff00}
    .color-bg_grey{background:#e4e4e4}
    .color-bg_green{background:#60A265}
    .color-bg_pink{background:#fce4d6}
    .color-red{color:#f00}
    .color-grey{color:grey}
    .color-pink{color:#fce4d6}
    .color-lightyellow{color:#ffff00}
    .color-yellow{color:#ffc000}
    .color-green{color:#60A265}
    .color-black{color:#000}
    .align-center{text-align:center}
    table{table-layout:fixed;font-size:12px;line-height:30px;width:100%;border:0px solid #d1d1d1;border-collapse:collapse;color:#444}
    table th,table td{padding:0 5px;white-space:wrap;word-wrap:break-word;}
    table tr { border-bottom: 1px solid #d1d1d1;}
    table tr:first-child {border-top: 1px solid #d1d1d1;}
    table tr > td {border-right: 1px solid #d1d1d1;}
    table tr > td:first-child {border-left: 1px solid #d1d1d1;}
    table .td-t{background:#e4e4e4;color:#666}
    table .td-c{color:#444}
    td.space{height:10px;line-height:10px;background:#f5f3f3}
    td.space0{height:5px;line-height:5px;background:#f5f3f3}
    table tr.strip:nth-child(2n+1){background:#eff6ff}
    table tr.strip:nth-child(2n){background:#ffffff}
    table .mode-rl{writing-mode:vertical-rl;vertical-align:middle;text-align:center;letter-spacing:5px}
`;
      // 控制橫向打印還是縱向打印(但總是不生效),控制上下左右邊距
      var content = `
      <style type="text/css" media="print">
        @page {
          size:${isPortrait ? "portrait" : "landscape"}
           @bottom-center {
            content: "Page " counter(page) " of " counter(pages);
           }
        }
        body{ ${isPortrait ? "margin-left:1cm;" : "margin-top:0cm;"} }
        *{box-sizing:border-box;-webkit-print-color-adjust:exact;print-color-adjust:exact;color-adjust:exact;}
        ${printStyles}
     </style>`;
      content += document.getElementById(id).innerHTML;

      const printWindow = window.open("·", "_blank");
      printWindow.document.write(`
            <!DOCTYPE html>
            <html lang="en">
              <head>
                <meta charset="UTF-8" />
                <meta name="viewport" content="width=device-width, initial-scale=1.0" />
                <title></title>
              </head>
              <body>
                ${content}
              </body>
            </html>
      `);
      printWindow.document.close();
      printWindow.focus();
      printWindow.print();
      printWindow.close();
      // ending
    }
  }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末按摘,一起剝皮案震驚了整個濱河市包券,隨后出現(xiàn)的幾起案子纫谅,更是在濱河造成了極大的恐慌,老刑警劉巖溅固,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件付秕,死亡現(xiàn)場離奇詭異,居然都是意外死亡侍郭,警方通過查閱死者的電腦和手機询吴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來亮元,“玉大人猛计,你說我怎么就攤上這事∑凰冢” “怎么了有滑?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長嵌削。 經常有香客問我毛好,道長,這世上最難降的妖魔是什么苛秕? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任肌访,我火速辦了婚禮,結果婚禮上艇劫,老公的妹妹穿的比我還像新娘吼驶。我一直安慰自己,他們只是感情好店煞,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布蟹演。 她就那樣靜靜地躺著,像睡著了一般顷蟀。 火紅的嫁衣襯著肌膚如雪酒请。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天鸣个,我揣著相機與錄音羞反,去河邊找鬼。 笑死囤萤,一個胖子當著我的面吹牛昼窗,可吹牛的內容都是我干的。 我是一名探鬼主播涛舍,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼澄惊,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起缤削,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤窘哈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后亭敢,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡图筹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年帅刀,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片远剩。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡扣溺,死狀恐怖,靈堂內的尸體忽然破棺而出瓜晤,到底是詐尸還是另有隱情锥余,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布痢掠,位于F島的核電站驱犹,受9級特大地震影響,放射性物質發(fā)生泄漏足画。R本人自食惡果不足惜雄驹,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望淹辞。 院中可真熱鬧医舆,春花似錦、人聲如沸象缀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽央星。三九已至霞怀,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間等曼,已是汗流浹背里烦。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留禁谦,地道東北人胁黑。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像州泊,于是被迫代替她去往敵國和親丧蘸。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內容