简体   繁体   English

如何导出带有固定 header 的 `ag-grid`.PDF 版本?

[英]How to export .PDF version of `ag-grid` with fixed header?

I need to export Ag-Grid to PDF .我需要将Ag-Grid导出到PDF To do this I became able to convert ag-grid div to canvas (html2canvas) img .为此,我能够将ag-grid div转换为canvas (html2canvas) img So, converting img to PDF (jsPdf) works fine.因此,将img转换为PDF (jsPdf) 工作正常。

在此处输入图像描述

But since I have pinned header on Grid,it only converts the visible part of grid not all rows in grid.但是由于我在网格上固定了 header,它只转换网格的可见部分而不是网格中的所有行。 To do this I tried something like below but didn't work为此,我尝试了类似下面的方法,但没有用

downloadAsPdf() {
    let doc = new jsPDF();
    html2canvas(this.agGrid["_nativeElement"]).then(canvas => {
        var imgWidth = 210;
        var pageHeight = 295;
        var imgHeight = canvas.height * imgWidth / canvas.width;
        var position = 0;
        var heightLeft = imgHeight;
        let imageData = getBase64Image(canvas);
        doc.addImage(imageData, "PNG", 0, position, imgWidth, imgHeight);
        heightLeft -= pageHeight;
        while (heightLeft >= 0) {
            position = heightLeft - imgHeight;
            doc.addPage();
            doc.addImage(imageData, 'PNG', 0, position, imgWidth, imgHeight);
            heightLeft -= pageHeight;
        }
        doc.save("example.pdf");
    });
}

function getBase64Image(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    var dataURL = canvas.toDataURL("image/png");
    return dataURL;
}

I also tried to send second parameter to html2Canvas like below but didnt work either我还尝试将第二个参数发送到 html2Canvas,如下所示,但也没有用

    {scrollY: -window.scrollY}
{
        scrollX: 0,
        scrollY: 0
      }

Stackblitz link: https://stackblitz.com/edit/angular-grid-dynamic-height-snfadt?file=src%2Fapp%2Fapp.component.ts Stackblitz 链接: https://stackblitz.com/edit/angular-grid-dynamic-height-snfadt?file=src%2Fapp%2Fapp.component.ts

pdfMake includes the header row of a table on every page by default.默认情况下,pdfMake 在每一页上包含表格的 header 行。

See the example below:请参见下面的示例:

https://stackblitz.com/edit/js-ag-grid-pdf-make?file=index.js https://stackblitz.com/edit/js-ag-grid-pdf-make?file=index.js

To create the exported table you need to create a 2D array containing all columns and rows to be exported.要创建导出的表,您需要创建一个包含所有要导出的列和行的二维数组。 This can be done with the ag-Grid API which allows you to iterate over all columns and rows with the methods columnApi.getAllDisplayedColumns() and gridApi.forEachNode .这可以通过 ag-Grid API 来完成,它允许您使用方法columnApi.getAllDisplayedColumns()gridApi.forEachNode遍历所有列和行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM