暫無描述

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const pdfMake = require('pdfmake/build/pdfmake.js');
  2. const pdfFonts = require('pdfmake/build/vfs_fonts.js');
  3. pdfMake.vfs = pdfFonts.pdfMake.vfs;
  4. function table(course) {
  5. return {
  6. widths: [50, 200, 75, 50, 100],
  7. headerRows: 2,
  8. body: [
  9. [{text: `${course.code}\n${course.title}`, bold: true, colSpan: 5, alignment: 'center'}, {}, {}, {}, {}],
  10. [{text: 'Sección', bold: true}, {text: 'Profesor', bold: true}, {text: 'Lugar', bold: true}, {text: 'Días', bold: true}, {text: 'Horas', bold: true}],
  11. ].concat( course.sections.map( function(section) {
  12. var res = [section.code];
  13. if (section.hasOwnProperty('professors')) {
  14. res = res.concat( section.professors.reduce( function (acc, professor) {
  15. return ((acc === '') ? '' : acc + '\n') + professor.first_name + ' ' + professor.last_name;
  16. }, ''));
  17. } else {
  18. res = res.concat('');
  19. }
  20. if (section.hasOwnProperty('schedules')) {
  21. res = res.concat( section.schedules.reduce( function (acc, schedule) {
  22. return ((acc === '') ? '' : acc + '\n') + schedule.building + ' ' + schedule.room;
  23. }, ''));
  24. res = res.concat( section.schedules.reduce( function (acc, schedule) {
  25. return ((acc === '') ? '' : acc + '\n') + schedule.days;
  26. }, ''));
  27. res = res.concat( section.schedules.reduce( function (acc, schedule) {
  28. return ((acc === '') ? '' : acc + '\n') + schedule.time_start + '-' + schedule.time_end;
  29. }, ''));
  30. } else {
  31. res = res.concat(['', '', '']);
  32. }
  33. return res;
  34. }))
  35. }
  36. }
  37. var docDef = {
  38. content: [
  39. {text: `Horario ${data.dept.name} ${data.alpha}`, style: 'header'},
  40. {text: 'sujeto a cambios', style: 'header', fontSize: 18},
  41. ].concat( data.courses.map( function(course) {
  42. return {
  43. columns: [
  44. { width: '*', text: ''}, // Aligns table to center
  45. {
  46. style: 'tableStyle',
  47. alignment: 'center',
  48. width: 'auto',
  49. table: table(course)
  50. },
  51. { width: '*', text: ''} // Aligns table to center
  52. ],
  53. };
  54. })),
  55. styles: {
  56. header: {
  57. fontSize: 22,
  58. bold: true,
  59. alignment: 'center',
  60. },
  61. tableStyle: {
  62. margin: [0,20,0,20]
  63. },
  64. },
  65. pageBreakBefore: function(currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) {
  66. var pageInnerHeight = currentNode.startPosition.pageInnerHeight;
  67. var top = (currentNode.startPosition.top) ? currentNode.startPosition.top : 0;
  68. var nodeHeight = 0; // Distance from top of table to top of next table
  69. if (followingNodesOnPage && followingNodesOnPage.length) {
  70. var nextTableNodeIndex = followingNodesOnPage.findIndex( function(n) { return n.table; })
  71. if (nextTableNodeIndex !== -1 && nextTableNodeIndex < followingNodesOnPage.length) {
  72. nodeHeight = followingNodesOnPage[nextTableNodeIndex].startPosition.top - top;
  73. }
  74. }
  75. // if (currentNode.table) {
  76. // console.log(currentNode);
  77. // console.log(currentNode.pageNumbers)
  78. // console.log(currentNode.table.body[0][0].text);
  79. // console.log('t ' + top);
  80. // console.log('nh ' + nodeHeight);
  81. // console.log('pih ' + pageInnerHeight);
  82. // console.log((top + nodeHeight > pageInnerHeight));
  83. // }
  84. return (currentNode.table && (top + nodeHeight > pageInnerHeight))
  85. || currentNode.startPosition.verticalRatio >= 0.90;
  86. }
  87. };
  88. // console.log(docDef);
  89. pdfMake.createPdf(docDef).download(data.alpha + '.pdf', function () { window.location.replace('/dashboard')});
  90. // window.location.replace("/dashboard");