const pdfMake = require('pdfmake/build/pdfmake.js'); const pdfFonts = require('pdfmake/build/vfs_fonts.js'); pdfMake.vfs = pdfFonts.pdfMake.vfs; function table(course) { return { widths: [50, 200, 75, 50, 100], headerRows: 2, body: [ [{text: `${course.code}\n${course.title}`, bold: true, colSpan: 5, alignment: 'center'}, {}, {}, {}, {}], [{text: 'Sección', bold: true}, {text: 'Profesor', bold: true}, {text: 'Lugar', bold: true}, {text: 'Días', bold: true}, {text: 'Horas', bold: true}], ].concat( course.sections.map( function(section) { var res = [section.code]; if (section.hasOwnProperty('professors')) { res = res.concat( section.professors.reduce( function (acc, professor) { return ((acc === '') ? '' : acc + '\n') + professor.first_name + ' ' + professor.last_name; }, '')); } else { res = res.concat(''); } if (section.hasOwnProperty('schedules')) { res = res.concat( section.schedules.reduce( function (acc, schedule) { return ((acc === '') ? '' : acc + '\n') + schedule.building + ' ' + schedule.room; }, '')); res = res.concat( section.schedules.reduce( function (acc, schedule) { return ((acc === '') ? '' : acc + '\n') + schedule.days; }, '')); res = res.concat( section.schedules.reduce( function (acc, schedule) { return ((acc === '') ? '' : acc + '\n') + schedule.time_start + '-' + schedule.time_end; }, '')); } else { res = res.concat(['', '', '']); } return res; })) } } var docDef = { content: [ {text: `Horario ${data.dept.name} ${data.alpha}`, style: 'header'}, {text: 'sujeto a cambios', style: 'header', fontSize: 18}, ].concat( data.courses.map( function(course) { return { columns: [ { width: '*', text: ''}, // Aligns table to center { style: 'tableStyle', alignment: 'center', width: 'auto', table: table(course) }, { width: '*', text: ''} // Aligns table to center ], }; })), styles: { header: { fontSize: 22, bold: true, alignment: 'center', }, tableStyle: { margin: [0,20,0,20] }, }, pageBreakBefore: function(currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) { var pageInnerHeight = currentNode.startPosition.pageInnerHeight; var top = (currentNode.startPosition.top) ? currentNode.startPosition.top : 0; var nodeHeight = 0; // Distance from top of table to top of next table if (followingNodesOnPage && followingNodesOnPage.length) { var nextTableNodeIndex = followingNodesOnPage.findIndex( function(n) { return n.table; }) if (nextTableNodeIndex !== -1 && nextTableNodeIndex < followingNodesOnPage.length) { nodeHeight = followingNodesOnPage[nextTableNodeIndex].startPosition.top - top; } } // if (currentNode.table) { // console.log(currentNode); // console.log(currentNode.pageNumbers) // console.log(currentNode.table.body[0][0].text); // console.log('t ' + top); // console.log('nh ' + nodeHeight); // console.log('pih ' + pageInnerHeight); // console.log((top + nodeHeight > pageInnerHeight)); // } return (currentNode.table && (top + nodeHeight > pageInnerHeight)) || currentNode.startPosition.verticalRatio >= 0.90; } }; // console.log(docDef); pdfMake.createPdf(docDef).download(data.alpha + '.pdf', function () { window.location.replace('/dashboard')}); // window.location.replace("/dashboard");