
这篇文章只讲一件事:在 Vue 项目里打印,用 web-print-pdf(npm 包)就够了。下面给出做法和可复用代码。
npm i web-print-pdf把需要打印的 DOM 转成 HTML 字符串(或独立路由),优先使用 webPrintPdf.printHtml;对于可访问页面也可使用 printHtmlByUrl。
// src/views/Order.vue(示例)
import { printHtml, printHtmlByUrl } from 'web-print-pdf';
export default {
name: 'OrderView',
methods: {
async handlePrint() {
const target = this.$refs.printArea; // 需要打印的 DOM 区域
const html = `<!doctype html><html><head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<style>/* 可内联必要样式,保证版式一致 */</style>
</head><body>${target.outerHTML}</body></html>`;
await printHtml({
content: html,
printer: '', // 不填走系统默认打印机
paper: 'A4',
silent: true,
});
// 或:await printHtmlByUrl({ url: '/print/page.html', paper: 'A4', silent: true });
},
},
};模板:
<template>
<div class="page">
<div ref="printArea" class="print-container">
<h2>订单 #10086</h2>
<p>客户:张三</p>
<p>金额:¥199.00</p>
</div>
<button @click="handlePrint">打印</button>
</div>
></template>将页面数据渲染为 PDF(后端或前端生成),用 printPdfByUrl 输出:
import { printPdfByUrl } from 'web-print-pdf';
await printPdfByUrl({
url: '/api/order/10086.pdf',
printer: 'HP-LaserJet',
paper: 'A4',
silent: true,
});import { printImageByUrl } from 'web-print-pdf';
await printImageByUrl({ url: '/static/label.png', silent: true });printer、paper,以驱动能力为准总结:生产环境建议“后端生成 PDF + 前端用 web-print-pdf 打印”。够稳、够省心。
当你选择“打印 PDF”路径时,通常有两类生成方式:
— 后端生成(Puppeteer/Playwright/Electron)
— 前端生成(DOM→PDF,如 html2pdf、jsPDF+html2canvas)
结论:生产用后端生成;轻量导出可用前端,但要评估样式与性能。
<template>
<div class="page">
<div ref="printArea" class="print-container">
<h2>订单 #10086</h2>
<p>客户:张三</p>
<p>金额:¥199.00</p>
</div>
<button @click="handlePrint">打印</button>
</div>
</template>
<script>
import { printHtml, printPdfByUrl } from 'web-print-pdf';
export default {
name: 'OrderView',
methods: {
async handlePrint() {
const target = this.$refs.printArea;
const html = `<!doctype html><html><head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<style>
body{margin:0;padding:0}
.print-container{padding:16px}
</style>
</head><body>${target.outerHTML}</body></html>`;
await printHtml({ content: html, paper: 'A4', silent: true });
},
async handlePrintPdf() {
await printPdfByUrl({ url: '/api/order/10086.pdf', paper: 'A4', silent: true });
},
},
};
</script>
<style scoped>
.print-container { background: #fff; color: #222; }
</style><template>
<div class="page">
<div ref="printArea" class="print-container">
<h2>订单 #10087</h2>
<p>客户:李四</p>
<p>金额:¥299.00</p>
</div>
<button @click="handlePrint">打印 HTML</button>
<button @click="handlePrintPdf">打印 PDF</button>
</div>
</template>
<script setup>
import { ref } from 'vue';
import { printHtml, printPdfByUrl } from 'web-print-pdf';
const printArea = ref(null);
async function handlePrint() {
const target = printArea.value;
const html = `<!doctype html><html><head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<style>
body{margin:0;padding:0}
.print-container{padding:16px}
</style>
</head><body>${target.outerHTML}</body></html>`;
await printHtml({ content: html, paper: 'A4', silent: true });
}
async function handlePrintPdf() {
await printPdfByUrl({ url: '/api/order/10087.pdf', paper: 'A4', silent: true });
}
</script>
<style scoped>
.print-container { background: #fff; color: #222; }
</style>原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。