首页 > 解决方案 > 如何以 7/8/9/10 的角度将表格转换为 pdf?

问题描述

我进行了广泛的搜索,发现了许多适用于 jsPDF 的解决方案。但是我找到的解决方案要么尝试手动添加文本行,这在我的场景中是不可能的;或者他们转换

标签: angular

解决方案


首先安装jspdf库

“npm 安装 jspdf --save”

jspdf@2.3.1 为我工作。

然后在组件中导入jspdf并创建导出功能

从'jspdf'导入{jsPDF};

      generatePdf() {
            var head = [['NAME', 'DATE', 'TIME']];
            var table_data = [];
    
            array.forEach((element: any) => {
            var item = [element.Title, element.Date, element.Time];
            table_data.push(item);
            });
    
            var doc = new jsPDF();
    
            doc.setFontSize(18);
            doc.text('Page', 11, 8);
            doc.setFontSize(11);
            doc.setTextColor(100);
            (doc as any).autoTable({
            head: head,
            body: table_data,
            theme: 'striped',
            didDrawCell: data => {
                // data
            }
            })
    
            var data64 = doc.output('datauristring');
    
            // create pdf file and download it
            let a = document.createElement("a");
            a.href = data64;
            a.download = "myearning.pdf"
            a.click();
    
        }

推荐阅读