【问题标题】:HTML2Canvas render only what is visible on screenHTML2Canvas 仅呈现屏幕上可见的内容
【发布时间】:2021-02-27 12:56:09
【问题描述】:

我正在使用 HTML2Canvas 渲染两个反应组件
问题是文档的输出仅呈现屏幕上可见的内容,
所以如果组件的内容很短,我可以看到 pdf 中的所有内容,
但是如果内容很长,我需要滚动到页面的最顶部以使其正常工作,或者我需要将导航缩放到 50%,然后单击打印
我的代码有问题吗:

import React, {Component, PropTypes} from 'react';
import html2canvas from 'html2canvas';
import domtoimage from 'dom-to-image';

import jsPDF from 'jspdf';
import Pie from './Pie.js';
import Post from './Post.js';

class Qu extends Component {
  constructor(props) {
    super(props);
  }

  printDocument() {
    const input = document.getElementById('divToPrint');
    html2canvas(input)
    
      .then((canvas) => {
        
        let imgWidth = 208;
        let imgHeight = canvas.height * imgWidth / canvas.width;
        const imgData = canvas.toDataURL('img/png');
        const pdf = new jsPDF('p', 'mm', 'a4');
        pdf.addImage(imgData, 'PNG', 0, 0, imgWidth, imgHeight);
        pdf.save("download.pdf");
      })
    ;
  }

  render() {
    return (<div>
    
      <div id="divToPrint" className="mt4" style={{
                width: '100%',
                minHeight: '29.7cm',
                border: '1px #D3D3D3 solid',
                borderRadius: '5px',
                background: 'white',
                boxShadow: '0 0 5px rgba(0, 0, 0, 0.1)',
                size: 'A4',
                margin: 'auto'
            }}>
      
        <div><Pie /></div>
        <div><Pie /></div>
        <div className="mb5">
        <button onClick={this.printDocument}>Print</button>
      </div>
      </div>
    </div>);
  }
}
export default Qu;

【问题讨论】:

    标签: javascript reactjs html2canvas pdfjs


    【解决方案1】:

    解决方案是:在 html2canvas(input) 之前添加 window.scrollTo(0,0);

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多