将html导出图片

378 阅读1分钟
import { saveAs } from 'filesaver.js-npm';
import html2canvas from 'html2canvas';
exportJPEG = () => {
    let { shop } = this.props;
    let element = document.querySelector('.pdf');
    let { height } = getComputedStyle(element, false);
    let { width } = getComputedStyle(element, false);
    let canvas = document.createElement('canvas');
    canvas.width = parseInt(width, 10);
    canvas.height = parseInt(height, 10);
    let context = canvas.getContext('2d');
    context.scale(2, 2);
    html2canvas(element, {
      useCORS: true,
    }).then(canvas => {
      canvas.toBlob((blob) => {
        saveAs(blob, `${shop.shopName}.jpeg`);
      });
    });
  }