我想要将项目中的一个元素制作成PDF文件,但是图像总是在右侧被裁剪掉了。我使用了jspdf和html2canvas。
这是我想要的效果:
而这是我在PDF中得到的结果:
正如您所看到的,图像无法适应右侧宽度。
我尝试过以下方法:
Html2Canvas图片被裁剪 https://tutel.me/c/programming/questions/44796908/jspdf+html2canvas++html++images+cut+off+capture https://www.reddit.com/r/learnjavascript/comments/cnn7q4/anyone_experience_with_jspdf_my_image_is_cut_off/ html2canvas离屏
但是这些方法都不起作用。
以下是我的代码:
const offerteId = $(e).data("id"),
card = document.querySelector('.body-pdf-' + offerteId + ''),
filename = offerteId + '.pdf';
html2canvas(card).then(function(canvas) {
const img = canvas.toDataURL("image/png"),
pdf = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4',
});
// Optional - set properties on the document
pdf.setProperties({
title: offerteId.toString(),
subject: 'Offerte: ' + offerteId.toString(),
});
const imgProps = pdf.getImageProperties(img);
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(img, 'JPEG', 0, 0, pdfWidth, pdfHeight);
pdf.save(filename);
});
希望有人能够帮忙