vue3+ts+html2canvas实现html下载成图片

<template>
    <div class="reporting">
        <div class="btns">
            <div class="reporting-btn" @click="goDown">下载报告</div>
        </div>
        <div class="content-box">
            <div class="content-box2" ref="canvasRef">
                <el-table :data="[{name:'zhangsan',age:18,female:'男'},{name:'zhangsan',age:18,female:'女'}]">
                    <el-table-column prop="name" label="姓名"/>
                    <el-table-column prop="age" label="年龄"/>
                    <el-table-column prop="female" label="性别"/>
                </el-table>
                即使对方了哈就换地方等级划分等级划分等级的就恢复了开始
                <img src="https://img.tuguaishou.com/ips_templ_preview/f0/f9/5a/lg_3599867_1607869606_5fd624a673379.jpg!w1024_w?auth_key=2300366484-0-0-322cbbf653e2e67b5b049a4c3c78070d" :width="400" :height="200"/>
                
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
    import { ref } from "vue";
    import html2canvas from "html2canvas";

    // 绑定  需要把那个内容生成图片
    const canvasRef=ref();

    //下载报告
    function goDown(){
        const {offsetHeight, offsetWidth} = canvasRef.value
        html2canvas(canvasRef.value,{
            backgroundColor:'#23282f',//海报的背景颜色
            useCORS:true, // 允许跨域 
            width:offsetWidth, //生成海报的w
            height:offsetHeight, //生成海报的h    默认是px
        }).then(canvas=>{
            // canvas 其实就是我们所讲的res 的意思 返回报文的意思
            let  baseImg = canvas.toDataURL("image/png");
            let save  = document.createElement('a');
            save.href = baseImg;
            // 下载的名字
            save.download = '报告'
            // 直接回调a的点击事件
            save.click()
    
        })
    }

</script>

<style lang="less" scoped>
    .reporting{
        width: 100%;
        height: 100%;
        padding: 20px;
        box-sizing: border-box;
        .btns{
            display: flex;
            align-items: center;
            &>div:first-child{
                margin-right: 20px;
            }
        }
        .reporting-btn{
            width: 160px;
            height: 32px;
            line-height: 32px;
            text-align: center;
            background-color: rgb(55, 126, 246);
            border-radius: 4px;
            cursor: pointer;
        }
        .content-box{
            width: 100%;
            height: calc(100% - 32px);
            overflow-y: scroll;
            .content-box2{
                width: 100%;
                height: 100%;
                padding: 10px;
                box-sizing: border-box;
            }
        }
    }
</style>

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,我们可以使用uni-app的canvas组件来实现电子签名。以下是实现步骤: 1. 在uni-app项目中安装canvas组件:`npm install --save uni-canvas` 2. 在需要使用电子签名的页面中引入canvas组件: ```vue <template> <view> <canvas :canvas-id="canvasId" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"></canvas> <button @click="clearCanvas">清除</button> <button @click="saveCanvas">保存</button> </view> </template> <script> import uniCanvas from 'uni-canvas'; export default { components: { uniCanvas }, data() { return { canvasId: 'myCanvas', ctx: null, isDrawing: false, lastX: 0, lastY: 0 } }, mounted() { this.ctx = uni.createCanvasContext(this.canvasId, this); }, methods: { handleTouchStart(e) { this.isDrawing = true; this.lastX = e.touches[0].x; this.lastY = e.touches[0].y; }, handleTouchMove(e) { if (!this.isDrawing) return; this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(e.touches[0].x, e.touches[0].y); this.ctx.stroke(); this.lastX = e.touches[0].x; this.lastY = e.touches[0].y; }, handleTouchEnd() { this.isDrawing = false; }, clearCanvas() { this.ctx.clearRect(0, 0, uni.upx2px(375), uni.upx2px(500)); }, saveCanvas() { uni.canvasToTempFilePath({ canvasId: this.canvasId, success: function(res) { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function() { uni.showToast({ title: '保存功' }); } }); } }, this); } } } ``` 在模板中,我们使用canvas组件来绘制电子签名,同时添加了两个按钮,一个用于清除画布,一个用于保存签名。在脚本中,我们使用uni.createCanvasContext方法获取canvas上下文,然后监听touch事件来绘制签名。最后,我们使用uni.canvasToTempFilePath方法将canvas转换为图片,并使用uni.saveImageToPhotosAlbum方法将图片保存到相册中。 3. 在tsconfig.json文件中添加以下配置: ```json { "compilerOptions": { "target": "es6", "module": "esnext", "moduleResolution": "node", "strict": true, "jsx": "preserve", "sourceMap": true, "resolveJsonModule": true, "esModuleInterop": true, "lib": [ "esnext", "dom", "dom.iterable", "scripthost" ] }, "include": [ "src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx" ], "exclude": [ "node_modules" ] } ``` 在tsconfig.json文件中,我们将target设置为es6,module设置为esnext,同时开启了strict模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值