vue3实现打印功能

安装依赖包

npm i print-js

图片 /assets/print.png

template代码

注意:打印模板的css样式需要写在标签上

    <div class="report-box" ref="printRef">
        <div class="page-item" style="break-after: page;">
            <h1 class="h1-class"
                style="text-align: center; margin-top: 140px; font-size: 40px; font-weight: bold;margin-bottom: 80px;">
                项目系统运行报告</h1>
            <table class="message"
                style="font-size: 14px; line-height: 1.43; color: rgb(0, 0, 0); margin: 20% auto 0px; width: 76%; padding: 10px; border: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238); border-spacing: 0px; border-collapse: collapse;">
                <tbody>
                    <tr v-for="item in 3" :key="item">
                        <td class="td-label"
                            style="width: 150px; padding: 10px; border: 1px solid rgb(204, 204, 204); display: table-cell;">
                            第{{ item }}行标题: </td>
                        <td class="td-value"
                            style="padding: 10px; border: 1px solid rgb(204, 204, 204); display: table-cell;">
                            第{{ item }}行内容
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

        <div class="page-item" style="break-after: page;">
            <h3 style="font-size: 24px; font-weight: 500;margin-bottom: 20px;">
                ⼀、项⽬设备汇总
            </h3>
            <table class="info" style="width: 100%; border-collapse: collapse; margin: 0px auto; text-align: center;">
                <thead>
                    <tr style="background-color: rgb(248, 203, 173); height: 30px;">
                        <th v-for="item in 3" :key="item"
                            style="border: 1px solid rgb(221, 221, 221); color: rgb(102, 102, 102); height: 38px; margin: 0px auto; text-align: center;">
                            第{{ item }}列表格头
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in 2" :key="item">
                        <td class="td-value"
                            style="padding: 10px;border: 1px solid rgb(204, 204, 204);display: table-cell;">
                            {{ item }}</td>
                        <td class="td-value"
                            style="padding: 10px;border: 1px solid rgb(204, 204, 204);display: table-cell;">
                            {{ item }}</td>
                        <td class="td-value"
                            style="padding: 10px;border: 1px solid rgb(204, 204, 204);display: table-cell;">
                            {{ item }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="btn-print">
        <button type="button" id="print" class="CustomPrint" @click="onPrintJs">打印</button>
    </div>

 script代码

// 定义变量内容
const printRef = ref();

// 打印点击
const onPrintJs = () => {
    printJs({
        printable: printRef.value,
        type: 'html',
        css: ['//at.alicdn.com/t/c/font_2298093_rnp72ifj3ba.css', '//unpkg.com/element-plus/dist/index.css'],
        scanStyles: false,
        style: `@media print{.mb15{margin-bottom:15px;}.el-button--small i.iconfont{font-size: 12px !important;margin-right: 5px;}}`,
    });
};

css代码

.report-box {
    width: 680px;
    margin: 0 auto;

    .page-item {
        background: #fff;
        padding: 30px;
        margin-bottom: 10px;
    }
}

.btn-print {
    position: fixed;
    right: 180px;
    top: 200px;
    display: flex;
    flex-direction: column;

    .CustomPrint {
        cursor: pointer;
        background-color: rgba(29, 205, 188, 0.1);
        width: 95px;
        height: 68px;
        padding: 0;
        border-radius: 5px;
        border: 0;
        color: #FFF;
        background-image: url("/@/assets/print.png");
        background-size: 40px 30px;
        background-position: 27px 10px;
        background-repeat: no-repeat;
        padding-top: 34px;

        &:hover {
            background-color: #1e5761;
        }
    }
}

打印效果图

页面效果图 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值