使用jspdf将数组导出为表格 [英] export an array as table using jspdf

查看:416
本文介绍了使用jspdf将数组导出为表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据格式如下.

[
    {"Company":"XYZ1", "FName":"John", "LName": "Deere", "ID": 1234}, 
    {"Company":"XYZ2", "FName":"Jack", "LName": "Jones", "ID": 2345},
    {"Company":"XYZ3", "FName":"James", "LName": "Lebron", "ID": 3456}
]

是否可以将2个不同表的相同数据导出为PDF文件?我正在使用Angular 4和jsPDF软件包.

Is there a way to export the same data as 2 different tables into a PDF file ? I am using Angular 4 and jsPDF package.

表格:1

**Company**    **FName**
  XYZ1            John
  XYZ2            Jack
  XYZ3            James

表格:2

**LName**    **ID**
  Deere        1234
  Jones        2345
  Lebron       3456

推荐答案

您可以使用jspdf和jspdf-autotable以pdf格式下载.这是相同的示例.您可以根据需要进行修改.希望对您有所帮助.

You can use jspdf and jspdf-autotable to download as pdf. Here is the example for the same. You can modify according to your needs.. Hope it will help u

在HTML中:

<a (click)="convert()">Generate PDF</a>

在TS文件中:

import * as jsPDF from 'jspdf';
import 'jspdf-autotable';

并使用以下功能:

convert() {

       var doc = new jsPDF();
       var col = ["Sr. No.","Details"];
       var col1 = ["Details", "Values"];
       var rows = [];
       var rows1 = [];

  /* The following array of object as response from the API req  */



var itemNew = [

  { index:'1',id: 'Case Number', name : '101111111' },
  { index:'2',id: 'Patient Name', name : 'UAT DR' },
  { index:'3',id: 'Hospital Name', name: 'Dr Abcd' }

]


   itemNew.forEach(element => {      
        var temp = [element.index,element.id];
        var temp1 = [element.id,element.name];
        rows.push(temp);
        rows1.push(temp1);

    });        

        doc.autoTable(col, rows, { startY: 10 });

        doc.autoTable(col1, rows1, { startY: 60 });
        doc.save('Test.pdf');
      }

它将如下图所示:

这篇关于使用jspdf将数组导出为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆