Vue3 使用 vue-plugin-hiprint 实现无预览打印

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue3 使用 vue-plugin-hiprint 实现无预览打印</title>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/vue-plugin-hiprint.js"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 20px;
    }
    .container {
      max-width: 800px;
      margin: 0 auto;
    }
    button {
      padding: 10px 15px;
      background-color: #42b983;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      margin: 10px 0;
    }
    button:hover {
      background-color: #369f6b;
    }
    .print-content {
      border: 1px dashed #ccc;
      padding: 20px;
      margin: 20px 0;
    }
  </style>
</head>
<body>
  <div id="app">
    <div class="container">
      <h1>Vue3 使用 vue-plugin-hiprint 实现无预览打印</h1>
      
      <p>本示例演示如何使用 vue-plugin-hiprint 插件在 Vue3 中实现无预览直接打印功能。</p>
      
      <div class="print-content">
        <h2>打印内容区域</h2>
        <p>这是一个示例打印内容,您可以自定义任何需要打印的内容。</p>
       </p>
        <p>当前时间:{{ currentTime }}</p>
      </div>
      
      <button @click="directPrint">直接打印</button>
      <button @click="printWithOptions">带选项打印</button>
    </div>
  </div>

  <script>
    const { createApp, ref, onMounted } = Vue;
    
    createApp({
      setup() {
        const currentTime = ref(new Date().toLocaleString());
        
        // 初始化 hiprint
        let hiprintInstance = null;
        
        onMounted(() => {
          // 创建 hiprint 实例
          hiprintInstance = new hiprint.PrintTemplate({
            template: {},
            settingContainer: '#app', // 通常用于预览,这里我们不需要
          });
          
          // 更新当前时间每秒
          setInterval(() => {
            currentTime.value = new Date().toLocaleString();
          }, 1000);
        });
        
        // 直接打印
        const directPrint = () => {
          if (!hiprintInstance) return;
          
          // 获取要打印的 HTML 内容
          const printContent = document.querySelector('.print-content').innerHTML;
          
          // 直接打印
          hiprintInstance.print(printContent, {
            title: '我的打印文档',
            styleHandler: () => {
              // 可以在这里添加自定义样式
              return `
                body { font-family: Arial; }
                h2 { color: #333; }
                p { margin: 5px 0; }
              `;
            }
          });
          
          console.log('打印任务已发送');
        };
        
        // 带选项打印
        const printWithOptions = () => {
          if (!hiprintInstance) return;
          
          const printContent = document.querySelector('.print-content').innerHTML;
          
          // 打印选项
          const options = {
            title: '自定义打印文档',
            copies: 1, // 打印份数
            printer: '', // 打印机名称,空表示默认打印机
            pageSize: 'A4', // 纸张大小
            layout: 'portrait', // 方向: portrait(纵向), landscape(横向)
            margin: { // 边距
              top: '10mm',
              left: '10mm',
              right: '10mm',
              bottom: '10mm'
            },
            styleHandler: () => {
              return `
                body { font-size: 14px; }
                h2 { color: #42b983; border-bottom: 1px solid #eee; }
                a { color: #42b983; text-decoration: none; }
              `;
            }
          };
          
          hiprintInstance.print(printContent, options);
        };
        
        return {
          currentTime,
          directPrint,
          printWithOptions
        };
      }
    }).mount('#app');
  </script>
</body>
</html>

### 如何在 Vue3使用 `vue-plugin-hiprint` 实现 PDF 打印 #### 安装依赖库 为了能够在 Vue3 项目中使用 `vue-plugin-hiprint` 插件,需先通过 npm 或 yarn 来安装该插件。命令如下所示[^2]: ```bash npm install -S vue-plugin-hiprint ``` #### 配置插件 完成安装之后,在项目的入口文件(通常是 main.js 或者类似的初始化文件)引入并注册此插件: ```javascript import { createApp } from &#39;vue&#39; import App from &#39;./App.vue&#39; // 导入 HiPrint 插件 import VuePluginHiPrint from &#39;vue-plugin-hiprint&#39;; const app = createApp(App) app.use(VuePluginHiPrint, { // 可选配置项... }) app.mount(&#39;#app&#39;) ``` #### 使用组件进行打印操作 接下来可以在页面中的某个按钮点击事件或者其他触发条件下来调用打印功能。下面是一个简单的例子来展示如何定义一个用于启动打印过程的方法。 假设有一个名为 PrintComponent 的单文件组件 (`.vue`) 文件: ```html <template> <div class="print-container"> <!--打印的内容 --> <section id="content-to-print"> <h1>这是要被打印的内容</h1> <p>更多内容...</p> </section> <!-- 触发打印的按钮 --> <button @click="handlePrint">打印</button> </div> </template> <script setup> import { ref } from "vue"; import { usePrinter } from &#39;vue-plugin-hiprint&#39;; function handlePrint() { const printer = new usePrinter(); printer.print({ templateId: &#39;default&#39;, // 如果有预设模板则指定ID contentSelector: &#39;#content-to-print&#39;, type: &#39;pdf&#39; // 设置输出格式为 pdf }); } </script> <style scoped> /* 样式省略 */ </style> ``` 上述代码展示了怎样创建一个带有特定 ID 的 HTML 结构作为待打印区域,并设置了一个按钮用来触发展示打印对话框的功能。当用户点击这个按钮时会执行 `handlePrint()` 方法,进而调用了来自 `vue-plugin-hiprint` 提供的 API 函数来进行实际的打印工作[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值