javascript - Html2canvas/jspdf 裁剪图片

标签 javascript image pdf jspdf html2canvas

我想为我的项目中的一个元素制作一个 pdf,但图像总是在右侧被 chop 。 我使用 jspdf 和 html2canvas

这就是我想要的

My destination

这是我在 pdf 中得到的:

My current status

如您所见,图片宽度不合适。

我尝试了以下方法:

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/

相关文章:

javascript - Node.JS HTTPS 问题 TypeError : Cannot read property 'writeQueueSize' of null

javascript - 只允许字符串中的特定字符。 Javascript

javascript - 在 JavaScript Protractor 端到端测试中模拟 HTTP 后端

javascript - 使用Jquery获取图像的位置

ios - Autosize 和 UIImageView 无法正常工作

html - 在 css 中使用线性背景渐变效果后,带有超链接的图像不起作用

javascript - 显示 pdf 的搜索文本以及页码

javascript - Facebook 图形 API : GET/feed without privacy "SELF"

具有垂直分页的android pdf查看器

excel - 将 Excel 范围另存为 pdf