HTML2Canvas只呈现屏幕上可见的内容



我使用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;

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

最新更新