react项目用html2canvas导出图片,加粗等样式失效
问题描述
react项目用html2canvas导出图片,加粗等样式失效
问题出现的环境背景及自己尝试过哪些方法
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
exportPDF = () => {
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 => {
let pdf = new JsPDF('', 'pt', 'a4');
context.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
let pdfWidth = canvas.width;
let pdfHeight = canvas.height;
let pageHeight = pdfWidth / 592.28 * 841.89;
let leftHeight = pdfHeight;
let position = 0;
let imgWidth = 595.28;
let imgHeight = 592.28 / pdfWidth * pdfHeight;
let pageData = canvas.toDataURL('image/jpeg', 1.0);
if (leftHeight < pageHeight) {
pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
} else {
while (leftHeight > 0) {
pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight);
leftHeight -= pageHeight;
position -= 841.89;
if (leftHeight > 0) {
pdf.addPage();
}
}
}
pdf.save(`${shop.shopName}.pdf`);
});
}
你期待的结果是什么?实际看到的错误信息又是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论