随笔 - 32,  文章 - 0,  评论 - 0,  阅读 - 38203

首先我们要把页面连接放在ifram中,利用html2canvas和Canvas2Image对页面进行转换

<template>

<div style="height: 90vh;"><iframe src="XXX.html" frameborder="0" height="90%" id="frameid" width="100%"></iframe></div>
<div id="img" v-html="html"></div>
<button @click="toPic">下载图片</button>

script

import html2canvas from 'html2canvas'
import Canvas2Image from './canvas2image/index'

const toPic = () => {
  const ifram: any = document.getElementById('frameid')
  const testbody: any = ifram.contentWindow.document.getElementById('testbody'); // textbody为XXX.html的body的id名称
  let canvas2 = document.createElement("canvas");
  let w = parseInt(window.getComputedStyle(testbody).width);
  let h = parseInt(window.getComputedStyle(testbody).height);
  let context =  canvas2.getContext("2d");
  context?.scale(1,1);
  canvas2.width = w;
  canvas2.height = h;
  html2canvas(testbody, {canvas: canvas2}).then((canvas: any) => {
  const data = document.getElementById('img');
  let img:any = Canvas2Image.convertToImage(canvas, w , h)
  data?.appendChild(img)
  // console.log(state.html)
  })
  }

Canvas2Image 下载地址 https://github.com/randreucetti/canvas2image
html2canvas 可以直接用npm安装: npm install html2canvas -S

pdf转图片同样适用

posted on   打怪升级小妮子  阅读(1203)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示