Javascript HTML2Canvas仅渲染屏幕上可见的内容

Javascript HTML2Canvas仅渲染屏幕上可见的内容,javascript,reactjs,html2canvas,pdfjs,Javascript,Reactjs,Html2canvas,Pdfjs,我正在使用HTML2Canvas渲染两个react组件 问题是文档的输出仅呈现屏幕上可见的内容, 因此,如果组件的内容较短,我可以在pdf中看到所有内容, 但是,如果内容很长,我需要滚动到页面的最顶端以使其正常工作,或者我需要将导航缩放到50%,然后单击打印 我的代码中是否有错误: import React, {Component, PropTypes} from 'react'; import html2canvas from 'html2canvas'; import domtoimage

我正在使用HTML2Canvas渲染两个react组件
问题是文档的输出仅呈现屏幕上可见的内容,
因此,如果组件的内容较短,我可以在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;
import React,{Component,PropTypes}来自'React';
从“html2canvas”导入html2canvas;
从“dom到图像”导入domtoimage;
从“jsPDF”导入jsPDF;
从“/Pie.js”导入饼图;
从“/Post.js”导入Post;
类扩展组件{
建造师(道具){
超级(道具);
}
打印文档(){
常量输入=document.getElementById('divToPrint');
html2canvas(输入)
.然后((画布)=>{
设imgWidth=208;
设imgHeight=canvas.height*imgWidth/canvas.width;
const imgData=canvas.toDataURL('img/png');
常量pdf=新的jsPDF('p','mm','a4');
pdf.addImage(imgData,'PNG',0,0,imgWidth,imgHeight);
pdf.save(“download.pdf”);
})
;
}
render(){
返回(
印刷品
);
}
}
导出默认Qu;

解决方案是:添加
window.scrollTo(0,0)
就在
html2canvas(输入)

之前,解决方案是:添加
window.scrollTo(0,0)就在html2canvas(输入)之前