萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 打印功能完整實現:打印預覽、頁面設置、直接打印、關閉窗口

核心技術:WebBrowser 控件 (IE 專用) + window.print()

注意:WebBrowser (CLSID:8856F961-340A-11D0-A96B-00C04FD705A2) **僅限 IE/Edge IE 模式**。現代瀏覽器請使用標準 window.print() + CSS @media print。

完整 IE 專用實現 (歷史參考)

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>打印功能 Demo (IE Only)</title>
  <style>
    /* 打印時隱藏的元素 */
    @media print {
      .noprint { display: none !important; }
    }
    .print-btns { margin: 20px; padding: 10px; }
  </style>
  <script>
  // WebBrowser 控件方法封裝
  var wb = null; // 將在 body onload 後賦值

  function initWB() {
    wb = document.getElementById('wb');
  }

  // 1. 打印頁面設置
  function printSetup() {
    if (wb) wb.execWB(8, 1); // OLECMDID_PAGESETUP = 8
  }

  // 2. 打印預覽
  function printPreview() {
    if (wb) wb.execWB(7, 1); // OLECMDID_PRINTPREVIEW = 7
  }

  // 3. 直接打印
  function printDirect() {
    if (confirm('確定打印嗎?')) {
      if (wb) wb.execWB(6, 1); // OLECMDID_PRINT = 6
      else window.print(); // 現代瀏覽器降級
    }
  }

  // 4. 關閉窗口
  function closeWindow() {
    window.close();
  }
  </script>
</head>
<body onload="initWB()">
  <!-- WebBrowser ActiveX 控件 (隱藏) -->
  <object 
    id="wb" 
    classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" 
    width="0" height="0">
  </object>

  <div class="print-btns noprint">
    <button onclick="printDirect()">打印</button>
    <button onclick="printPreview()">打印預覽</button>
    <button onclick="printSetup()">頁面設置</button>
    <button onclick="closeWindow()">關閉窗口</button>
  </div>

  <div class="print-content">
    <h1>中國綠色廚房計劃</h1>
    <p>體察民情 — 到最偏遠最貧窮的地方雪中送炭</p>
    <p>下黨鄉,壽寧縣最邊遠的山鄉之一...</p>
    <!-- 更多打印內容 -->
  </div>
</body>
</html>

現代跨瀏覽器標準方案 (推薦)

1. CSS 控制打印樣式

<style>
  @media print {
    /* 隱藏不需打印元素 */
    .noprint, nav, .sidebar, .btn, footer { display: none !important; }
    
    /* 重置佈局 */
    .container { width: 100%; max-width: none; padding: 0; margin: 0; }
    body { font-size: 12pt; line-height: 1.5; color: #000; background: #fff; }
    
    /* 分頁控制 */
    .page-break { page-break-before: always; }
    .avoid-break { page-break-inside: avoid; }
    
    /* 表格表頭重複 */
    thead { display: table-header-group; }
    tr { page-break-inside: avoid; }
  }
</style>

2. 統一打印入口

<button class="noprint" onclick="window.print()">打印/預覽</button>
<!-- 
  現代瀏覽器 window.print() 會彈出系統打印對話框,
  包含「預覽」、「打印機選擇」、「頁面設置」等所有功能,
  無需 ActiveX 控件。
-->

3. 進階:生成純打印頁面 (新窗口/iframe)

function printReport(data) {
  // 1. 構建打印專用 HTML
  var html = `
    
    
    
      
      報表打印
      
    
    
      

${data.title}

${data.items.map(i => ``).join('')}
項目金額
${i.name}${i.amount}
`; // 2. 新窗口打印 (避免汙染主頁) var win = window.open('', '_blank', 'width=800,height=600'); win.document.write(html); win.document.close(); // 3. 若需靜默打印 (無彈窗),需瀏覽器支持 kiosk 模式或打印服務 }

WebBrowser execWB 命令 ID 速查 (IE 專用)

命令ID說明
OLECMDID_OPEN1打開文件
OLECMDID_SAVE4保存
OLECMDID_PRINT6直接打印
OLECMDID_PRINTPREVIEW7打印預覽
OLECMDID_PAGESETUP8頁面設置
OLECMDID_PROPERTIES10屬性
OLECMDID_SELECTALL17全選
OLECMDID_REFRESH22刷新
OLECMDID_CLOSE45關閉窗口無提示

遷移建議

  1. 新項目**直接使用 window.print() + @media print CSS**
  2. 舊 IE 專項目:維持現狀,或引導用戶升級瀏覽器
  3. 複雜報表:考慮後端生成 PDF (puppeteer, wkhtmltopdf, jspdf) 前端下載打印
  4. 需要靜默打印:配置瀏覽器 kiosk 模式、打印服務器、或 Electron/TAURI 桌面應用
copyright © 萬盛學電腦網 all rights reserved