how to dynamically populate pdf with pdfmake node

Solutions on MaxInterview for how to dynamically populate pdf with pdfmake node by the best coders in the world

showing results for - "how to dynamically populate pdf with pdfmake node"
Elena
12 Jun 2019
1// playground requires you to assign document definition to a variable called dd
2
3var headers = {
4    fila_0:{
5        col_1:{ text: 'Faltas', style: 'tableHeader',rowSpan: 2, alignment: 'center',margin: [0, 8, 0, 0] },
6        col_2:{ text: 'Fecha', style: 'tableHeader',rowSpan: 2, alignment: 'center',margin: [0, 8, 0, 0] },
7        col_3:{ text: 'Descripción', style: 'tableHeader',rowSpan: 2, alignment: 'center',margin: [0, 8, 0, 0] },
8        col_4:{ text: 'Cita con acudientes', style: 'tableHeader',colSpan: 2, alignment: 'center' }
9    },
10    fila_1:{
11        col_1:{ text: 'Header 1', style: 'tableHeader', alignment: 'center' },
12        col_2:{ text: 'Header 2', style: 'tableHeader', alignment: 'center' }, 
13        col_3:{ text: 'Header 3', style: 'tableHeader', alignment: 'center' },
14        col_4:{ text: 'Citación', style: 'tableHeader', alignment: 'center' },
15        col_5:{ text: 'Cumplimiento', style: 'tableHeader', alignment: 'center'}
16    }
17}
18var rows = {
19    a: {
20        peaje: '1',
21        ruta: '2',
22        fechaCruce: '3',
23        hora: '4',
24        valor: '5'
25    },
26    b: {
27        peaje: '1',
28        ruta: '2',
29        fechaCruce: '3',
30        hora: '4',
31        valor: '5'
32    }
33}
34
35var body = [];
36for (var key in headers){
37    if (headers.hasOwnProperty(key)){
38        var header = headers[key];
39        var row = new Array();
40        row.push( header.col_1 );
41        row.push( header.col_2 );
42        row.push( header.col_3 );
43        row.push( header.col_4 );
44        row.push( header.col_5 );
45        body.push(row);
46    }
47}
48for (var key in rows) 
49{
50    if (rows.hasOwnProperty(key))
51    {
52        var data = rows[key];
53        var row = new Array();
54        row.push( data.peaje.toString() );
55        row.push( data.ruta.toString()  );
56        row.push( data.fechaCruce.toString() );
57        row.push( data.hora.toString()  );
58        row.push( data.valor.toString() );
59        body.push(row);
60    }
61}
62
63var dd = {
64        pageMargins: [40,155,40,55],
65        pageOrientation: 'landscape',
66        header: function() {
67            return {
68                margin: 40,
69                columns: [
70                  {
71                    },
72                    { text:['Resumen disciplinario'], 
73                            alignment: 'left',bold:true,margin:[-405,80,0,0],fontSize: 24}
74                ]
75            }
76        },
77        footer: function(currentPage, pageCount) {
78            return { text:'Pagina '+ currentPage.toString() + ' de ' + pageCount, alignment: 'center',margin:[0,30,0,0] };
79        },
80        content: [
81            //{ text: 'Tables', style: 'header' },
82            '\nEl estudiante AGRESOTH NEGRETE JORYETH TATIANA - 901 - TARDE tiene 1 actas, con 1 faltas acomuladas y a manera de resumen descritas a continuación:\n\n',
83            //{ text: 'A simple table (no headers, no width specified, no spans, no styling)', style: 'sta' },
84            //'The following table has nothing more than a body array',
85            {
86                style: 'tableExample',
87                table: {
88                    widths: [ '*', '*', '*', '*', '*' ],
89                    headerRows: 2,
90                    // keepWithHeaderRows: 1,
91                    body: body
92                }
93            }],
94        styles: {
95            header: {
96                fontSize: 28,
97                bold: true
98            },
99            subheader: {
100                fontSize: 15,
101                bold: true
102            },
103            quote: {
104                italics: true
105            },
106            small: {
107                fontSize: 8
108            },
109            sta: {
110                fontSize: 11,
111                bold: false,
112                alignment: 'justify'
113            }
114        }
115}
116