我想为我的项目中的一个元素制作一个 pdf,但图像总是在右侧被 chop 。 我使用 jspdf 和 html2canvas
这就是我想要的
这是我在 pdf 中得到的:
如您所见,图片宽度不合适。
我尝试了以下方法:
Html2Canvas image is getting cut 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 offscreen
但是这些都不起作用。
这是我的代码:
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);
});
希望有人能帮忙
最佳答案
我今天遇到了类似的问题。我绕过它的方式是因为你在官方 github 上提出了一个问题:https://github.com/eKoopmans/html2pdf.js/issues/39 .
在将页面呈现为 PDF 之前,通过将 html 和 body 标签的宽度设置为 794px,页面可以很好地适应宽度。它对我有用,所以这对我来说已经足够了,尽管它可能不是正确的答案。
关于javascript - Html2canvas/jspdf 裁剪图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59159579/