在vue项目中用html2canvas遇到因为有滚动条截图不完整问题的解决方法(设置height和windowHeight)

本文介绍如何使用html2canvas生成包含复杂内容(如表格和图片)的完整页面截图,解决了因页面内容过多出现滚动条而导致截图不全的问题。
部署运行你感兴趣的模型镜像

最近有业务需求要将某个页面生成图片然后将图片导出,然后我选择用html2canvas进行图片生成.

首先安装html2canvas

npm install html2canvas --save

然后在需要使用的页面导入html2canvas

import html2canvas from 'html2canvas'

生成图片及导出的代码如下

<template>
   <el-dialog id="dialog">
      <el-table></el-table>
      <img />
     <el-button @click="printReport">导出报告</el-button>
   </el-dialog>
</template>

export default {
  methods: {
     printReport() {
        html2canvas(document.getElementById('dialog'), {
          backgroundColor: 'white',
          useCORS: true, //支持图片跨域
          scale: 1, //设置放大的倍数
        })
          .then((canvas) => {
            // 生成图片导出
            const a = document.createElement('a');
            a.href = canvas.toDataURL('image/png');
            a.download = this.title;
            a.click();
          })
    }
  }
} 

结果因为页面有表格有图片内容比较多,出现了纵向的滚动条,截图出来的效果只能截取到视图窗口显示的部分,超出窗口部分则无法生成.我查了一些资料基本都是建议加上以下代码,在生成图片前让页面滚动到最顶端.

window.pageYoffset = 0;

document.documentElement.scrollTop = 0;

document.body.scrollTop = 0;

但是不知道什么原因,我加上这三行代码依然无法成功.没办法我只能再去翻html2canvas的文档,研究与高度相关的属性,最后在设置了如下两个属性后问题终于得到解决!

 html2canvas(document.getElementById('dialog'), {
          backgroundColor: 'white',
          useCORS: true, //支持图片跨域
          scale: 1, //设置放大的倍数
          height: document.getElementById('dialog').scrollHeight,
          windowHeight: document.getElementById('dialog').scrollHeight
        })  

您可能感兴趣的与本文相关的镜像

FLUX.1-dev

FLUX.1-dev

图片生成
FLUX

FLUX.1-dev 是一个由 Black Forest Labs 创立的开源 AI 图像生成模型版本,它以其高质量和类似照片的真实感而闻名,并且比其他模型更有效率

要使用Vue、jspdfhtml2canvas实现滚动表格数据导出为PDF,可按以下步骤操作: ### 1. 安装依赖 在项目中安装`html2canvas``jspdf`: ```bash npm install html2canvas jspdf -s ``` ### 2. 创建导出工具文件 创建一个`htmlToPdf.js`文件,用于封装导出PDF的方法: ```javascript // htmlToPdf.js import html2Canvas from 'html2canvas' import JsPDF from 'jspdf' export default { install(Vue, options) { Vue.prototype.getPdf = function(selector, title) { const scale = 2; html2Canvas(document.querySelector(selector), { allowTaint: true, // 开启跨域 scale // 提升画面质量,但是会增加文件大小 }).then(function(canvas) { const contentWidth = canvas.width / scale; const contentHeight = canvas.height / scale; const PDF = new JsPDF('', 'pt', [contentWidth, contentHeight]); const pageData = canvas.toDataURL('image/jpeg', 1.0); PDF.addImage(pageData, 'JPEG', 0, 0, contentWidth, contentHeight); PDF.save(title + '.pdf'); }) } } } ``` ### 3. 在`main.js`中引入并使用 在`main.js`中引入上述封装的工具文件并使用: ```javascript // main.js import Vue from 'vue' import App from './App.vue' // 引入html转pdf的js import htmlToPdf from './assets/js/htmlToPdf.js' Vue.use(htmlToPdf) new Vue({ render: h => h(App), }).$mount('#app') ``` ### 4. 在Vue组件中使用导出方法 在需要导出滚动表格数据的Vue组件中,添加一个按钮触发导出操作: ```vue <template> <div> <button @click="exportToPdf">导出为PDF</button> <div id="pdfDom"> <!-- 这里放置滚动表格 --> <table> <!-- 表格内容 --> </table> </div> </div> </template> <script> export default { data() { return { htmlTitle: '滚动表格数据' } }, methods: { exportToPdf() { this.getPdf('#pdfDom', this.htmlTitle); } } } </script> ``` ### 注意事项 - 确保滚动表格所在的容器有一个唯一的ID,这里使用`#pdfDom`作为示例。 - `html2canvas`在处理跨域资源时可能会有问题,可通过设置`allowTaint: true`来开启跨域支持。 - 调整`scale`参数可以提升画面质量,但会增加生成的PDF文件大小。
评论 10
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值