【问题标题】:Give space to element if it does not fit within A4 size如果元素不适合 A4 尺寸,请为元素留出空间
【发布时间】:2018-08-02 21:04:01
【问题描述】:

我的目标是将包含图像的长 div 导出为 PDF 格式。我正在使用 Html2CanvasJSPDF 库。 我已经设法拍摄快照并将其导出到不同页面中的 JS。 但是,我面临的问题是原始图像被剪裁为 Canvas 元素将其导出为一张大图像。 如果下一个元素不适合 A4 尺寸,我认为将提供帮助的解决方案是提供填充/空间。

以下是我的代码

 function Export2PDF(element){



                     var div = document.getElementById("ExportDiv");
                     var rect = div.getBoundingClientRect();


                     var canvas = document.createElement("canvas");
                     canvas.width = rect.width;
                     canvas.height = rect.height;

                     var ctx = canvas.getContext("2d");
                     ctx.translate(-rect.left,-rect.top);

                     document.body.appendChild(canvas)
                     html2canvas(div, {
                                 canvas:canvas,
                                 height:rect.height,
                                 width:rect.width,
                                 onrendered: function(canvas) {
                                 var image = canvas.toDataURL("image/png");

                                 var imgWidth = 210;
                                 var pageHeight = 295;
                                 var imgHeight = canvas.height * imgWidth / canvas.width;
                                 var heightLeft = imgHeight;
                                 var doc = new jsPDF('p', 'mm');
                                 var position = 0;

                                 doc.addImage(image, 'PNG', 20, position, imgWidth, imgHeight);
                                 heightLeft -= pageHeight;

                                 while (heightLeft >= 0) {
                                 position = heightLeft - imgHeight;
                                 doc.addPage();
                                 doc.addImage(image, 'PNG', 20, position, imgWidth, imgHeight);
                                 heightLeft -= pageHeight;
                                 }
                                 doc.save( 'file.pdf');






                                 }
                                 });
                     }

任何人都可以帮助我实现同样的目标吗?提前谢谢你。

编辑

div结构如下:-

<div>
<table >Table 1
<tr>
<tr>
<tr>
</table>

<table >Table 2</table>
<table >Table 3</table>
</div>

每个 TR 输出为

【问题讨论】:

    标签: javascript jquery html jspdf html2canvas


    【解决方案1】:

    当我遇到类似问题时,我会跟踪高度“指针”的位置以及currentHeight + imageHeight &gt; somevalue 的位置,然后我只是添加了新页面,示例代码:

    var height = 0;
    var image = loadImage();
    ...
    ...
    doc.text(0, height, 'asd');
    height += 30;
    if (height + image.height > 230) //you need to experiment with value, I found that 230 was fine for me
    {
        doc.addPage();
        height = 0;
    }
    doc.image(0, height, image);
    

    【讨论】:

    • 这样添加图片没问题吗?我的图像是不可预测的并且随机出现。
    • 一切正常,您可以在image方法中指定图像的高度和宽度,您需要仔细检查您的代码并寻找高度修改,您需要仔细跟踪高度然后检查是否currentHeight + imageHeight &lt; pageSize,如果你做对了,它不可能不能正常工作
    • 让我分享一下 div 结构的快照。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2015-10-04
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多